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[] = {1, 1, 1, 0}
Output: 1 1
Explanation:
For the given array arr[] = {1, 1, 1, 0}
The sum of elements at odd position, Odd_Sum = arr[1] + arr[3] = 1 + 1 = 2.
The sum of elements at even position, Even_Sum = arr[2] + arr[4] = 1 + 0 = 1.
Since Odd_Sum & Even_Sum are not equal, remove some elements to make them equal.
After removing arr[3] and arr[4] the array become arr[] = {1, 1} such that sum of elements at odd indices and even indices are equal.

Input: arr[] = {1, 0}
Output: 0
Explanation:
For the given array arr[] = {1, 0}
The sum of elements at odd position, Odd_Sum = arr[1] = 0 = 0.
The sum of elements at even position, Even_Sum = 1 = 1.
Since Odd_Sum & Even_Sum are not equal, remove some elements to make them equal.
After removing arr[0] the array become arr[] = {0} such that sum of elements at odd indices and even indices are equal.

Approach: The idea is to count the number of 1s and 0s in the given array and then make the resultant sum equal by the following steps: 

  1. Count the number of 0s and 1s in the given array arr[] and store them in variables say count_0 and count_1 respectively.
  2. If the sum of the elements at odd and even indices are equal, then no need to remove any array element. Print the original array as the answer.
  3. If count_0 ≥ N/2, then remove all 1s and print all the zeros count_0 number of times.
  4. Otherwise, if count_0 < N/2, by removing all the 0s, the sum of even and odd indices can be made the same as per the following conditions: 
    • If count_1 is odd, then print 1 as an element of the new array (count_1 – 1) number of times.
    • Otherwise, print 1 as an element of the new array count_1 number of times.
  5. Print the resultant array as per the above conditions.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to modify array to make
