Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Count ways to make Bitwise XOR of odd and even indexed elements equal by removing an array element
Next article icon

Print indices of array elements whose removal makes the sum of odd and even-indexed elements equal

Last Updated : 03 Jun, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 the 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 the sum of even-indexed array elements, P, is equal to the 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;
}
 
 

Java




// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find indices of array elements
// whose removal makes the sum of odd and
// even indexed array elements equal
static void removeIndicesToMakeSumEqual(int []arr)
{
     
    // Stores size of array
    int N = arr.length;
 
    // Store prefix sum of odd
    // index array elements
    int []odd = new int[N];
 
    // Store prefix sum of even
    // index array elements
    int []even = new int[N];
 
    // 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
    boolean find = false;
 
    // 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)
    {
        System.out.print("0 ");
        find = true;
    }
 
    // 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 = true;
 
            // Print the current index
            System.out.print(i + " ");
        }
    }
 
    // If no index found
    if (!find)
    {
         
        // Print not possible
        System.out.print(-1);
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 4, 1, 6, 2 };
     
    removeIndicesToMakeSumEqual(arr);
}
}
 
// This code is contributed by Amit Katiyar
 
 

Python3




# Python program to implement
# the above approach
 
# Function to find indices of array elements
# whose removal makes the sum of odd and
# even indexed array elements equal
def removeIndicesToMakeSumEqual(arr):
   
    # Stores size of array
    N = len(arr);
 
    # Store prefix sum of odd
    # index array elements
    odd = [0] * N;
 
    # Store prefix sum of even
    # index array elements
    even = [0] * N;
 
    # Update even[0]
    even[0] = arr[0];
 
    # Traverse the given array
    for i in range(1, N):
 
        # 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
    find = False;
 
    # Store odd indices sum by
    # removing 0-th index
    p = odd[N - 1];
 
    # Store even indices sum by
    # removing 0-th index
    q = even[N - 1] - arr[0];
 
    # If p and q are equal
    if (p == q):
        print("0 ");
        find = True;
 
    # Traverse the array arr
    for i in range(1, N):
 
        # 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 = True;
 
            # Print the current index
            print(i, end = "");
 
    # If no index found
    if (find == False):
       
        # Print not possible
        print(-1);
 
# Driver Code
if __name__ == '__main__':
    arr = [4, 1, 6, 2];
 
    removeIndicesToMakeSumEqual(arr);
 
    # This code is contributed by shikhasingrajput
 
 

C#




// C# program to implement
// the above approach
using System;
 
class GFG{
     
// Function to find indices of array elements
// whose removal makes the sum of odd and
// even indexed array elements equal
static void removeIndicesToMakeSumEqual(int []arr)
{
     
    // Stores size of array
    int N = arr.Length;
 
    // Store prefix sum of odd
    // index array elements
    int []odd = new int[N];
 
    // Store prefix sum of even
    // index array elements
    int []even = new int[N];
 
    // 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 = false;
 
    // 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)
    {
        Console.Write("0 ");
        find = true;
    }
 
    // 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 = true;
 
            // Print the current index
            Console.Write(i + " ");
        }
    }
 
    // If no index found
    if (!find)
    {
         
        // Print not possible
        Console.Write(-1);
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int[] arr = { 4, 1, 6, 2 };
     
    removeIndicesToMakeSumEqual(arr);
}
}
 
// This code is contributed by AnkThon
 
 

Javascript




<script>
// javascript program to implement
// the above approach
 
    // Function to find indices of array elements
    // whose removal makes the sum of odd and
    // even indexed array elements equal
    function removeIndicesToMakeSumEqual(arr) {
 
        // Stores size of array
        var N = arr.length;
 
        // Store prefix sum of odd
        // index array elements
        var odd = Array(N).fill(0);
 
        // Store prefix sum of even
        // index array elements
        var even = Array(N).fill(0);
 
        // Update even[0]
        even[0] = arr[0];
 
        // Traverse the given array
        for (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
        var find = false;
 
        // Store odd indices sum by
        // removing 0-th index
        var p = odd[N - 1];
 
        // Store even indices sum by
        // removing 0-th index
        var q = even[N - 1] - arr[0];
 
        // If p and q are equal
        if (p == q) {
            document.write("0 ");
            find = true;
        }
 
        // Traverse the array arr
        for (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 = true;
 
                // Print the current index
                document.write(i + " ");
            }
        }
 
        // If no index found
        if (!find) {
 
            // Print not possible
            document.write(-1);
        }
    }
 
    // Driver Code
     
        var arr = [ 4, 1, 6, 2 ];
 
        removeIndicesToMakeSumEqual(arr);
 
