Minimum elements to be removed such that sum of adjacent elements is always odd
Given an array of N integers. The task is to eliminate the minimum number of elements such that in the resulting array the sum of any two adjacent values is odd.
Examples:
Input: arr[] = {1, 2, 3}
Output: 0
Sum of all adjacent elements is already odd.
Input: arr[] = {1, 3, 5, 4, 2}
Output: 3
Eliminate 3, 5 and 2 so that in the resulting array the sum of any two adjacent values is odd.
Approach The sum of 2 numbers is odd if one of them is odd and the other is even. This means for every pair of consecutive numbers that have the same parity, eliminate one of them, it doesn’t matter which. So the following greedy algorithm works:
- Go through all the elements in order.
- If the current number has the same parity as the previous one eliminate it, otherwise don’t.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int min_elimination( int n, int arr [])
{
int count = 0;
int prev_val = arr[0];
for ( int i = 1; i < n; i++)
{
int curr_val = arr[i];
if (curr_val % 2 == prev_val % 2)
count++;
prev_val = curr_val;
}
return count;
}
int main()
{
int arr [] = { 1, 2, 3, 7, 9 };
int n = sizeof (arr)/ sizeof (arr[0]);
cout << min_elimination(n, arr);
return 0;
}
|
Java
class GFG {
static int min_elimination( int n, int [] arr)
{
int count = 0 ;
int prev_val = arr[ 0 ];
for ( int i = 1 ; i < n; i++) {
int curr_val = arr[i];
if (curr_val % 2 == prev_val % 2 )
count++;
prev_val = curr_val;
}
return count;
}
public static void main(String[] args)
{
int [] arr = new int [] { 1 , 2 , 3 , 7 , 9 };
int n = arr.length;
System.out.println(min_elimination(n, arr));
}
}
|
Python3
def min_elimination(n, arr):
count = 0
prev_val = arr[ 0 ]
for i in range ( 1 , n):
curr_val = arr[i];
if (curr_val % 2 = = prev_val % 2 ):
count = count + 1
prev_val = curr_val
return count
arr = [ 1 , 2 , 3 , 7 , 9 ]
n = len (arr)
print (min_elimination(n, arr));
|
C#
using System;
class GFG
{
static int min_elimination( int n, int [] arr)
{
int count = 0;
int prev_val = arr[0];
for ( int i = 1; i < n; i++)
{
int curr_val = arr[i];
if (curr_val % 2 == prev_val % 2)
count++;
prev_val = curr_val;
}
return count;
}
public static void Main()
{
int [] arr = new int [] { 1, 2, 3, 7, 9 };
int n = arr.Length;
Console.WriteLine(min_elimination(n, arr));
}
}
|
PHP
<?php
function min_elimination( $n , $arr )
{
$count = 0;
$prev_val = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
{
$curr_val = $arr [ $i ];
if ( $curr_val % 2 == $prev_val % 2)
$count ++;
$prev_val = $curr_val ;
}
return $count ;
}
$arr = array ( 1, 2, 3, 7, 9 );
$n = sizeof( $arr );
echo min_elimination( $n , $arr );
?>
|
Javascript
<script>
function min_elimination(n,arr)
{
let count = 0;
let prev_val = arr[0];
for (let i = 1; i < n; i++)
{
let curr_val = arr[i];
if (curr_val % 2 == prev_val % 2)
count++;
prev_val = curr_val;
}
return count;
}
let arr = [1, 2, 3, 7, 9];
let n = arr.length;
document.write(min_elimination(n, arr));
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Last Updated :
16 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...