Given an array arr[] of size N, having 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 element 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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to count reversals to // seperate 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 mimimum reversals // required to make 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; } |
3
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
Recommended Posts:
- Minimum decrements required such that sum of all adjacent pairs in an Array does not exceed K
- Minimum elements to be removed such that sum of adjacent elements is always odd
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Minimum elements to be removed such that sum of adjacent elements is always even
- Minimum number of swaps required for arranging pairs adjacent to each other
- Maximum length of subarray such that all elements are equal in the subarray
- Find element at given index after given range reversals
- Minimum Subarray flips required to convert all elements of a Binary Array to K
- Longest subarray such that adjacent elements have at least one common digit | Set 1
- Longest subarray such that adjacent elements have at least one common digit | Set - 2
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Minimum inversions required so that no two adjacent elements are same
- Maximum length of subarray such that sum of the subarray is even
- Smallest number possible by swapping adjacent even odd pairs
- Split array into K disjoint subarrays such that sum of each subarray is odd.
- Minimum value of maximum absolute difference of all adjacent pairs in an Array
- Count of minimum reductions required to get the required sum K
- Minimize subarray increments/decrements required to reduce all array elements to 0
- Check if all elements of a Circular Array can be made equal by increments of adjacent pairs
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.

