Make all the elements of array even with given operations

Given an array arr[] of positive integers, find the minimum number of operations required to make all the array elements even where:

  1. If there is an odd number then, increment the element and the next adjacent element by 1.
  2. Each increment costs one operations.

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. Following are the steps:

  1. Traverse the given array arr[].
  2. If an odd element occurs then increment that element by 1 to make it even and the next adjacent element by 1.
  3. Repeat the above step for all the odd element for the given array arr[].
  4. If all the element in arr[] are even then print the number of operation.
  5. Else print -1.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to make all array
// element even
#include "bits/stdc++.h"
using namespace std;
  
// Function to count the total
// number of operations needed to make
// all array element even
int countOperations(int arr[], int n)
{
    int count = 0;
  
    // Traverse the given array
    for (int i = 0; i < n - 1; i++) {
  
        // If an odd element occurs
        // then increment that element
        // and next adjacent element
        // by 1
        if (arr[i] & 1) {
            arr[i]++;
            arr[i + 1]++;
            count += 2;
        }
    }
  
    // Traverse the array if any odd
    // element occurs then return -1
    for (int i = 0; i < n; i++) {
        if (arr[i] & 1)
            return -1;
    }
  
    // Returns the count of operations
    return count;
}
  
int main()
{
    int arr[] = { 2, 3, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(int);
    cout << countOperations(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to make all array
// element even
class GFG
{
  
// Function to count the total
// number of operations needed to make
// all array element even
static int countOperations(int arr[], int n)
{
    int count = 0;
  
    // Traverse the given array
    for (int i = 0; i < n - 1; i++)
    {
  
        // If an odd element occurs
        // then increment that element
        // and next adjacent element
        // by 1
        if (arr[i] % 2 == 1
        {
            arr[i]++;
            arr[i + 1]++;
            count += 2;
        }
    }
  
    // Traverse the array if any odd
    // element occurs then return -1
    for (int i = 0; i < n; i++)
    {
        if (arr[i] % 2 == 1)
            return -1;
    }
  
    // Returns the count of operations
    return count;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 2, 3, 4, 5, 6 };
    int n = arr.length;
    System.out.print(countOperations(arr, n));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to make all array 
# element even 
  
# Function to count the total 
# number of operations needed to make 
# all array element even 
def countOperations(arr, n) :
  
    count = 0
  
    # Traverse the given array 
    for i in range(n - 1) :
  
        # If an odd element occurs 
        # then increment that element 
        # and next adjacent element 
        # by 1 
        if (arr[i] & 1) :
            arr[i] += 1
            arr[i + 1] += 1
            count += 2
  
    # Traverse the array if any odd 
    # element occurs then return -1 
    for i in range(n) :
        if (arr[i] & 1) :
            return -1
  
    # Returns the count of operations 
    return count; 
  
if __name__ == "__main__"
  
    arr = [ 2, 3, 4, 5, 6 ]; 
    n = len(arr); 
    print(countOperations(arr, n)); 
      
    # This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to make all array
// element even
using System;
  
class GFG
{
  
// Function to count the total
// number of operations needed to make
// all array element even
static int countOperations(int []arr, int n)
{
    int count = 0;
  
    // Traverse the given array
    for (int i = 0; i < n - 1; i++)
    {
  
        // If an odd element occurs
        // then increment that element
        // and next adjacent element
        // by 1
        if (arr[i] % 2 == 1) 
        {
            arr[i]++;
            arr[i + 1]++;
            count += 2;
        }
    }
  
    // Traverse the array if any odd
    // element occurs then return -1
    for (int i = 0; i < n; i++)
    {
        if (arr[i] % 2 == 1)
            return -1;
    }
  
    // Returns the count of operations
    return count;
}
  
// Driver code
public static void Main()
{
    int []arr = { 2, 3, 4, 5, 6 };
    int n = arr.Length;
    Console.Write(countOperations(arr, n));
}
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

4

Time Complexity: O(N) where N is the number of elements in array.

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

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.



Improved By : 29AjayKumar, AnkitRai01

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.