Given an array arr[] of size N, the task is to find indices of array elements whose removal makes the sum of odd and even indexed elements equal. If no such element exists, then print -1.
Examples:
Input: arr[] = {4, 1, 6, 2}
Output: 1
Explanation:
Removing arr[1] modifies arr[] to { 4, 6, 2 }
Sum of even-indexed array elements = arr[0] + arr[2] = 4 + 2 = 6
Sum of odd-indexed array elements = arr[1] = 6
Therefore, the required output is 1.Input:arr = {3, 2, 1}
Output: -1
Naive Approach: The simplest approach to solve this problem to traverse the array and remove the ith element from the array and check if the sum of odd-indexed array elements equal to the sum of even-indexed array elements or not. If found to be true then print the index.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized using Prefix Sum technique. The idea is to use the fact that If an array element removed from the index, then after that index, the even-indexed array elements become the odd-indexed and vice-versa. Follow the steps below to solve the problem:
- Initialize two arrays, say odd[] and even[], to store the prefix sum of odd and even-indexed array elements and prefix sum of even-indexed array elements respectively.
- Traverse the array arr[] and compute prefix sum of odd and even-indexed array elements.
- Initialize two variables, say P = 0 and Q = 0, to store the sum of even and odd-indexed array elements respectively after removal of an array element.
- Traverse the array and remove array elements one by one and update prefix sums accordingly. Check if sum of even-indexed array elements, P, is equal to sum of odd-indexed array elements, Q or not. If found to be true, then print the current index.
- Otherwise, print -1.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find indices of array elements // whose removal makes the sum of odd and // even indexed array elements equal void removeIndicesToMakeSumEqual(vector<int>& arr) { // Stores size of array int N = arr.size(); // Store prefix sum of odd // index array elements vector<int> odd(N, 0); // Store prefix sum of even // index array elements vector<int> even(N, 0); // Update even[0] even[0] = arr[0]; // Traverse the given array for (int i = 1; i < N; i++) { // Update odd[i] odd[i] = odd[i - 1]; // Update even[i] even[i] = even[i - 1]; // If the current index // is an even number if (i % 2 == 0) { // Update even[i] even[i] += arr[i]; } // If the current index // is an odd number else { // Update odd[i] odd[i] += arr[i]; } } // Check if at least one // index found or not that // satisfies the condition bool find = 0; // Store odd indices sum by // removing 0-th index int p = odd[N - 1]; // Store even indices sum by // removing 0-th index int q = even[N - 1] - arr[0]; // If p and q are equal if (p == q) { cout << "0 "; find = 1; } // Traverse the array arr[] for (int i = 1; i < N; i++) { // If i is an even number if (i % 2 == 0) { // Update p by removing // the i-th element p = even[N - 1] - even[i - 1] - arr[i] + odd[i - 1]; // Update q by removing // the i-th element q = odd[N - 1] - odd[i - 1] + even[i - 1]; } else { // Update q by removing // the i-th element q = odd[N - 1] - odd[i - 1] - arr[i] + even[i - 1]; // Update p by removing // the i-th element p = even[N - 1] - even[i - 1] + odd[i - 1]; } // If odd index values sum is equal // to even index values sum if (p == q) { // Set the find variable find = 1; // Print the current index cout << i << " "; } } // If no index found if (!find) { // Print not possible cout << -1; } } // Driver Code int main() { vector<int> arr = { 4, 1, 6, 2 }; removeIndicesToMakeSumEqual(arr); return 0; } |
1
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Split N into two integers whose addition to A and B makes them equal
- Find the element whose multiplication with -1 makes array sum 0
- Maximum removal from array when removal time >= waiting time
- Derive a MultiSet from given Array such that sum is > P and removing any element makes sum < P
- Minimum increments to make all array elements equal with sum same as the given array after exactly one removal
- Longest increasing subsequence consisting of elements from indices divisible by previously selected indices
- Find a pair of elements swapping which makes sum of two arrays same
- Replacing an element makes array elements consecutive
- Print the lexicographically smallest array by swapping elements whose sum is odd
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Sum of all the Composite Numbers from Odd indices of the given array
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Largest subset having with sum less than equal to sum of respective indices
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Node whose removal minimizes the maximum size forest from an N-ary Tree
- Maximize difference between the Sum of the two halves of the Array after removal of N elements
- Minimum flips of odd indexed elements from odd length subarrays to make two given arrays equal
- Count the pairs in an array such that the difference between them and their indices is equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

