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:
- Count the number of 0s and 1s in the given array arr[] and store them in variables say count_0 and count_1 respectively.
- 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.
- If count_0 ? N/2, then remove all 1s and print all the zeros count_0 number of times.
- 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.
- Print the resultant array as per the above conditions.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void makeArraySumEqual( int a[], int N)
{
int count_0 = 0, count_1 = 0;
int odd_sum = 0, even_sum = 0;
for ( int i = 0; i < N; i++) {
if (a[i] == 0)
count_0++;
else
count_1++;
if ((i + 1) % 2 == 0)
even_sum += a[i];
else if ((i + 1) % 2 > 0)
odd_sum += a[i];
}
if (odd_sum == even_sum) {
for ( int i = 0; i < N; i++)
cout << " " << a[i];
}
else {
if (count_0 >= N / 2) {
for ( int i = 0; i < count_0; i++)
cout << "0 " ;
}
else {
int is_Odd = count_1 % 2;
count_1 -= is_Odd;
for ( int i = 0; i < count_1; i++)
cout << "1 " ;
}
}
}
int main()
{
int arr[] = { 1, 1, 1, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
makeArraySumEqual(arr, N);
return 0;
}
|
C
#include <stdio.h>
void makeArraySumEqual( int a[], int N)
{
int count_0 = 0, count_1 = 0;
int odd_sum = 0, even_sum = 0;
for ( int i = 0; i < N; i++) {
if (a[i] == 0)
count_0++;
else
count_1++;
if ((i + 1) % 2 == 0)
even_sum += a[i];
else if ((i + 1) % 2 > 0)
odd_sum += a[i];
}
if (odd_sum == even_sum) {
for ( int i = 0; i < N; i++)
printf ( "%d " , a[i]);
}
else {
if (count_0 >= N / 2) {
for ( int i = 0; i < count_0; i++)
printf ( "0 " );
}
else {
int is_Odd = count_1 % 2;
count_1 -= is_Odd;
for ( int i = 0; i < count_1; i++)
printf ( "1 " );
}
}
}
int main()
{
int arr[] = { 1, 1, 1, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
makeArraySumEqual(arr, N);
return 0;
}
|
Java
class GFG {
static void makeArraySumEqual( int a[], int N)
{
int count_0 = 0 , count_1 = 0 ;
int odd_sum = 0 , even_sum = 0 ;
for ( int i = 0 ; i < N; i++) {
if (a[i] == 0 )
count_0++;
else
count_1++;
if ((i + 1 ) % 2 == 0 )
even_sum += a[i];
else if ((i + 1 ) % 2 > 0 )
odd_sum += a[i];
}
if (odd_sum == even_sum) {
for ( int i = 0 ; i < N; i++)
System.out.print(a[i] + " " );
}
else
{
if (count_0 >= N / 2 ) {
for ( int i = 0 ; i < count_0; i++)
System.out.print( "0 " );
}
else {
int is_Odd = count_1 % 2 ;
count_1 -= is_Odd;
for ( int i = 0 ; i < count_1; i++)
System.out.print( "1 " );
}
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 1 , 1 , 0 };
int N = arr.length;
makeArraySumEqual(arr, N);
}
}
|
Python3
def makeArraySumEqual(a, N):
count_0 = 0
count_1 = 0
odd_sum = 0
even_sum = 0
for i in range (N):
if (a[i] = = 0 ):
count_0 + = 1
else :
count_1 + = 1
if ((i + 1 ) % 2 = = 0 ):
even_sum + = a[i]
elif ((i + 1 ) % 2 > 0 ):
odd_sum + = a[i]
if (odd_sum = = even_sum):
for i in range (N):
print (a[i], end = " " )
else :
if (count_0 > = N / 2 ):
for i in range (count_0):
print ( "0" , end = " " )
else :
is_Odd = count_1 % 2
count_1 - = is_Odd
for i in range (count_1):
print ( "1" , end = " " )
arr = [ 1 , 1 , 1 , 0 ]
N = len (arr)
makeArraySumEqual(arr, N)
|
C#
using System;
class GFG{
static void makeArraySumEqual( int []a,
int N)
{
int count_0 = 0, count_1 = 0;
int odd_sum = 0, even_sum = 0;
for ( int i = 0; i < N; i++)
{
if (a[i] == 0)
count_0++;
else
count_1++;
if ((i + 1) % 2 == 0)
even_sum += a[i];
else if ((i + 1) % 2 > 0)
odd_sum += a[i];
}
if (odd_sum == even_sum)
{
for ( int i = 0; i < N; i++)
Console.Write(a[i] + " " );
}
else
{
if (count_0 >= N / 2)
{
for ( int i = 0; i < count_0; i++)
Console.Write( "0 " );
}
else
{
int is_Odd = count_1 % 2;
count_1 -= is_Odd;
for ( int i = 0; i < count_1; i++)
Console.Write( "1 " );
}
}
}
public static void Main(String[] args)
{
int []arr = {1, 1, 1, 0};
int N = arr.Length;
makeArraySumEqual(arr, N);
}
}
|
Javascript
<script>
function makeArraySumEqual(a, N)
{
let count_0 = 0, count_1 = 0;
let odd_sum = 0, even_sum = 0;
for (let i = 0; i < N; i++) {
if (a[i] == 0)
count_0++;
else
count_1++;
if ((i + 1) % 2 == 0)
even_sum += a[i];
else if ((i + 1) % 2 > 0)
odd_sum += a[i];
}
if (odd_sum == even_sum) {
for (let i = 0; i < N; i++)
document.write(a[i] + " " );
}
else
{
if (count_0 >= N / 2) {
for (let i = 0; i < count_0; i++)
document.write( "0 " );
}
else {
let is_Odd = count_1 % 2;
count_1 -= is_Odd;
for (let i = 0; i < count_1; i++)
document.write( "1 " );
}
}
}
let arr = [ 1, 1, 1, 0 ];
let N = arr.length;
makeArraySumEqual(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
10 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...