1

C program that finds starting and ending indexes of subarray to remove to make given array have equal sums of left and right side. If its impossible print -1. I need it to work for any array, this is just for testing. Heres a code that finds equilibrium.

#include <stdio.h> 

int equilibrium(int arr[], int n) 
{ 
int i, j; 
int leftsum, rightsum; 

/* Check for indexes one by one until 
an equilibrium index is found */
for (i = 0; i < n; ++i) {    

    /* get left sum */
    leftsum = 0; 
    for (j = 0; j < i; j++) 
        leftsum += arr[j]; 

    /* get right sum */
    rightsum = 0; 
    for (j = i + 1; j < n; j++) 
        rightsum += arr[j]; 

    /* if leftsum and rightsum are same, 
    then we are done */
    if (leftsum == rightsum) 
        return i; 
} 

/* return -1 if no equilibrium index is found */
return -1; 
} 

// Driver code 
int main() 
{ 
int arr[] = { -7, 1, 5, 2, -4, 3, 0 }; 
int arr_size = sizeof(arr) / sizeof(arr[0]); 
printf("%d", equilibrium(arr, arr_size)); 

getchar(); 
return 0; 
}
Joaquin
  • 2,013
  • 3
  • 14
  • 26
haxor567
  • 11
  • 3
  • Sounds like a fun problem. What you have tried so far? – user3386109 Dec 23 '18 at 01:59
  • Have it for one element to remove, need to make it to remove subarray. – haxor567 Dec 23 '18 at 02:08
  • So the easy O(n^2) answer is two nested `for` loops. The outer loop iterates the starting index, the inner loop iterates the ending index (which must be equal to or greater than the start). – user3386109 Dec 23 '18 at 02:14
  • I guess I should have mentioned that the sums should be updated incrementally. For example, the `leftSum` starts with `arr[0]`. Each time the start index increments, the `leftSum` is updated with `leftSum += arr[start]`. For that to work in the inner loop, the inner loop must run in reverse (starting at the end of the array and working backwards through the array until it reaches the start index). – user3386109 Dec 23 '18 at 02:26
  • Could you use C++`stdlib`? – Joaquin Dec 23 '18 at 02:59
  • just c, no c++. If someone could give me complete code, it would be great. – haxor567 Dec 23 '18 at 03:08
  • Can the left and right be empty (sub-array is the same size as the whole array) so that the sums of the left and right are always both zero? ;-) – Brendan Dec 23 '18 at 03:27
  • left and right cant be empty. – haxor567 Dec 23 '18 at 08:14

1 Answers1

1

You could solve this problem in O(NlogN) or O(N)(in average case).

First, you need to make a pre-processing, saving all the sums from right to left in a data structure, specifically a balanced binary search tree(e.g. Red Black tree, AVL Tree, Splay Tree, etc, if you could use stdlib, just use std::multiset) or a HashTable(in stdlib it's std::unordered_multiset):

void preProcessing(dataStructure & ds, int * arr, int n){
    int sum = 0;
    int * toDelete = (int) malloc(n)
    for(int i = n-1; i >= 0; --i){
        sum += arr[i];
        toDelete[i] = sum; // Save items to delete later.
        tree.insert(sum);
    }

So to solve problem you just need to traverse the array one time:

// It considers that the deleted subarray could be empty
bool Solve(dataStructure & ds, int * arr, int n, int * toDelete){
    int sum = 0;
    bool solved = false; // true if a solution is found
    for(int i = 0 ; i < n; ++i){ 
        ds.erase(toDelete[i]); // deletes the actual sum up to i-th element from data structure, as it couldn't be part of the solution.
                               // It costs O(logN)(BBST) or O(1)(Hashtable)
        sum += arr[i];
        if(ds.find(sum)){// If sum is in ds, then there's a leftsum == rightsum
                         // It costs O(logN)(BBST) or O(1)(HashTable)
            solved = true;
            break;
        }
    }
    return solved;
}

Then, if you use a BBST(Balanced Binary Search Tree) your solution would be O(NlogN), but, if you use HashTable, your solution would be O(N) on average. I wouldn't implemented it, so maybe there's a bug, but I try to explain the main idea.

Joaquin
  • 2,013
  • 3
  • 14
  • 26
  • It's generally not that useful to answer a C question with C++. There is no `std::xxxx` available in C as the question in tagged. (your general advise is sound, but must be implemented from scratch in C) – David C. Rankin Dec 23 '18 at 10:18
  • Data structure could be a stdlib but also you could implement it with C. – Joaquin Dec 23 '18 at 14:52