Make all the elements of array even with given operations
Last Updated :
11 Oct, 2022
Given an array arr[] of positive integers, find the minimum number of operations required to make all the array elements even where:
- If there is an odd number, then, increment the element and the next adjacent element by 1.
- Each increment costs one operation.
Note: If there is any number in arr[] which is odd after all operations, then, print -1.
Examples:
Input: arr[] = {2, 3, 4, 5, 6}
Output: 4
Explanation:
Add 1 to 3 (at 1st index) and add 1 to its adjacent element 4(2nd index).
Now the array becomes {2, 4, 5, 5, 6}.
Add 1 to 5 (at 2nd index) and add 1 to its adjacent element 5(3rd index).
Now the array becomes {2, 4, 6, 6, 6}.
The resultant array has all even numbers.
The total number of operations for 4 increments is 4.
Input: arr[] = {5, 6}
Output: -1
Explanation:
Adding 1 to 5(0th index), then we have to increment 1 to its adjacent element 6(1st index).
Now the array becomes {6, 7}.
And we have 1 odd number left after all possible increments. Therefore, we can’t make all array elements even.
Approach:
This problem can be solved using Greedy Approach. The following are the steps:
- Traverse the given array arr[].
- If an odd element occurs, then increment that element by 1 to make it even and the next adjacent element by 1.
- Repeat the above step for all the odd elements for the given array arr[].
- If all the elements in arr[] are even, then print the number of operations.
- Else print -1.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
int countOperations( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n - 1; i++) {
if (arr[i] & 1) {
arr[i]++;
arr[i + 1]++;
count += 2;
}
}
for ( int i = 0; i < n; i++) {
if (arr[i] & 1)
return -1;
}
return count;
}
int main()
{
int arr[] = { 2, 3, 4, 5, 6 };
int n = sizeof (arr) / sizeof ( int );
cout << countOperations(arr, n);
return 0;
}
|
Java
class GFG
{
static int countOperations( int arr[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
if (arr[i] % 2 == 1 )
{
arr[i]++;
arr[i + 1 ]++;
count += 2 ;
}
}
for ( int i = 0 ; i < n; i++)
{
if (arr[i] % 2 == 1 )
return - 1 ;
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 4 , 5 , 6 };
int n = arr.length;
System.out.print(countOperations(arr, n));
}
}
|
Python3
def countOperations(arr, n) :
count = 0 ;
for i in range (n - 1 ) :
if (arr[i] & 1 ) :
arr[i] + = 1 ;
arr[i + 1 ] + = 1 ;
count + = 2 ;
for i in range (n) :
if (arr[i] & 1 ) :
return - 1 ;
return count;
if __name__ = = "__main__" :
arr = [ 2 , 3 , 4 , 5 , 6 ];
n = len (arr);
print (countOperations(arr, n));
|
C#
using System;
class GFG
{
static int countOperations( int []arr, int n)
{
int count = 0;
for ( int i = 0; i < n - 1; i++)
{
if (arr[i] % 2 == 1)
{
arr[i]++;
arr[i + 1]++;
count += 2;
}
}
for ( int i = 0; i < n; i++)
{
if (arr[i] % 2 == 1)
return -1;
}
return count;
}
public static void Main()
{
int []arr = { 2, 3, 4, 5, 6 };
int n = arr.Length;
Console.Write(countOperations(arr, n));
}
}
|
Javascript
<script>
function countOperations(arr, n)
{
let count = 0;
for (let i = 0; i < n - 1; i++) {
if (arr[i] & 1) {
arr[i]++;
arr[i + 1]++;
count += 2;
}
}
for (let i = 0; i < n; i++) {
if (arr[i] & 1)
return -1;
}
return count;
}
let arr = [ 2, 3, 4, 5, 6 ];
let n = arr.length;
document.write(countOperations(arr, n));
</script>
|
Time Complexity: O(N) where N is the number of elements in the array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...