// This code is contributed by Rajput-Ji
</script>
 
 
Output: 
1

 

Time Complexity: O(N)
Auxiliary Space: O(N)



Next Article
Count ways to make Bitwise XOR of odd and even indexed elements equal by removing an array element
author
ajaykr00kj
Improve
Article Tags :
  • Arrays
  • DSA
  • Searching
  • Technical Scripter
  • prefix-sum
  • Technical Scripter 2020
Practice Tags :
  • Arrays
  • prefix-sum
  • Searching

Similar Reads

  • Ways to make sum of odd and even indexed elements equal by removing an array element
    Given an array, arr[] of size n, the task is to find the count of array indices such that removing an element from these indices makes the sum of even-indexed and odd-indexed array elements equal. Examples: Input: arr[] = [ 2, 1, 6, 4 ] Output: 1 Explanation: Removing arr[1] from the array modifies
    10 min read
  • Construct an Array of size N in which sum of odd elements is equal to sum of even elements
    Given an integer N which is always even, the task is to create an array of size N which contains N/2 even numbers and N/2 odd numbers. All the elements of array should be distinct and the sum of even numbers is equal to the sum of odd numbers. If no such array exists then print -1.Examples: Input: N
    7 min read
  • Number of permutations such that sum of elements at odd index and even index are equal
    Given N numbers, find the number of permutations in which the sum of elements at odd index and sum of elements at even index are equal. Examples: Input: 1 2 3 Output: 2 The permutations are: 1 3 2 sum at odd index = 1+2 = 3, sum at even index = 3 2 3 1 sum at odd index = 2+1 = 3, sum at even index =
    8 min read
  • Find the Array Permutation having sum of elements at odd indices greater than sum of elements at even indices
    Given an array arr[] consisting of N integers, the task is to find the permutation of array elements such that the sum of odd indices elements is greater than or equal to the sum of even indices elements. Examples: Input: arr[] = {1, 2, 3, 4}Output: 1 4 2 3 Explanation:Consider the permutation of th
    6 min read
  • Count ways to make Bitwise XOR of odd and even indexed elements equal by removing an array element
    Given an array arr[] of length N, the task is to find the count of array indices such that removing an element from these indices makes the Bitwise xor of odd-indexed elements and even-indexed (1-based indexing) elements are equal. Examples: Input: arr[] = {1, 0, 1, 0, 1}, N = 5Output: 3Explanation:
    10 min read
  • Count possible removals to make absolute difference between the sum of odd and even indexed elements equal to K
    Given an array arr[] consisting of N integers and an integer K, the task is to find the number of times the absolute difference between the sum of elements at odd and even indices is K after removing any one element at a time from the given array. Examples: Input: arr[] = {2, 4, 2}, K = 2Output: 2Ex
    15+ min read
  • Modify given array to make sum of odd and even indexed elements same
    Given a binary array arr[] of size N, remove at most N/2 elements from the array such that the sum of elements at odd and even indices becomes equal. The task is to print the modified array.Note: N is always even. There can be more than one possible result, print any of them. Examples: Input: arr[]
    10 min read
  • Minimize increments required to make count of even and odd array elements equal
    Given an array arr[] of size N, the task is to find the minimum increments by 1 required to be performed on the array elements such that the count of even and odd integers in the given array becomes equal. If it is not possible, then print "-1". Examples: Input: arr[] = {1, 3, 4, 9}Output: 1Explanat
    6 min read
  • Find the last remaining element after repeated removal of odd and even indexed elements alternately
    Given a positive integer N, the task is to print the last remaining element from a sequence [1, N] after repeatedly performing the following operations in the given order alternately: Remove all the odd-indexed elements from the sequence.Remove all the even-indexed elements from the sequence.Example
    12 min read
  • Construct array with sum of product of same indexed elements in the given array equal to zero
    Given an array, arr[] of size N (always even), the task is to construct a new array consisting of N non-zero integers such that the sum of the product of the same indexed elements of arr[] and the new array is equal to 0. If multiple solutions exist, print any one of them. Examples: Input: arr[] = {
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences