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++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to check // the possibility of K flips 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; } // Driver code int main() { vector< int > arr = { 0, 0, 0, 0, 1 }; int K = 2; cout << (flips(arr, K) ? "true" : "false" ); return 0; } |
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to check // the possibility of K flips 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 ; } // Driver code public static void main (String[] args) { int arr[] = { 0 , 0 , 0 , 0 , 1 }; int K = 2 ; System.out.println(flips(arr, K) ? "true" : "false" ); } } // This code is contributed by hrithikgarg03188 |
Python3
# Python code for the above approach # Function to check # the possibility of K flips 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 ; # Driver code arr = [ 0 , 0 , 0 , 0 , 1 ]; K = 2 ; if (flips(arr, K)): print ( "true" ); else : print ( "false" ); # This code is contributed by Saurabh Jaiswal |
C#
// C# program for the above approach using System; class GFG { // Function to check // the possibility of K flips 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; } // Driver code public static void Main () { int [] arr = { 0, 0, 0, 0, 1 }; int K = 2; Console.Write(flips(arr, K) ? "true" : "false" ); } } // This code is contributed by Saurabh Jaiswal |
Javascript
<script> // JavaScript code for the above approach // Function to check // the possibility of K flips 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; } // Driver code let arr = [0, 0, 0, 0, 1]; let K = 2; if (flips(arr, K)) document.write( "true" ); else document.write( "false" ); // This code is contributed by Potta Lokesh </script> |
Output
true
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...