Asked In Amazon, Ola Cabs Key benefits after reading this Blog Have you ever thought of any such service that could make our life easier by allowing…
Easy
Amazon, Adobe, Hike
This is a good coding interview problem to learn — how to optimize space complexity and solve the problem using a single scan. We can find a lot of similar problems asked during the coding interview.
Write a program to find the equilibrium index of an array. The equilibrium index of an array is an index such that sum of elements at lower indexes equal to the sum of elements at higher indexes.
In other words, the equilibrium index of an array is an index i such that the sum of elements at indices less than i is equal to the sum of elements at indices greater than i. Element at index i is not included in either part.
A[0] + A[1]+….. + A[i-1] = A[i+1] + ….. + A[n-1], Where 0 < i < n-1
Values in the input array can be repeated.
Example 1
Input: A[] = [-7, 1, 5, 2, -4, 3, 0]
Output: 3
Explanation: 3 is an equilibrium index
A[0] + A[1] + A[2] = A[4] + A[5] + A[6]
Example 2
Input: A[] = [0, 1, 3, -2, -1]
Output: 1
Explanation: 1 is an equilibrium index,
A[0] = A[2] + A[3] + A[4]
A straightforward method is to use two loops. The idea is to find the sum of elements for every range of indexes and observe if there exists an equilibrium index. The outer loop traverses the array, and the inner loop determines if there is an equilibrium index or not.
We are running nested loops and calculating leftsum and rightsum for every value of i. Total number of summation operation = n(n-1) (Think!)
Time Complexity = O(n²), Space Complexity: O(1)
The idea is to store the prefix sum of the array. The prefix sum would help keep track of the sum of all elements up to any index of the array. Now, we should find a way to keep track of the sum of values to the right of the current index. We can use a temporary sum variable, which initially stores the sum of all elements of the array. If we subtract the value at the current index, we will get the sum of values to the right. Now, compare both leftSum and rightSum to find whether the current index is the equilibrium index or not.
We are running two separate loops for calculating the prefix sum array and equilibrium index respectively. Time Complexity = Time complexity of calculating prefix sum array + Time complexity of calculating Equilibrium Index = O(n) + O(n) = O(n)
Space Complexity: The above algorithm uses extra space to create a prefix sum array. Hence, the space complexity would be O(n).
The motive is to get the total sum of the array first. The difference between the above method and this method is that we don’t need to store the prefix sum beforehand. Instead, we will keep track of the leftSum while traversing the array itself. In the loop, we can get the rightSum by subtracting the elements one by one.
We are running two separate loops for calculating the total sum and equilibrium index respectively. Time complexity = Time complexity of calculating prefix sum array + Time complexity of calculating Equilibrium Index = O(n) + O(n) = O(n)
Space Complexity = O(1), because we are just using variables to store the value of total, left, and right sum.
Asked In Amazon, Ola Cabs Key benefits after reading this Blog Have you ever thought of any such service that could make our life easier by allowing…
Algorithms have a long history, and the word can be traced back to the 9th century. At this time, the Persian mathematician Abdullah Muhammad bin Musa…
What is Competitive Programming? Competitive Programming is a programming sport involving many participants competing with each other to achieve…
Subscribe to get free weekly content on DSA, Machine Learning and System Design. Content will be delivered every Monday.