Equilibrium Point | Practice | GeeksforGeeks

Given an array A of n positive numbers. The task is to find the first Equilibium Point in the array. 
Equilibrium Point in an array is a position such that the sum of elements before it is equal to the sum of elements after it.
Note: Retun the index of Equilibrium point. (1-based index)
Example 1:
Input: 
n = 5 
A[] = {1,3,5,2,2} 
Output: 3 
Explanation: For second test case 
equilibrium point is at position 3 
as elements before it (1+3) = 
elements after it (2+2). 

 
Example 2:
Input:
n = 1
A[] = {1}
Output: 1
Explanation:
Since its the only element hence
its the only equilibrium point.
 
Your Task:
The task is to complete the function equilibriumPoint() which takes the array and n as input parameters and returns the point of equilibrium. Return -1 if no such point exists. 
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(1)
Constraints:
1 <= n <= 106
1 <= A[i] <= 108
0
somnathjha07200012 hours ago
java 
  if(n==1) return 1;      long leftsum = 0;      long rightsum = 0;      for(int i =1;i<n;i++)      {          rightsum +=arr[i];      }      for(int i =1;i<n;i++)      {          if(rightsum-leftsum==0) return i;          else{              leftsum +=arr[i-1];              rightsum -=arr[i];          }      }           return -1;
}
0
dharpranoy225513 hours ago
java solution:
       if (n==1){           return 1;       }       long leftsum=arr[0];       long rightsum=0;       for (int i=1;i<n;i++){           rightsum+=arr[i];       }       for (int i=1;i<n;i++){           rightsum-=arr[i];           if (leftsum==rightsum){               return i+1;           }           leftsum+=arr[i];       }       return -1;
+1
bipulharsh1231 day ago
int equilibriumPoint(long long a[], int n) {          // Your code here       if(n==1)           return 1;       if(n==2)           return -1;       int equipt = 1;       int lsum = a[0];       int rsum = 0;       for(int ind=2; ind<n; ind++)           rsum += a[ind];       while(lsum < rsum){           lsum += a[equipt++];           rsum -= a[equipt];       }       if(lsum == rsum)           return equipt+1;       return -1;   }
0
mrnecto1 week ago
C++
class Solution{
   public:
   // Function to find equilibrium point in the array.
   // a: input array
   // n: size of array
   int equilibriumPoint(long long a[], int n) {
       // Your code here
       if(n == 1) return 1;
       int left = 0, right = 0, i, j;
       for(i = 0, j = n - 1; i <= j;)
       {
           if(i == j && left == right) return i + 1;
           if(left < right) left += a[i++];
           else right += a[j--];
       }
       return -1;
   }
};
0
shrivastavap6911 week ago
cpp sol
int equilibriumPoint(long long a[], int n) {
        
        long long sum=0;
        for(int i=0;i<n;i++){
            sum+=a[i];
        }
        
        int s=0;
        
        for(int i=n-1;i>=0;i--){
            if(sum-a[i]-(2*s) == 0)
                return i+1;
            s+=a[i];
        }
        
        return -1;
        
    }
+1
bhardwajji1 week ago
int equilibriumPoint(long long a[], int n) {
        
        long long sum=0;
        for(int i=0;i<n;i++){
            sum+=a[i];
        }
        
        int s=0;
        
        for(int i=n-1;i>=0;i--){
            if(sum-a[i]-(2*s) == 0)
                return i+1;
            s+=a[i];
        }
        
        return -1;
        
    }
0
sobhan7dash1 week ago
Python Binary Search
def equilibriumPoint(self,A, N):
        mid= N // 2
        left = 0
        right = 0
        for i in range(mid):
            left += A[i]
        for i in range(N-1, mid, -1):
            right += A[i]
        
        if right > left:
            while right > left and mid < N-1:
                right -= A[mid+1]
                left += A[mid]
                mid += 1
        else:
            while left > right and mid > 0:
                right += A[mid]
                left -= A[mid-1]
                mid -= 1
        if right == left:
            return mid+1
        else:
            return -1
+1
ashayjainrocks102 weeks ago
C++ || O(1) space || O(n) time
int equilibriumPoint(long long a[], int n) {
        long long sum=0,pre=0;
        for(int i=0;i<n;i++) sum+=a[i];
        for(int i=n-1;i>=0;i--)
        {
            if(sum-a[i]-pre==pre) return i+1;
            pre+=a[i];
        }
        return -1;
    }

0
ashayjainrocks102 weeks ago
C++ || Naive approach
int equilibriumPoint(long long a[], int n) {
    
        long long pre[n]={0};
        long long post[n]={0};
        for(int i=1;i<n;i++)
        {
            pre[i]=pre[i-1]+a[i-1];
            post[n-i-1]=post[n-i]+a[n-i];
        }
        for(int i=0;i<n;i++)
        {
            if(pre[i]==post[i]) return i+1;
        }
        return -1;
    }

+1
atharvadev2 weeks ago
Best time and space JAVA
 
public static int equilibriumPoint(long arr[], int n) {
       long sum[] = new long[arr.length];       if(arr.length==1){           return 1;       }       sum[0] = arr[0];       for(int i=1; i<arr.length; i++){           sum[i]=sum[i-1]+arr[i];       }       for(int i=1; i<sum.length-1; i++){           if(sum[i-1]==sum[sum.length-1]-sum[i]){               return i+1;           }       }       return -1;   }
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.