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:
- If there is any subarray of length M having all elements of the same parity then there also exists a subarray of length M having all elements of the opposite parity as the count of even and odd element are the same in the array.
- Therefore, the count of reversal operation required to make the subarray of length M of alternate parity is (M – 1).
Follow the steps below to solve the problem:
- Traverse the given array and find the count of reversal of subarray required for each consecutive same odd and even elements in the array.
- Let the count of reversal for consecutive odd and even elements be cntOdd and cntEven respectively.
- The minimum count of reversal is given by the maximum of cntOdd and cntEven as the task is to remove all the consecutive same elements and maximum consecutive needed to be removed.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int separate( int arr[], int n, int parity)
{
int count = 1, res = 0;
for ( int i = 1; i < n; i++) {
if (((arr[i] + parity) & 1)
&& ((arr[i - 1] + parity) & 1))
count++;
else {
if (count > 1)
res += count - 1;
count = 1;
}
}
return res;
}
void printArray( int arr[], int n)
{
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
}
void requiredOps( int arr[], int N)
{
int res1 = separate(arr, N, 0);
int res2 = separate(arr, N, 1);
cout << max(res1, res2);
}
int main()
{
int arr[] = { 13, 2, 6, 8, 3, 5,
7, 10, 14, 15 };
int N = sizeof (arr) / sizeof (arr[0]);
requiredOps(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int separate( int arr[], int n,
int parity)
{
int count = 1 , res = 0 ;
for ( int i = 1 ; i < n; i++)
{
if (((arr[i] + parity) & 1 ) != 0 &&
((arr[i - 1 ] + parity) & 1 ) != 0 )
count++;
else
{
if (count > 1 )
res += count - 1 ;
count = 1 ;
}
}
return res;
}
void printArray( int arr[], int n)
{
for ( int i = 0 ; i < n; i++)
System.out.println(arr[i] + " " );
}
static void requiredOps( int arr[], int N)
{
int res1 = separate(arr, N, 0 );
int res2 = separate(arr, N, 1 );
System.out.print(Math.max(res1, res2));
}
public static void main(String args[])
{
int arr[] = { 13 , 2 , 6 , 8 , 3 , 5 ,
7 , 10 , 14 , 15 };
int N = arr.length;
requiredOps(arr, N);
}
}
|
Python3
def separate(arr, n, parity):
count = 1
res = 0
for i in range ( 1 , n):
if (((arr[i] + parity) & 1 ) and
((arr[i - 1 ] + parity) & 1 )):
count + = 1
else :
if (count > 1 ):
res + = count - 1
count = 1
return res
def printArray(arr, n):
for i in range (n):
print (arr[i],
end = " " )
def requiredOps(arr, N):
res1 = separate(arr, N, 0 )
res2 = separate(arr, N, 1 )
print ( max (res1, res2))
if __name__ = = "__main__" :
arr = [ 13 , 2 , 6 , 8 , 3 , 5 ,
7 , 10 , 14 , 15 ]
N = len (arr)
requiredOps(arr, N)
|
C#
using System;
class GFG{
static int separate( int [] arr, int n,
int parity)
{
int count = 1, res = 0;
for ( int i = 1; i < n; i++)
{
if (((arr[i] + parity) & 1) != 0 &&
((arr[i - 1] + parity) & 1) != 0)
count++;
else
{
if (count > 1)
res += count - 1;
count = 1;
}
}
return res;
}
void printArray( int [] arr, int n)
{
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
static void requiredOps( int [] arr, int N)
{
int res1 = separate(arr, N, 0);
int res2 = separate(arr, N, 1);
Console.Write(Math.Max(res1, res2));
}
public static void Main()
{
int [] arr = { 13, 2, 6, 8, 3, 5,
7, 10, 14, 15 };
int N = arr.Length;
requiredOps(arr, N);
}
}
|
Javascript
<script>
function separate(arr, n, parity)
{
let count = 1, res = 0;
for (let i = 1; i < n; i++)
{
if (((arr[i] + parity) & 1) != 0 &&
((arr[i - 1] + parity) & 1) != 0)
count++;
else
{
if (count > 1)
res += count - 1;
count = 1;
}
}
return res;
}
function printArray(arr, n)
{
for (let i = 0; i < n; i++)
document.write(arr[i] + " " );
}
function requiredOps( arr, N)
{
let res1 = separate(arr, N, 0);
let res2 = separate(arr, N, 1);
document.write(Math.max(res1, res2));
}
let arr = [ 13, 2, 6, 8, 3, 5,
7, 10, 14, 15 ];
let N = arr.length;
requiredOps(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)