Equilibrium index of an array - GeeksforGeeks
The idea is to get total sum of array first. Then Iterate through the array and keep updating the left sum which is initialized as zero. In the loop, we can get right sum by subtracting the elements one by one.
O(N^2)
http://www.cnblogs.com/EdwardLiu/p/4199081.html
Read full article from Equilibrium index of an array - 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. For example, in an arrya A:
A[0] = -7, A[1] = 1, A[2] = 5, A[3] = 2, A[4] = -4, A[5] = 3, A[6]=0
3 is an equilibrium index, because:
A[0] + A[1] + A[2] = A[4] + A[5] + A[6]
A[0] + A[1] + A[2] = A[4] + A[5] + A[6]
6 is also an equilibrium index, because sum of zero elements is zero, i.e., A[0] + A[1] + A[2] + A[3] + A[4] + A[5]=0
7 is not an equilibrium index, because it is not a valid index of array A.
Write a function int equilibrium(int[] arr, int n); that given a sequence arr[] of size n, returns an equilibrium index (if any) or -1 if no equilibrium indexes exist.
O(N):The idea is to get total sum of array first. Then Iterate through the array and keep updating the left sum which is initialized as zero. In the loop, we can get right sum by subtracting the elements one by one.
int
equilibrium(
int
arr[],
int
n)
{
int
sum = 0;
// initialize sum of whole array
int
leftsum = 0;
// initialize leftsum
int
i;
/* Find sum of the whole array */
for
(i = 0; i < n; ++i)
sum += arr[i];
for
( i = 0; i < n; ++i)
{
sum -= arr[i];
// sum is now right sum for index i
if
(leftsum == sum)
return
i;
leftsum += arr[i];
}
/* If no equilibrium index found, then return 0 */
return
-1;
}
int
equilibrium(
int
arr[],
int
n)
{
int
i, j;
int
leftsum, rightsum;
/* Check for indexes one by one until an equilibrium
index is found */
for
( i = 0; i < n; ++i)
{
leftsum = 0;
// initialize left sum for current index i
rightsum = 0;
// initialize right sum for current index i
/* get left sum */
for
( j = 0; j < i; j++)
leftsum += arr[j];
/* get right sum */
for
( j = i+1; j < n; j++)
rightsum += arr[j];
/* if leftsum and rightsum are same, then we are done */
if
(leftsum == rightsum)
return
i;
}
/* return -1 if no equilibrium index is found */
return
-1;
}
Method 1 (Simple but inefficient)
Use two loops. Outer loop iterates through all the element and inner loop finds out whether the current index picked by the outer loop is equilibrium index or not. Time complexity of this solution is O(n^2).
Use two loops. Outer loop iterates through all the element and inner loop finds out whether the current index picked by the outer loop is equilibrium index or not. Time complexity of this solution is O(n^2).
int
equilibrium(
int
arr[],
int
n)
{
int
i, j;
int
leftsum, rightsum;
/* Check for indexes one by one until an equilibrium
index is found */
for
( i = 0; i < n; ++i)
{
leftsum = 0;
// initialize left sum for current index i
rightsum = 0;
// initialize right sum for current index i
/* get left sum */
for
( j = 0; j < i; j++)
leftsum += arr[j];
/* get right sum */
for
( j = i+1; j < n; j++)
rightsum += arr[j];
/* if leftsum and rightsum are same, then we are done */
if
(leftsum == rightsum)
return
i;
}
/* return -1 if no equilibrium index is found */
return
-1;
}
Read full article from Equilibrium index of an array - GeeksforGeeks