// sum of odd and even indexed
// elements equal
void makeArraySumEqual(int a[], int N)
{
    // Stores the count of 0s, 1s
    int count_0 = 0, count_1 = 0;
 
    // Stores sum of odd and even
    // indexed elements respectively
    int odd_sum = 0, even_sum = 0;
 
    for (int i = 0; i < N; i++) {
 
        // Count 0s
        if (a[i] == 0)
            count_0++;
 
        // Count 1s
        else
            count_1++;
 
        // Calculate odd_sum and even_sum
        if ((i + 1) % 2 == 0)
            even_sum += a[i];
 
        else if ((i + 1) % 2 > 0)
            odd_sum += a[i];
    }
 
    // If both are equal
    if (odd_sum == even_sum) {
 
        // Print the original array
        for (int i = 0; i < N; i++)
            printf("%d ", a[i]);
    }
 
    // Otherwise
    else {
 
        if (count_0 >= N / 2) {
 
            // Print all the 0s
            for (int i = 0; i < count_0; i++)
                printf("0 ");
        }
        else {
 
            // For checking even or odd
            int is_Odd = count_1 % 2;
 
            // Update total count of 1s
            count_1 -= is_Odd;
 
            // Print all 1s
            for (int i = 0; i < count_1; i++)
                printf("1 ");
        }
    }
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 1, 1, 0 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    makeArraySumEqual(arr, N);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
 
class GFG {
 
    // Function to modify array to make
    // sum of odd and even indexed
    // elements equal
    static void makeArraySumEqual(int a[], int N)
    {
        // Stores the count of 0s, 1s
        int count_0 = 0, count_1 = 0;
 
        // Stores sum of odd and even
        // indexed elements respectively
        int odd_sum = 0, even_sum = 0;
 
        for (int i = 0; i < N; i++) {
 
            // Count 0s
            if (a[i] == 0)
                count_0++;
 
            // Count 1s
            else
                count_1++;
 
            // Calculate odd_sum and even_sum
            if ((i + 1) % 2 == 0)
                even_sum += a[i];
 
            else if ((i + 1) % 2 > 0)
                odd_sum += a[i];
        }
 
        // If both are equal
        if (odd_sum == even_sum) {
 
            // Print the original array
            for (int i = 0; i < N; i++)
                System.out.print(a[i] + " ");
        }
 
        else
        {
            if (count_0 >= N / 2) {
 
                // Print all the 0s
                for (int i = 0; i < count_0; i++)
                    System.out.print("0 ");
            }
            else {
 
                // For checking even or odd
                int is_Odd = count_1 % 2;
 
                // Update total count of 1s
                count_1 -= is_Odd;
 
                // Print all 1s
                for (int i = 0; i < count_1; i++)
                    System.out.print("1 ");
            }
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Given array arr[]
        int arr[] = { 1, 1, 1, 0 };
 
        int N = arr.length;
 
        // Function Call
        makeArraySumEqual(arr, N);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
  
# Function to modify array to make
# sum of odd and even indexed
# elements equal
def makeArraySumEqual(a, N):
     
    # Stores the count of 0s, 1s
    count_0 = 0
    count_1 = 0
  
    # Stores sum of odd and even
    # indexed elements respectively
    odd_sum = 0
    even_sum = 0
  
    for i in range(N):
  
        # Count 0s
        if (a[i] == 0):
            count_0 += 1
  
        # Count 1s
        else:
            count_1 += 1
  
        # Calculate odd_sum and even_sum
        if ((i + 1) % 2 == 0):
            even_sum += a[i]
  
        elif ((i + 1) % 2 > 0):
            odd_sum += a[i]
     
    # If both are equal
    if (odd_sum == even_sum):
  
        # Print the original array
        for i in range(N):
            print(a[i], end = " ")
     
    # Otherwise
    else:
        if (count_0 >= N / 2):
  
            # Print all the 0s
            for i in range(count_0):
                print("0", end = " ")
         
        else:
  
            # For checking even or odd
            is_Odd = count_1 % 2
  
            # Update total count of 1s
            count_1 -= is_Odd
  
            # Print all 1s
            for i in range(count_1):
                print("1", end = " ")
         
# Driver Code
 
# Given array arr[]
arr = [ 1, 1, 1, 0 ]
  
N = len(arr)
  
# Function call
makeArraySumEqual(arr, N)
 
# This code is contributed by code_hunt

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for
// the above approach
using System;
class GFG{
 
// Function to modify array to make
// sum of odd and even indexed
// elements equal
static void makeArraySumEqual(int []a,
                              int N)
{
  // Stores the count of 0s, 1s
  int count_0 = 0, count_1 = 0;
 
  // Stores sum of odd and even
  // indexed elements respectively
  int odd_sum = 0, even_sum = 0;
 
  for (int i = 0; i < N; i++)
  {
    // Count 0s
    if (a[i] == 0)
      count_0++;
 
    // Count 1s
    else
      count_1++;
 
    // Calculate odd_sum and even_sum
    if ((i + 1) % 2 == 0)
      even_sum += a[i];
 
    else if ((i + 1) % 2 > 0)
      odd_sum += a[i];
  }
 
  // If both are equal
  if (odd_sum == even_sum)
  {
    // Print the original array
    for (int i = 0; i < N; i++)
      Console.Write(a[i] + " ");
  }
  else
  {
    if (count_0 >= N / 2)
    {
      // Print all the 0s
      for (int i = 0; i < count_0; i++)
        Console.Write("0 ");
    }
    else
    {
      // For checking even or odd
      int is_Odd = count_1 % 2;
 
      // Update total count of 1s
      count_1 -= is_Odd;
 
      // Print all 1s
      for (int i = 0; i < count_1; i++)
        Console.Write("1 ");
    }
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given array []arr
  int []arr = {1, 1, 1, 0};
 
  int N = arr.Length;
 
  // Function Call
  makeArraySumEqual(arr, N);
}
}
 
// This code is contributed by 29AjayKumar

chevron_right


Output: 

1 1







 

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

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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

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.