Open In App

Minimum subarray reversals required such that sum of all pairs of adjacent elements is odd

Given an array arr[] of size N, having an equal number of even and odd integers, the task is to find the minimum number of subarrays required to be reversed to make the sum of pairs of adjacent elements odd.

Examples:



Input: arr[] = {13, 2, 6, 8, 3, 5, 7, 10, 14, 15} 
Output: 3
Explanation:
Step 1: Reverse subarray [2, 4] to modify the array to {13, 2, 3, 8, 6, 5, 7, 10, 14, 15}
Step 2: Reverse subarray [4, 5] to modify the array to {13, 2, 3, 8, 5, 6, 7, 10, 14, 15}
Step 3: Reverse subarray [8, 9] to modify the array to {13, 2, 3, 8, 5, 6, 7, 10, 15, 14}
After the above reversals, the sum of all adjacent element pairs is odd.
Therefore, the minimum reversals required is 3.

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



Approach: To make the sum of the adjacent elements odd, the idea is to arrange the elements in an odd-even or an even-odd manner. To minimize the count of reversal operations, observe the following property of the given array:

Follow the steps below to solve the problem:

Below is the implementation of the above approach:




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count reversals to
// separate elements with same parity
int separate(int arr[], int n, int parity)
{
    int count = 1, res = 0;
 
    // Traverse the given array
    for (int i = 1; i < n; i++) {
 
        // Count size of subarray having
        // integers with same parity only
        if (((arr[i] + parity) & 1)
            && ((arr[i - 1] + parity) & 1))
            count++;
 
        // Otherwise
        else {
 
            // Reversals required is equal
            // to one less than subarray size
            if (count > 1)
                res += count - 1;
 
            count = 1;
        }
    }
 
    // Return the total reversals
    return res;
}
 
// Function to print the array elements
void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Function to count the minimum reversals
// required to make sum
// of all adjacent elements odd
void requiredOps(int arr[], int N)
{
    // Stores operations required for
    // separating adjacent odd elements
    int res1 = separate(arr, N, 0);
 
    // Stores operations required for
    // separating adjacent even elements
    int res2 = separate(arr, N, 1);
 
    // Maximum of two is the return
    cout << max(res1, res2);
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 13, 2, 6, 8, 3, 5,
                  7, 10, 14, 15 };
 
    // Size of array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    requiredOps(arr, N);
 
    return 0;
}




// Java program for the above approach
import java.util.*;
 
class GFG{
   
// Function to count reversals to
// separate elements with same parity
static int separate(int arr[], int n, 
                    int parity)
{
    int count = 1, res = 0;
     
    // Traverse the given array
    for(int i = 1; i < n; i++)
    {
         
        // Count size of subarray having
        // integers with same parity only
        if (((arr[i] + parity) & 1) != 0 &&
            ((arr[i - 1] + parity) & 1) != 0)
            count++;
 
        // Otherwise
        else
        {
             
            // Reversals required is equal
            // to one less than subarray size
            if (count > 1)
                res += count - 1;
 
            count = 1;
        }
    }
 
    // Return the total reversals
    return res;
}
 
// Function to print the array elements
void printArray(int arr[], int n)
{
    for(int i = 0; i < n; i++)
        System.out.println(arr[i] + " ");
}
 
// Function to count the minimum reversals
// required to make sum
// of all adjacent elements odd
static void requiredOps(int arr[], int N)
{
     
    // Stores operations required for
    // separating adjacent odd elements
    int res1 = separate(arr, N, 0);
 
    // Stores operations required for
    // separating adjacent even elements
    int res2 = separate(arr, N, 1);
 
    // Maximum of two is the return
    System.out.print(Math.max(res1, res2));
}
 
// Driver Code
public static void main(String args[])
{
     
    // Given array arr[]
    int arr[] = { 13, 2, 6, 8, 3, 5,
                  7, 10, 14, 15 };
                   
    // Size of array
    int N = arr.length;
     
    // Function Call
    requiredOps(arr, N);
}
}
 
// This code is contributed by ipg2016107




# Python3 program for the
# above approach
 
