Minimum array size after repeated replacement of even sum pair with sum
Given an array of size N. Choose a random pair of elements from the sequence such that their sum is even, delete those two elements from the sequence and insert their sum into the sequence instead in order to minimize the length of the array. Finally, print the minimum possible size of the array.
Examples :
Input : 88 98 1 7 3
Output : 2
By following above rules --[88, 98, 1, 10]
[98, 88, 1]---[186, 1]--we cannot move further since
186 + 1 = 187, which is not even. So, size = 2.
Input : 7 4 3 2 6
Output : 1
delete 7 and 3, insert 10---[10, 4, 2, 6]
repeating the process of deleting and inserting---[14, 8]--[22]
size of array becomes 1.
Approach: A pair of numbers can sum upto an even number if both the numbers are even or both of them are odd. So, we just need to count the odd numbers present in the given array. Answer can either be 2 or 1 (and nothing else), depending upon the condition. If total number of odds in the array is an odd number then print 2 otherwise print 1.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool check( int a[], int n)
{
int i, c = 0;
if (n == 1)
return false ;
for (i = 0; i < n; i++)
if (a[i] & 1)
c++;
if (c & 1)
return true ;
return false ;
}
int main()
{
int n, a[] = { 7, 4, 3, 2, 6 };
n = sizeof (a) / sizeof (a[0]);
if (check(a, n))
cout << 2;
else
cout << 1;
return 0;
}
|
Java
import java.io.*;
import java.math.*;
class GFG {
static boolean check( int a[], int n)
{
int i, c = 0 ;
if (n == 1 )
return false ;
for (i = 0 ; i < n; i++)
if ((a[i] & 1 )== 1 )
c++;
if ((c & 1 ) == 1 )
return true ;
return false ;
}
public static void main(String args[])
{
int n, a[] = { 7 , 4 , 3 , 2 , 6 };
n = a.length;
if (check(a, n))
System.out.println( "2" );
else
System.out.println( "1" );
}
}
|
Python 3
def check(a):
c = 0
if len (a) = = 1 :
return False
for i in a:
if i & 1 :
c + = 1
if c & 1 :
return True
return False
a = [ 7 , 4 , 3 , 2 , 6 ]
if (check(a)):
print ( 2 )
else :
print ( 1 )
|
C#
using System;
class GFG {
static bool check( int []a, int n)
{
int i, c = 0;
if (n == 1)
return false ;
for (i = 0; i < n; i++)
if ((a[i] & 1)==1)
c++;
if ((c & 1) == 1)
return true ;
return false ;
}
public static void Main()
{
int n;
int []a = { 7, 4, 3, 2, 6 };
n = a.Length;
if (check(a, n))
Console.WriteLine( "2" );
else
Console.WriteLine( "1" );
}
}
|
PHP
<?php
function check( $a , $n )
{
$i ; $c = 0;
if ( $n == 1)
return false;
for ( $i = 0; $i < $n ; $i ++)
if ( $a [ $i ] & 1)
$c ++;
if ( $c & 1)
return true;
return false;
}
$n ; $a = array ( 7, 4, 3, 2, 6 );
$n = count ( $a );
if (check( $a , $n ))
echo 2;
else
echo 1;
?>
|
Javascript
<script>
function check(a, n)
{
let i, c = 0;
if (n == 1)
return false ;
for (i = 0; i < n; i++)
if ((a[i] & 1)==1)
c++;
if ((c & 1) == 1)
return true ;
return false ;
}
let n;
let a = [7, 4, 3, 2, 6];
n = a.length;
if (check(a, n))
document.write( "2" + "</br>" );
else
document.write( "1" );
</script>
|
Time Complexity: O(n)
Space Complexity: O(1).
Last Updated :
02 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...