Remove all 1s from the adjacent left of 0s in a Binary Array
Last Updated :
06 Sep, 2022
Given a binary array arr[], the task is to find the number of operations required to remove all 1s from the adjacent left of 0s. In each operation, all 1s, to the immediate left of a 0, are changed to 0.
Examples:
Input: arr[] = { 1, 0, 0, 1, 1, 0 }
Output: 2
Explanation:
Operation 1: Change in index 0 and 4. arr[] = { 0, 0, 0, 1, 0, 0 }
Operation 2: Change in index 3. arr[] = { 0, 0, 0, 0, 0, 0 }
No more operations required.
Input: arr[] = { 0, 1, 0, 1 }
Output: 1
Explanation:
Operation 1: Change in index 1. arr[] = { 0, 0, 0, 1 }
No more operations required.
Approach: This problem can be solved using Greedy Approach. The idea is to calculate the maximum number of consecutive 1’s before 0 which gives the number of times the given operation needed to perform such that the array cannot be altered further.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void noOfMoves( int arr[], int n)
{
int cnt = 0;
int maxCnt = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] == 1) {
cnt++;
}
else {
if (cnt != 0) {
maxCnt = max(maxCnt, cnt);
cnt = 0;
}
}
}
cout << maxCnt << endl;
}
int main()
{
int arr[] = { 0, 1, 1, 1, 1, 0,
0, 1, 1, 0, 0, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
noOfMoves(arr, N);
int arr1[] = { 1, 0, 1, 0, 1, 0, 1, 0 };
N = sizeof (arr) / sizeof (arr[0]);
noOfMoves(arr1, N);
return 0;
}
|
Java
class GFG{
static void noOfMoves( int arr[], int n)
{
int cnt = 0 ;
int maxCnt = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] == 1 ) {
cnt++;
}
else {
if (cnt != 0 ) {
maxCnt = Math.max(maxCnt, cnt);
cnt = 0 ;
}
}
}
System.out.print(maxCnt + "\n" );
}
public static void main(String[] args)
{
int arr[] = { 0 , 1 , 1 , 1 , 1 , 0 ,
0 , 1 , 1 , 0 , 0 , 1 };
int N = arr.length;
noOfMoves(arr, N);
int arr1[] = { 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 };
N = arr1.length;
noOfMoves(arr1, N);
}
}
|
Python 3
def noOfMoves(arr,n):
cnt = 0
maxCnt = 0
for i in range (n):
if (arr[i] = = 1 ):
cnt + = 1
else :
if (cnt ! = 0 ):
maxCnt = max (maxCnt, cnt)
cnt = 0
print (maxCnt)
if __name__ = = '__main__' :
arr = [ 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 ]
N = len (arr)
noOfMoves(arr, N)
arr1 = [ 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 ]
N = len (arr1)
noOfMoves(arr1, N)
|
C#
using System;
public class GFG{
static void noOfMoves( int []arr, int n)
{
int cnt = 0;
int maxCnt = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] == 1) {
cnt++;
}
else {
if (cnt != 0) {
maxCnt = Math.Max(maxCnt, cnt);
cnt = 0;
}
}
}
Console.Write(maxCnt + "\n" );
}
public static void Main(String[] args)
{
int []arr = { 0, 1, 1, 1, 1, 0,
0, 1, 1, 0, 0, 1 };
int N = arr.Length;
noOfMoves(arr, N);
int []arr1 = { 1, 0, 1, 0, 1, 0, 1, 0 };
N = arr1.Length;
noOfMoves(arr1, N);
}
}
|
Javascript
<script>
function noOfMoves(arr, n)
{
let cnt = 0;
let maxCnt = 0;
for (let i = 0; i < n; i++) {
if (arr[i] == 1) {
cnt++;
}
else {
if (cnt != 0) {
maxCnt = Math.max(maxCnt, cnt);
cnt = 0;
}
}
}
document.write( maxCnt + "<br>" );
}
let arr = [ 0, 1, 1, 1, 1, 0,
0, 1, 1, 0, 0, 1 ];
let N = arr.length;
noOfMoves(arr, N);
let arr1 = [ 1, 0, 1, 0, 1, 0, 1, 0 ];
N = arr.length;
noOfMoves(arr1, N);
</script>
|
Time Complexity: O(N), where N is the length of the array.
Auxiliary Space: O(1), as constant space is required.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...