Finds index in array such that - sum of elements on whose left is equal to sum of elements on right in java


In this core java programming tutorial we will write a program to Finds index in array such that - sum of elements on whose left is equal to sum of elements on right in java.

Let’s understand the question first :
In ar=[2 4 4 1 1 ] : sum of elements on left(i.e. from index 0 to 1) = sum of elements on right(i.e. from index 2 to 4)



In ar=[2 1 1 4 ] : sum of elements on left(i.e. from index 0 to 2) = sum of elements on right(i.e. from index 3 to 3)


In ar=[2 1 1 2 2 4 ] : sum of elements on left(i.e. from index 0 to 3) = sum of elements on right(i.e. from index 4 to 5)

In ar=[2 4 1 1 ] : No index found where  sum of elements on left = sum of elements on right

In this program we will try to finds index in array such that - sum of elements on whose left is equal to sum of elements on right in java.





Full Program/SourceCode / Example to Finds index in array such that - sum of elements on whose left is equal to sum of elements on right in java >
/** Copyright (c), AnkitMittal www.JavaMadeSoEasy.com */
public class FindIndexSumOfElementsWhoseLeftIsEqualToRightExample {
   public static void main(String...args) {
       int[] ar = { 2, 4, 4, 2, 6, 1, 1 };
      
       System.out.print("In ar=[");
       for(int n:ar){
          System.out.print(n+" ");
       }
       System.out.print("] : ");
       int findIndex=findIndex(ar);
       System.out.println(findIndex!=-1 ? "sum of elements on left(i.e. from index 0 to "+findIndex +") = sum of elements on right(i.e. from index " + (findIndex+1)+" to "+ (ar.length-1) +")"  :  "No index found where  sum of elements on left = sum of elements on right");
      
   }
   /**
   * Finds index such that > sum of elements on whose left is equal to sum of elements on right
   */
   public static int findIndex(int[] ar) {
      int leftIndex = 0, rightIndex = ar.length - 1;
       int leftSum=0 , rightSum = 0;
      
       while (leftIndex <= rightIndex) {
        if (leftSum > rightSum)
            rightSum = rightSum + ar[rightIndex--];
        else
            leftSum = leftSum + ar[leftIndex++];
       }
  
       if (leftSum == rightSum)
          return rightIndex;
       else
        return -1; //appropriate index not found
      
      
   }
}
/*OUTPUT
In ar=[2 4 4 2 6 1 1 ] : sum of elements on left(i.e. from index 0 to 2) = sum of elements on right(i.e. from index 3 to 6)
*/


So in this core java programming tutorial we wrote a program how to Finds index in array such that - sum of elements on whose left is equal to sum of elements on right in java.



Previous program                                                                  Next program




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>




>Pattern/Pyramid generating programs



eEdit
Must read for you :