Search code examples
javaarrayssub-array

split array into two equal subarray where index is selected


I need to return an index of the element where the sum of the elements on the left is equal to the sum of the elements on the right. e.g for the array [-3, 8, 3, 1, 1, 3], the return value is index 2 since the sum of the elements to the left of the first 3 ([-3, 8]) is the same as the sum of elements to its right ([1, 1, 3]).

So I started by doing a liner-search function to find the intended index, then after that i attempted to split the array left and right of the selected index but had no success doing so

I haven't had much success getting it to work

//linear-search portion,x is the index selected to be split point
public static int findindex(int arr[], int x) {
//if array is null  
if (arr == null) {
        return -1;
    }
//find array length
int len = arr.length;
int i = 0;
//traverse the array
while (i < len) {
//if the i-th element is is x then return the index
    if (arr[i] == x) {
        return i;
    } else {
        i = i + 1;
    }
}
//splint array portion,returns index if not possible
int leftsum = 0;
//treverse array elements
for (int i = 0; i < x; i++) {
//adds current elements to left
    leftsum += arr[i];
//find sum of remader the array elements to rightsum
    int rightsum = 0;
    for (int j = i + 1; j < x; J++)
        rightsum += arr[j];
//split pint index
    if (leftsum == rightsum)
        return i + 1;
}
//if not possible return
return -1;
}

// driver code
public static void main(String[] args) {
    int[] array1 = { -3, 8, 3, 1, 1, 3 };
    System.out.println(findindex(array1));
}

Solution

  • You can use the below code for solve the problem

    static void Main(string[] args)
            {
                int[] array1 = {-3, 8, 3, 1, 1, 3}; // { -3, 8, 3, 1, 1, 3, 6, 1, 19 };
                int indexPosition = GetIndex(array1);
                if (indexPosition != -1)
                {
                    Console.WriteLine(indexPosition);
                }
            }
    
            static int GetIndex(int[] param)
            {
                if (param.Length < 0) return -1;
                int leftSum = 0, rightSum = 0; int rightIndex = param.Length - 1;
                for (int i = 0; i < param.Length; i++)
                {
                    if (i < rightIndex)
                    {
                        if (leftSum > rightSum)
                        {
                            rightSum += param[rightIndex];
                            rightIndex -= 1;
                        }
                        else
                        {
                            if (i < rightIndex)
                            {
                                leftSum += param[i];
                            }
                        }
                    }
                    else
                    {
                        rightSum += param[rightIndex]; // if you are looking for only index position you can comment this line,
                        //variable rightSum and leftSum will give you the sum of left and right side of the array
                        rightIndex -= 1;
                        break;
                    }
                }
    
                return rightIndex;
            }
    

    Hope this helps .