class Solution { // Function to find equilibrium point in the array. public static int equilibriumPoint(long a[], int n) { //We store the sum of all array elements. long sum = 0; for (int i = 0; i < n; i++) sum += a[i]; //sum2 is used to store prefix sum. long sum2 = 0; int ans = -1; for (int i = 0; i < n; i++) { //Leaving out the value of current element from suffix sum. sum = sum - a[i]; //Checking if suffix and prefix sums are same. if (sum2 == sum) { //returning the index or equilibrium point. return (i + 1); } //Adding the value of current element to prefix sum. sum2 = sum2 + a[i]; } return -1; } }
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