Minimum Subarray reversals to sort given Binary Array Last Updated : 28 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Given a binary array A[] of size N, the task is to find the minimum number of subarrays that need to be reversed to sort the binary array. Examples: Input: N = 4, A[]: {1, 0 , 0, 1} Output: 1Explanation: Reverse the array from 0 to 2 to change the array to {0, 0, 1, 1} Input: N = 4, A[]: {1, 0, 1 , 0}Output: 2Explanation: Reverse the array from 1 to 2 and then from 0 to 3 Approach: The idea to solve the problem is as follows: To sort A[] iterate through the array and reverse every leftmost instance of a subarray of A[] with consecutive '1's and then consecutive '0's. The count of all these subarrays can be found by counting the indices where A[i] = 1 and the next element i.e., A[i+1] = 0. Follow the steps below to implement the idea: Initialize a count variable with 0. Run a loop from 0 to N-2, and in each iteration do the following:If the ith element is 1 and (i+1)th element is 0 increment count by one as there is a subarray of the type [. . .1100. . . ] that needs to be reversed to sort the array.Return the count variable. Below is the implementation of the above approach. C++ // C++ Code to Implement the approach #include <bits/stdc++.h> using namespace std; // Function to count the minimum number of reversals int minOperations(int n, int A[]) { // Declare variable to count the operations int count = 0; for (int i = 0; i < n - 1; i++) { // Whenever there is a pattern of // consecutive 1's followed by 0's // It means you have to reverse that // subarray, so increase your count by 1 if (A[i] == 1 && A[i + 1] == 0) { count++; } } // Return the count return count; } // Driver Code int main() { int A[] = { 1, 0, 1, 0 }; int N = sizeof(A) / sizeof(A[0]); // Function Call cout << minOperations(N, A); return 0; } Java // Java Code to Implement the approach public class GFG { // Function to count the minimum number of reversals static int minOperations(int n, int A[]) { // Declare variable to count the operations int count = 0; for (int i = 0; i < n - 1; i++) { // Whenever there is a pattern of // consecutive 1's followed by 0's // It means you have to reverse that // subarray, so increase your count by 1 if (A[i] == 1 && A[i + 1] == 0) { count++; } } // Return the count return count; } // Driver Code public static void main (String[] args) { int A[] = { 1, 0, 1, 0 }; int N = A.length; // Function Call System.out.println(minOperations(N, A)); } } // This code is contributed by AnkThon Python3 # Python3 Code to Implement the approach # Function to count the minimum number of reversals def minOperations(n, A) : # Declare variable to count the operations count = 0; for i in range(n-1) : # Whenever there is a pattern of # consecutive 1's followed by 0's # It means you have to reverse that # subarray, so increase your count by 1 if (A[i] == 1 and A[i + 1] == 0) : count += 1; # Return the count return count; # Driver Code if __name__ == "__main__" : A = [ 1, 0, 1, 0 ]; N = len(A); # Function Call print(minOperations(N, A)); # This code is contributed by AnkThon C# // C# Code to Implement the approach using System; public class GFG { // Function to count the minimum number of reversals static int minOperations(int n, int []A) { // Declare variable to count the operations int count = 0; for (int i = 0; i < n - 1; i++) { // Whenever there is a pattern of // consecutive 1's followed by 0's // It means you have to reverse that // subarray, so increase your count by 1 if (A[i] == 1 && A[i + 1] == 0) { count++; } } // Return the count return count; } // Driver Code public static void Main (string[] args) { int []A = { 1, 0, 1, 0 }; int N = A.Length; // Function Call Console.WriteLine(minOperations(N, A)); } } // This code is contributed by AnkThon JavaScript <script> // JavaScript Code to Implement the approach // Function to count the minimum number of reversals const minOperations = (n, A) => { // Declare variable to count the operations let count = 0; for (let i = 0; i < n - 1; i++) { // Whenever there is a pattern of // consecutive 1's followed by 0's // It means you have to reverse that // subarray, so increase your count by 1 if (A[i] == 1 && A[i + 1] == 0) { count++; } } // Return the count return count; } // Driver Code let A = [1, 0, 1, 0]; let N = A.length // Function Call document.write(minOperations(N, A)); // This code is contributed by rakeshsahni </script> Output2 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Minimum Subarray reversals to sort given Binary Array meswatisingh630 Follow Improve Article Tags : C/C++ Puzzles Advanced Data Structure DSA Practice Tags : Advanced Data Structure Similar Reads Minimum subarray reversals required to make given binary array alternating Given a binary array arr[] consisting of equal count of 0s and 1s, the task is to count the minimum number of subarray reversal operations required to make the binary array alternating. In each operation reverse any subarray of the given array. Examples: Input: arr[] = { 1, 1, 1, 0, 1, 0, 0, 0 } Out 5 min read Minimum moves to sort Binary Array in increasing order by deleting Subarray Given a binary array A[] of length N, the task is to find the minimum number of operations required so that array A is sorted in increasing order and we can perform the following operation on array A[] such as: Choose any subarray Aiâ¦j(1 ? i ? j ? N) which is sorted in increasing order and remove it 10 min read Minimum length subarray of 1s in a Binary Array Given binary array. The task is to find the length of subarray with minimum number of 1s.Note: It is guaranteed that there is atleast one 1 present in the array.Examples : Input : arr[] = {1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1} Output : 3 Minimum length subarray of 1s is {1, 1}.Input : arr[] = {0, 0, 1 8 min read Minimum adjacent swaps required to Sort Binary array Given a binary array, the task is to find the minimum number of swaps needed to sort this binary array. We are allowed to swap only adjacent elements.Examples: Input : arr[] = [0, 0, 1, 0, 1, 0, 1, 1]Output : 3Explanation:1st swap : [0, 0, 1, 0, 0, 1, 1, 1]2nd swap : [0, 0, 0, 1, 0, 1, 1, 1]3rd swap 5 min read Number of subarrays required to be rearranged to sort the given array Given an array arr[] consisting of the first N natural numbers, the task is to find the minimum number of subarrays required to be rearranged such that the resultant array is sorted. Examples: Input: arr[] = {2, 1, 4, 3, 5}Output: 1Explanation:Operation 1: Choose the subarray {arr[0], arr[3]}, i.e. 6 min read Minimum number of 1's to be replaced in a binary array Given a binary array arr[] of zero's and one's only. The task is to find the minimum number of one's to be changed to zero such if there exist any index i in the array such that arr[i] = 0 then arr[i-1] and arr[i+1] both should not be equals to 1 at the same time. That is, for any index i the below 5 min read Sort the Array by reversing the numbers in it Given an array arr[] of N non-negative integers, the task is to sort these integers according to their reverse. Examples: Input: arr[] = {12, 10, 102, 31, 15} Output: 10 31 12 15 102 Reversing the numbers: 12 -> 21 10 -> 01 102 -> 201 31 -> 13 15 -> 51 Sorting the reversed numbers: 01 6 min read Minimum Subarray flips required to convert all elements of a Binary Array to K The problem statement is asking for the minimum number of operations required to convert all the elements of a given binary array arr[] to a specified value K, where K can be either 0 or 1. The operations can be performed on any index X of the array, and the operation is to flip all the elements of 8 min read Check if reversing a sub array make the array sorted Given an array of n distinct integers. The task is to check whether reversing any one sub-array can make the array sorted or not. If the array is already sorted or can be made sorted by reversing any one subarray, print "Yes", else print "No". Examples: Input : arr [] = {1, 2, 5, 4, 3}Output : YesBy 15+ min read Minimum cost to sort strings using reversals of different costs Given an array of strings arr[] and an array cost[] representing the cost to reverse each corresponding string. We are not allowed to move or reorder the strings in the array, we can only reverse individual strings if needed. The task is to choose which strings to reverse such that the final array i 15+ min read Like