//{ Driver Code Starts #include <iostream> using namespace std; // } Driver Code Ends 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) { int i=0; int curr=0; int sum=0; for(int i=0 ; i<n ; i++) { sum+=a[i]; } while(i<n) { curr+=a[i]; if(curr==sum) { return i+1; } else { sum-=a[i]; } i++; } return -1; } }; //{ Driver Code Starts. int main() { long long t; //taking testcases cin >> t; while (t--) { long long n; //taking input n cin >> n; long long a[n]; //adding elements to the array for (long long i = 0; i < n; i++) { cin >> a[i]; } Solution ob; //calling equilibriumPoint() function cout << ob.equilibriumPoint(a, n) << endl; } return 0; } // } Driver Code Ends
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter