Equilibrium index of an array | Practice | GeeksforGeeks

Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes.
Given an array, your task is to find the index of first Equilibrium point in the array.

Input Format:
The first line of input takes an integer T denoting the no of test cases, then T test cases follow. The first line of each test case is an integer N denoting The size of the array. Then in the next line are N space-separated values of the array. 

Output Format:
For each test case, the output will be the equilibrium index of the array. If no such index exists output will be -1.
User Task :
Your task is to complete the function (findEquilibrium) below which returns the index of the first Equilibrium point in the array. The function takes two arguments. The first argument is the array A[ ] and the second argument is the size of the array A.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
1<=T<=100
1<=N<=105
-1000<=A[]<=1000

Example(To be used only for expected output):
Input
2
4
1 2 0 3
4
1 1 1 1

Output
2
-1

Note: The Input/Output format and Examples given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
0
swapniltayal4221 month ago
def findEquilibrium(a,n):   # Code here   rsum = sum(arr)   lsum = 0   for i in range(n-1, -1, -1):              rsum -= a[i]       if rsum == lsum:           return i                     lsum += a[i]      return -1
0
lakshaychauhan1 month ago
C++ | PREFIX SUM METHOD | EASY | T.C - O(N)
    int sum = 0;   for (int i = 0; i < n; i++)       sum += A[i];   int lsum = 0, rsum = sum;   for (int i = 0; i < n; i++)   {       rsum -= A[i];       if (lsum == rsum)           return i;       lsum += A[i];   }   return -1;
0
ramdurgasais2 months ago
In Python
def findEquilibrium(a,n):
    left_sum, right_sum = 0, sum(a)
    for i in range(n):
        right_sum -= a[i]
        if left_sum == right_sum : return i
        left_sum += a[i]
    return -1
0
kartikeyashokgautam2 months ago
            int total = 0, sum = 0;       for (int num : arr) total += num;       for (int i = 0; i < n;i++)
     {      sum += arr[i];           if (sum * 2 == total - arr[i]) return i;
     
  }       return -1;             
+1
kartikeyashokgautam
This comment was deleted.
+1
singhalsarthak7773 months ago
EASY CPP SOLUTION
int rightsum=0,leftsum=0;for(int i=0;i<n;i++){    rightsum+=a[i];}     for(int i=0;i<n;i++){   rightsum=rightsum-a[i];    if(rightsum==leftsum)    return i;    leftsum=leftsum+a[i];}return -1;
0
mauryasangeeta33213 months ago
int findEquilibrium(int A[], int n){ //Your code here int sum1=0,sum2=0;  for(int i=0;i<n;i++)     sum1+=A[i];     for(int i=0;i<n;i++) {     if(sum1-A[i]==2*sum2)     return i;     sum2+=A[i]; } return -1;}
0
mauryasangeeta3321
This comment was deleted.
0
ekkafranco3 months ago
int findEquilibrium(int A[], int n){   if (n==1)       return 0;   if (n==2)       return -1;   int mid = n/2;   int res1=0;   int res2=0;         for(int i=0;i<mid;i++)           res1+=A[i];       for(int j=mid+1;j<n;j++)           res2+=A[j];
       while(mid>0){           if(res1==res2)               return mid;           res1= res1-A[mid-1];           res2=res2+A[mid];           mid--;       }       while(mid<n){           if(res1==res2)               return mid;           res1= res1+A[mid];           res2=res2-A[mid+1];           mid++;       }   return -1;}
+1
ekkafranco
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.