# Function to count reversals
# to separate elements with
# same parity
def separate(arr, n, parity):
 
    count = 1
    res = 0
 
    # Traverse the given array
    for i in range(1, n):
 
        # Count size of subarray
        # having integers with
        # same parity only
        if (((arr[i] + parity) & 1) and
            ((arr[i - 1] + parity) & 1)):
            count += 1
 
        # Otherwise
        else:
 
            # Reversals required is
            # equal to one less than
            # subarray size
            if (count > 1):
                res += count - 1
 
            count = 1
 
    # Return the total
    # reversals
    return res
 
# Function to print the
# array elements
def printArray(arr, n):
 
    for i in range(n):
        print(arr[i],
              end = " ")
 
# Function to count the minimum
# reversals required to make
# make sum of all adjacent
# elements odd
def requiredOps(arr, N):
 
    # Stores operations required
    # for separating adjacent
    # odd elements
    res1 = separate(arr, N, 0)
 
    # Stores operations required
    # for separating adjacent
    # even elements
    res2 = separate(arr, N, 1)
 
    # Maximum of two is the
    # return
    print(max(res1, res2))
 
# Driver Code
if __name__ == "__main__":
 
    # Given array arr[]
    arr = [13, 2, 6, 8, 3, 5,
           7, 10, 14, 15]
 
    # Size of array
    N = len(arr)
 
    # Function Call
    requiredOps(arr, N)
 
# This code is contributed by Chitranayal




// C# program for the above approach
using System;
  
class GFG{
  
// Function to count reversals to
// separate elements with same parity
static int separate(int[] arr, int n, 
                    int parity)
{
    int count = 1, res = 0;
      
    // Traverse the given array
    for(int i = 1; i < n; i++)
    {
         
        // Count size of subarray having
        // integers with same parity only
        if (((arr[i] + parity) & 1) != 0 &&
            ((arr[i - 1] + parity) & 1) != 0)
            count++;
  
        // Otherwise
        else
        {
             
            // Reversals required is equal
            // to one less than subarray size
            if (count > 1)
                res += count - 1;
  
            count = 1;
        }
    }
  
    // Return the total reversals
    return res;
}
  
// Function to print the array elements
void printArray(int[] arr, int n)
{
    for(int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
}
  
// Function to count the minimum reversals
// required to make make sum
// of all adjacent elements odd
static void requiredOps(int[] arr, int N)
{
      
    // Stores operations required for
    // separating adjacent odd elements
    int res1 = separate(arr, N, 0);
  
    // Stores operations required for
    // separating adjacent even elements
    int res2 = separate(arr, N, 1);
  
    // Maximum of two is the return
    Console.Write(Math.Max(res1, res2));
}
 
// Driver code
public static void Main()
{
     
    // Given array arr[]
    int[] arr = { 13, 2, 6, 8, 3, 5,
                  7, 10, 14, 15 };
                    
    // Size of array
    int N = arr.Length;
      
    // Function Call
    requiredOps(arr, N);
}
}
 
// This code is contributed by sanjoy_62




<script>
 
// JavaScript program to implement
// the above approach
 
// Function to count reversals to
// separate elements with same parity
function separate(arr, n, parity)
{
    let count = 1, res = 0;
      
    // Traverse the given array
    for(let i = 1; i < n; i++)
    {
          
        // Count size of subarray having
        // integers with same parity only
        if (((arr[i] + parity) & 1) != 0 &&
            ((arr[i - 1] + parity) & 1) != 0)
            count++;
  
        // Otherwise
        else
        {
              
            // Reversals required is equal
            // to one less than subarray size
            if (count > 1)
                res += count - 1;
  
            count = 1;
        }
    }
  
    // Return the total reversals
    return res;
}
  
// Function to print the array elements
function printArray(arr, n)
{
    for(let i = 0; i < n; i++)
        document.write(arr[i] + " ");
}
  
// Function to count the minimum reversals
// required to make make sum
// of all adjacent elements odd
function requiredOps( arr, N)
{
      
    // Stores operations required for
    // separating adjacent odd elements
    let res1 = separate(arr, N, 0);
  
    // Stores operations required for
    // separating adjacent even elements
    let res2 = separate(arr, N, 1);
  
    // Maximum of two is the return
    document.write(Math.max(res1, res2));
}
 
// Driver Code
 
    // Given array arr[]
    let arr = [ 13, 2, 6, 8, 3, 5,
                  7, 10, 14, 15 ];
                    
    // Size of array
    let N = arr.length;
      
    // Function Call
    requiredOps(arr, N);
      
</script>

Output: 
3

 

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


Article Tags :