|
| 1 | +/* |
| 2 | + NOTE:- it is imp ARRAY QUESTION NOT STRING |
| 3 | + link:- https://practice.geeksforgeeks.org/problems/equilibrium-point-1587115620/1 |
| 4 | + |
| 5 | + problem:- Given an array A of n positive numbers. The task is to find the first Equilibium Point in the array. |
| 6 | +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. |
| 7 | + |
| 8 | +Input: |
| 9 | +n = 5 |
| 10 | +A[] = {1,3,5,2,2} |
| 11 | +Output: 3 |
| 12 | +Explanation: For second test case |
| 13 | +equilibrium point is at position 3 |
| 14 | +as elements before it (1+3) = |
| 15 | +elements after it (2+2). |
| 16 | + |
| 17 | +Input: |
| 18 | +n = 1 |
| 19 | +A[] = {1} |
| 20 | +Output: 1 |
| 21 | +Explanation: |
| 22 | +Since its the only element hence |
| 23 | +its the only equilibrium point. |
| 24 | + |
| 25 | +*/ |
| 26 | + |
| 27 | +// { Driver Code Starts |
| 28 | +#include <iostream> |
| 29 | +using namespace std; |
| 30 | + |
| 31 | + |
| 32 | + // } Driver Code Ends |
| 33 | +class Solution{ |
| 34 | + public: |
| 35 | + // Function to find equilibrium point in the array. |
| 36 | + // a: input array |
| 37 | + // n: size of array |
| 38 | + int equilibriumPoint(long long arr[], int n) { |
| 39 | + int t_sum=0,sum1=0,sum2=0; |
| 40 | + if(n==1) |
| 41 | +{ |
| 42 | + return 1; |
| 43 | +} |
| 44 | + for(int i=0;i<n;i++) |
| 45 | +{ |
| 46 | + t_sum+=arr[i]; |
| 47 | +} |
| 48 | + for(int i=1;i<n;i++) |
| 49 | +{ |
| 50 | + sum1=sum1+arr[i-1]; |
| 51 | + sum2=t_sum-sum1-arr[i]; |
| 52 | + |
| 53 | + if(sum1==sum2) |
| 54 | + { |
| 55 | + return i+1; |
| 56 | + } |
| 57 | + |
| 58 | +} |
| 59 | + return -1; |
| 60 | +} |
| 61 | + |
| 62 | +}; |
| 63 | + |
0 commit comments