Minimum Subarray reversals to sort given Binary Array (original) (raw)
Last Updated : 28 Jul, 2022
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: 1
Explanation: 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: 2
Explanation: 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>
`
Time Complexity: O(N)
Auxiliary Space: O(1)
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
- Minimize cost to sort given array by sorting unsorted subarrays Given an array arr[] of size N, the task is to minimize the cost to sort the array by sorting any unsorted subarray where the cost of the operation is the difference between the maximum and minimum element of that subarray. This operation can be performed infinite times including 0. Examples: Input: 13 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
- Length of smallest subarray to be removed such that the remaining array is sorted Given an array arr[] consisting of N integers, the task is to print the length of the smallest subarray to be removed from arr[] such that the remaining array is sorted. Examples: Input: arr[] = {1, 2, 3, 10, 4, 2, 3, 5}Output: 3Explanation:The smallest subarray to be remove is {10, 4, 2} of length 8 min read
- Minimum range increment operations to Sort an array Given an array containing N elements. It is allowed to do the below move any number of times on the array: Choose any L and R and increment all numbers in range L to R by 1. The task is to find the minimum number of such moves required to sort the array in non decreasing order. Examples: Input : arr 5 min read