Minimum moves to sort Binary Array in increasing order by deleting Subarray
Last Updated :
04 Apr, 2023
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 from A. (Both the left and right halves are merged after performing the operation)
Examples:
Input: A[] = {0, 1, 1, 1, 0}
Output: 1
?Explanation: We can use one operation to remove the sorted subarray A2…4 = {1, 1, 1} from array A .Thus, the remaining array A = {0, 0} is sorted.
Input: A[] = {1, 1}
Output: 0
Approach: The problem can be solved based on the following observation:
- Deleting a sorted subarray means we delete either:
- A subarray whose characters are all the same; or
- A subarray that looks like {0, 0, …, 0, 1, …1, 1, }
- This also tells us that the final array must be of this form. In particular, a binary array is sorted if and only if it doesn’t contain {1, 0} as a subarray, so our aim is to remove all such subsequences.
Now note in a case where we have an alternating binary array, whose first character is 1 and last character is 0. This means it must look like {1, 0, 1, 0, 1, 0, …1, 0} and in particular has even length, say 2K. The minimum number of moves needed to sort such an array is simply K.
Follow the below steps to solve the problem:
- Set count = 0.
- Iterate a loop from 0 to N-2 on array A[]:
- Check if there is a sequence of ‘1’ followed by a ‘0’ exist,
- Then increment the value of the count.
- Print the value of count as an answer for the minimum number of operations required so that array A is sorted in increasing order.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minOperation( int arr[], int n)
{
int count = 0;
for ( int j = 0; j < n - 1; j++) {
if (arr[j] == 1 && arr[j + 1] == 0) {
count++;
}
}
return count;
}
int main()
{
int A[] = { 0, 1, 1, 1, 0 };
int N = sizeof (A) / sizeof ( int );
cout << minOperation(A, N);
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
public static int minOperation( int arr[], int n)
{
int count = 0 ;
for ( int j = 0 ; j < n - 1 ; j++) {
if (arr[j] == 1 && arr[j + 1 ] == 0 ) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
int [] A = { 0 , 1 , 1 , 1 , 0 };
int N = A.length;
System.out.println(minOperation(A, N));
}
}
|
Python3
def minOperation(arr, n):
count = 0
for j in range ( 0 , n - 1 ):
if (arr[j] = = 1 and arr[j + 1 ] = = 0 ):
count + = 1
return count
A = [ 0 , 1 , 1 , 1 , 0 ]
N = len (A)
print (minOperation(A, N))
|
C#
using System;
public class GFG{
public static int minOperation( int [] arr, int n)
{
int count = 0;
for ( int j = 0; j < n - 1; j++) {
if (arr[j] == 1 && arr[j + 1] == 0) {
count++;
}
}
return count;
}
static public void Main (){
int [] A = { 0, 1, 1, 1, 0 };
int N = A.Length;
Console.WriteLine(minOperation(A, N));
}
}
|
Javascript
function minOperation(arr, n)
{
let count = 0
for (let j = 0; j < n - 1; j++) {
if (arr[j] == 1 && arr[j + 1] == 0) {
count += 1
}
}
return count;
}
let A = [ 0, 1, 1, 1, 0 ]
let N = A.length
console.log(minOperation(A, N))
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Another Approach:
- Set count = 0.
- Check if the input array is an alternating binary array of length 2K. If it is, return K.
- Otherwise, iterate a loop from 0 to N-2 on array A[]:
- Check if there is a sequence of ‘1’ followed by a ‘0’ exist, Then increment the value of the count.
- Print the value of count as an answer for the minimum number of operations required so that array A is sorted in increasing order.
C++
#include <bits/stdc++.h>
using namespace std;
int isAlternatingBinary( int arr[], int n)
{
if (n % 2 == 1
|| set< int >(arr, arr + n) != set< int >({ 0, 1 })) {
return -1;
}
for ( int i = 0; i < n; i++) {
if (i % 2 == 0 && arr[i] != 1) {
return -1;
}
if (i % 2 == 1 && arr[i] != 0) {
return -1;
}
}
return n / 2;
}
int minOperation( int arr[], int n)
{
int K = isAlternatingBinary(arr, n);
if (K != -1) {
return K;
}
int count = 0;
for ( int j = 0; j < n - 1; j++) {
if (arr[j] == 1 && arr[j + 1] == 0) {
count++;
}
}
return count;
}
int main()
{
int A[] = { 0, 1, 1, 1, 0 };
int N = sizeof (A) / sizeof ( int );
cout << minOperation(A, N);
}
|
Python3
def isAlternatingBinary(arr, n):
if n % 2 = = 1 or set (arr) ! = { 0 , 1 }:
return - 1
for i in range (n):
if i % 2 = = 0 and arr[i] ! = 1 :
return - 1
if i % 2 = = 1 and arr[i] ! = 0 :
return - 1
return n / / 2
def minOperation(arr, n):
K = isAlternatingBinary(arr, n)
if K ! = - 1 :
return K
count = 0
for j in range (n - 1 ):
if arr[j] = = 1 and arr[j + 1 ] = = 0 :
count + = 1
return count
A = [ 0 , 1 , 1 , 1 , 0 ]
N = len (A)
print (minOperation(A, N))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static int IsAlternatingBinary( int [] arr, int n)
{
if (n % 2 == 1 || new HashSet< int >(arr).SetEquals( new HashSet< int > { 0, 1 }))
{
return -1;
}
for ( int i = 0; i < n; i++)
{
if (i % 2 == 0 && arr[i] != 1)
{
return -1;
}
if (i % 2 == 1 && arr[i] != 0)
{
return -1;
}
}
return n / 2;
}
static int MinOperation( int [] arr, int n)
{
int K = IsAlternatingBinary(arr, n);
if (K != -1)
{
return K;
}
int count = 0;
for ( int j = 0; j < n - 1; j++)
{
if (arr[j] == 1 && arr[j + 1] == 0)
{
count++;
}
}
return count;
}
static void Main( string [] args)
{
int [] A = { 0, 1, 1, 1, 0 };
int N = A.Length;
Console.WriteLine(MinOperation(A, N));
}
}
|
Java
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static int isAlternatingBinary( int [] arr, int n)
{
if (n % 2 == 1 || ! new HashSet<Integer>() {
{
add( 0 );
add( 1 );
}
}.equals( new HashSet<Integer>() {
{
for ( int i : arr)
add(i);
}
})) {
return - 1 ;
}
for ( int i = 0 ; i < n; i++) {
if (i % 2 == 0 && arr[i] != 1 ) {
return - 1 ;
}
if (i % 2 == 1 && arr[i] != 0 ) {
return - 1 ;
}
}
return n / 2 ;
}
public static int minOperation( int [] arr, int n)
{
int K = isAlternatingBinary(arr, n);
if (K != - 1 ) {
return K;
}
int count = 0 ;
for ( int j = 0 ; j < n - 1 ; j++) {
if (arr[j] == 1 && arr[j + 1 ] == 0 ) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
int [] A = { 0 , 1 , 1 , 1 , 0 };
int N = A.length;
System.out.println(minOperation(A, N));
}
}
|
Javascript
function isAlternatingBinary(arr) {
const n = arr.length;
if (n % 2 === 1 || new Set(arr).size !== 2 || !arr.every(x => x === 0 || x === 1)) {
return -1;
}
for (let i = 0; i < n; i++) {
if (i % 2 === 0 && arr[i] !== 1) {
return -1;
}
if (i % 2 === 1 && arr[i] !== 0) {
return -1;
}
}
return n/2;
}
function minOperation(arr) {
const K = isAlternatingBinary(arr);
if (K !== -1) {
return K;
}
let count = 0;
for (let j = 0; j < arr.length - 1; j++) {
if (arr[j] === 1 && arr[j + 1] === 0) {
count++;
}
}
return count;
}
const A = [0, 1, 1, 1, 0];
console.log(minOperation(A));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...