Check if K 0s can be flipped such that the given Array has no adjacent 1s
Given a binary array arr[] of size N, and an integer K, the task is to check if K 0s can be flipped such that the array has no adjacent 1s.
Examples:
Input: arr[] = {0, 0, 0, 0, 1}, K=2
Output: true
Explanation: The 0 at indices 0 and 2 can be replaced by 1.
Hence 2 elements can be flipped (=K).
Input: arr[] = {1, 0, 0, 0, 1}, K = 2
Output: false
Explanation: The 0 at index 2 can be replaced by 1.
Hence 1 element can be flipped (!= K).
Approach: The solution is based on greedy approach. Please follow the steps mentioned below:
- Iterate over the array and for every index which has 0, check if its adjacent two indices have 0 or not. For each possible flip, decrement the count of K.
- For the last and first index of the array, check for the adjacent left and right index respectively.
- For every such index satisfying the above condition, Decrement the count of K if it is possible.
- Return true if K<=0, else return false.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool flips(vector< int >& arr, int K)
{
if (arr[0] == 0 && (arr.size() == 1
|| arr[1] == 0)) {
arr[0] = 1;
K--;
}
for ( int i = 1; i < arr.size() - 1;
i++) {
if (arr[i - 1] == 0 && arr[i] == 0
&& arr[i + 1] == 0) {
arr[i] = 1;
K--;
}
}
if (arr.size() > 1
&& arr[arr.size() - 2] == 0
&& arr[arr.size() - 1] == 0) {
arr[arr.size() - 1] = 1;
K--;
}
return K <= 0;
}
int main()
{
vector< int > arr = { 0, 0, 0, 0, 1 };
int K = 2;
cout << (flips(arr, K) ? "true" : "false" );
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static Boolean flips( int arr[], int K)
{
if (arr[ 0 ] == 0 && (arr.length == 1
|| arr[ 1 ] == 0 )) {
arr[ 0 ] = 1 ;
K--;
}
for ( int i = 1 ; i < arr.length - 1 ;
i++) {
if (arr[i - 1 ] == 0 && arr[i] == 0
&& arr[i + 1 ] == 0 ) {
arr[i] = 1 ;
K--;
}
}
if (arr.length > 1
&& arr[arr.length - 2 ] == 0
&& arr[arr.length - 1 ] == 0 ) {
arr[arr.length - 1 ] = 1 ;
K--;
}
return K <= 0 ;
}
public static void main (String[] args) {
int arr[] = { 0 , 0 , 0 , 0 , 1 };
int K = 2 ;
System.out.println(flips(arr, K) ? "true" : "false" );
}
}
|
Python3
def flips(arr, K):
if (arr[ 0 ] = = 0 and ( len (arr) = = 1 or arr[ 1 ] = = 0 )):
arr[ 0 ] = 1 ;
K - = 1
for i in range ( 1 , len (arr) - 1 ):
if (arr[i - 1 ] = = 0 and arr[i] = = 0
and arr[i + 1 ] = = 0 ):
arr[i] = 1 ;
K - = 1
if ( len (arr) > 1
and arr[ len (arr) - 2 ] = = 0
and arr[ len (arr) - 1 ] = = 0 ):
arr[ len (arr) - 1 ] = 1 ;
K - = 1
return K < = 0 ;
arr = [ 0 , 0 , 0 , 0 , 1 ];
K = 2 ;
if (flips(arr, K)):
print ( "true" );
else :
print ( "false" );
|
C#
using System;
class GFG {
static Boolean flips( int [] arr, int K)
{
if (arr[0] == 0 && (arr.Length == 1 || arr[1] == 0)) {
arr[0] = 1;
K--;
}
for ( int i = 1; i < arr.Length - 1;
i++) {
if (arr[i - 1] == 0 && arr[i] == 0
&& arr[i + 1] == 0) {
arr[i] = 1;
K--;
}
}
if (arr.Length > 1
&& arr[arr.Length - 2] == 0
&& arr[arr.Length - 1] == 0) {
arr[arr.Length - 1] = 1;
K--;
}
return K <= 0;
}
public static void Main () {
int [] arr = { 0, 0, 0, 0, 1 };
int K = 2;
Console.Write(flips(arr, K) ? "true" : "false" );
}
}
|
Javascript
<script>
function flips(arr, K) {
if (arr[0] == 0 && (arr.length == 1
|| arr[1] == 0)) {
arr[0] = 1;
K--;
}
for (let i = 1; i < arr.length - 1;
i++) {
if (arr[i - 1] == 0 && arr[i] == 0
&& arr[i + 1] == 0) {
arr[i] = 1;
K--;
}
}
if (arr.length > 1
&& arr[arr.length - 2] == 0
&& arr[arr.length - 1] == 0) {
arr[arr.length - 1] = 1;
K--;
}
return K <= 0;
}
let arr = [0, 0, 0, 0, 1];
let K = 2;
if (flips(arr, K))
document.write( "true" );
else
document.write( "false" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
27 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...