Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Make all array elements even by replacing any pair of array elements with their sum

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given an array arr[] consisting of N positive integers, the task is to make all array elements even by replacing any pair of array elements with their sum.

Examples: 

Input: arr[] = {5, 6, 3, 7, 20}
Output: 3
Explanation: 
Operation 1: Replace arr[0] and arr[2] by their sum ( = 5 + 3 = 8) modifies arr[] to {8, 6, 8, 7, 20}.
Operation 2: Replace arr[2] and arr[3] by their sum ( = 7 + 8 = 15) modifies arr[] to {8, 6, 15, 15, 20}.
Operation 3: Replace arr[2] and arr[3] by their sum ( = 15 + 15 = 30) modifies arr[] to {8, 6, 30, 30, 20}.

Input: arr[] = {2, 4, 16, 8, 7, 9, 3, 1}
Output: 2

 

 Approach: The idea is to keep replacing two odd array elements by their sum until all array elements are even. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum number
// of replacements required to make
// all array elements even
void minMoves(int arr[], int N)
{
    // Stores the count of odd elements
    int odd_element_cnt = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Increase count of odd elements
        if (arr[i] % 2 != 0) {
            odd_element_cnt++;
        }
    }
 
    // Store number of replacements required
    int moves = (odd_element_cnt) / 2;
 
    // Two extra moves will be required
    // to make the last odd element even
    if (odd_element_cnt % 2 != 0)
        moves += 2;
 
    // Print the minimum replacements
    cout << moves;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 6, 3, 7, 20 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    minMoves(arr, N);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find the minimum number
// of replacements required to make
// all array elements even
static void minMoves(int arr[], int N)
{
   
    // Stores the count of odd elements
    int odd_element_cnt = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
        // Increase count of odd elements
        if (arr[i] % 2 != 0)
        {
            odd_element_cnt++;
        }
    }
 
    // Store number of replacements required
    int moves = (odd_element_cnt) / 2;
 
    // Two extra moves will be required
    // to make the last odd element even
    if (odd_element_cnt % 2 != 0)
        moves += 2;
 
    // Print the minimum replacements
    System.out.print(moves);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 5, 6, 3, 7, 20 };
    int N = arr.length;
 
    // Function call
    minMoves(arr, N);
}
}
 
// This code is contributed by shikhasingrajput


C#




// C# program for the above approach
using System;
public class GFG
{
 
  // Function to find the minimum number
  // of replacements required to make
  // all array elements even
  static void minMoves(int []arr, int N)
  {
 
    // Stores the count of odd elements
    int odd_element_cnt = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
      // Increase count of odd elements
      if (arr[i] % 2 != 0)
      {
        odd_element_cnt++;
      }
    }
 
    // Store number of replacements required
    int moves = (odd_element_cnt) / 2;
 
    // Two extra moves will be required
    // to make the last odd element even
    if (odd_element_cnt % 2 != 0)
      moves += 2;
 
    // Print the minimum replacements
    Console.Write(moves);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int []arr = { 5, 6, 3, 7, 20 };
    int N = arr.Length;
 
    // Function call
    minMoves(arr, N);
  }
}
 
// This code is contributed by 29AjayKumar


Python3




# Python program for the above approach
 
# Function to find the minimum number
# of replacements required to make
# all array elements even
def minMoves(arr, N):
   
    # Stores the count of odd elements
    odd_element_cnt = 0;
 
    # Traverse the array
    for i in range(N):
 
        # Increase count of odd elements
        if (arr[i] % 2 != 0):
            odd_element_cnt += 1;
 
    # Store number of replacements required
    moves = (odd_element_cnt) // 2;
 
    # Two extra moves will be required
    # to make the last odd element even
    if (odd_element_cnt % 2 != 0):
        moves += 2;
 
    # Print the minimum replacements
    print(moves);
 
# Driver Code
if __name__ == '__main__':
    arr = [5, 6, 3, 7, 20];
    N = len(arr);
 
    # Function call
    minMoves(arr, N);
 
    # This code is contributed by 29AjayKumar


Javascript




<script>
 
// javascript program for the above approach
 
// Function to find the minimum number
// of replacements required to make
// all array elements even
function minMoves(arr, N)
{
    // Stores the count of odd elements
    var odd_element_cnt = 0;
     
    var i;
    // Traverse the array
    for(i = 0; i < N; i++) {
 
        // Increase count of odd elements
        if (arr[i] % 2 != 0) {
            odd_element_cnt++;
        }
    }
 
    // Store number of replacements required
    var moves = Math.floor((odd_element_cnt)/2);
 
    // Two extra moves will be required
    // to make the last odd element even
    if (odd_element_cnt % 2 != 0)
        moves += 2;
 
    // Print the minimum replacements
    document.write(moves);
}
 
// Driver Code
    var arr = [5, 6, 3, 7, 20];
    N =  arr.length;
 
    // Function call
    minMoves(arr, N);
 
</script>


Output: 

3

 

Time complexity: O(N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Last Updated : 28 Feb, 2022
Like Article
Save Article
Similar Reads
Related Tutorials