Check if given Array can be made a binary Array with K consecutive 1s
Last Updated :
06 Dec, 2022
Given an array A[] of size N and an integer K. Each element of the array is either 0, 1 or 2. The task is to check if the array can be converted into a binary array (elements will be either 0 or 1) by replacing every element equal to 2 with either 0 or 1, such that there are only K occurrences of 1, all of them being consecutive.
Note: If there are multiple ways possible to create such a binary array then print "MULTIPLE".
Examples:
Input: N = 3, K = 2, A[] = {1, 2, 2}
Output: YES
Explanation: Since K = 2, we have to get 2 consecutive 1s in the array. The array could be transformed as -
{1, 2, 2} -> {1, 1, 0}. The resultant array contains only 2 ones, both of which are consecutive and hence the output is YES.
Input: N = 4, K = 2, A[] = {2, 1, 2, 0}
Output: MULTIPLE
Explanation: Note that the array can be transformed into {1, 1, 0, 0} and {0, 1, 1, 0}, both of which satisfies the given condition.
Approach: To solve the problem follow the below observations:
Observation:
Let X be the number of 1s in the array A[]. For each i from i = 0 to N - K, we need to check if we can make all the elements of the subarray A[i . . . i + K - 1] equal to 1 and rest other elements equal to 0. This is possible if, all the 1s are present in subarray A[i] . . . A[i + K - 1] and number of 0s in the same subarray is 0. If these conditions are satisfied by exactly one i, then the output would be YES. Otherwise, it will be multiple if such i are more than 1 or NO if no such i.
Based on the above observation, the following steps can be used to solve the problem :
- Initialize 2 prefix arrays (say s0[] and s1[]), that store the number of 0s and 1s till each index respectively.
- Traverse through the array from i = 0 to N - K.
- At each iteration, check if the subarray from index i to i + K - 1 satisfies both the given conditions (using the prefix sum arrays s0[] and s1[]).
- Return the count of such indices and based on that print the answer.
Following is the code based on the above approach:
C++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to determine if there is only
// one subarray containing K 1s, after
// replacing each 2 with either 0 or 1
string isPossible(int A[], int N, int K)
{
// s0 stores the number of zeroes till
// each index s1 stores the number of
// ones till each index
int s0[N + 1], s1[N + 1];
s0[0] = s1[0] = 0;
// Filling the values of s0 and s1
// for each index
for (int i = 0; i < N; ++i) {
s0[i + 1] = s0[i] + (A[i] == 0 ? 1 : 0);
s1[i + 1] = s1[i] + (A[i] == 1 ? 1 : 0);
}
int answer = 0;
// Traversing from i = 0 to N - K and
// checking if both the conditions are
// satisfied for any index
for (int i = 0; i + K <= N; ++i) {
int c0 = s0[i + K] - s0[i];
int c1 = s1[i + K] - s1[i];
// Checking the condition that all
// the 1s are present in the current
// subarray and there are no zeroes
if (c0 == 0 && c1 == s1[N]) {
answer++;
}
}
// Return the correct answer based on the
// count of indices
if (answer == 1)
return "YES";
else if (answer == 0)
return "NO";
else
return "MULTIPLE";
}
// Driver code
int main()
{
int N = 3, K = 2;
int A[] = { 1, 2, 2 };
// Function Call
cout << isPossible(A, N, K);
return 0;
}
Java
// Java code to implement the approach
import java.io.*;
class GFG {
// Function to determine if there is only
// one subarray containing K 1s, after
// replacing each 2 with either 0 or 1
static String isPossible(int[] A, int N, int K)
{
// s0 stores the number of zeroes till
// each index s1 stores the number of
// ones till each index
int[] s0 = new int[N + 1];
int[] s1 = new int[N + 1];
s0[0] = s1[0] = 0;
// Filling the values of s0 and s1
// for each index
for (int i = 0; i < N; ++i) {
s0[i + 1] = s0[i] + (A[i] == 0 ? 1 : 0);
s1[i + 1] = s1[i] + (A[i] == 1 ? 1 : 0);
}
int answer = 0;
// Traversing from i = 0 to N - K and
// checking if both the conditions are
// satisfied for any index
for (int i = 0; i + K <= N; ++i) {
int c0 = s0[i + K] - s0[i];
int c1 = s1[i + K] - s1[i];
// Checking the condition that all
// the 1s are present in the current
// subarray and there are no zeroes
if (c0 == 0 && c1 == s1[N]) {
answer++;
}
}
// Return the correct answer based on the
// count of indices
if (answer == 1)
return "YES";
else if (answer == 0)
return "NO";
else
return "MULTIPLE";
}
public static void main(String[] args)
{
int N = 3, K = 2;
int A[] = { 1, 2, 2 };
// Function call
System.out.print(isPossible(A, N, K));
}
}
// This code is contributed by lokeshmvs21.
Python3
# Python code for the above approach
# Function to determine if there is only
# one subarray containing K 1s, after
# replacing each 2 with either 0 or 1
def isPossible(A, N, K):
# s0 stores the number of zeroes till
# each index s1 stores the number of
# ones till each index
s0 = [0] * (N + 1)
s1 = [0] * (N + 1)
s0[0] = s1[0] = 0;
# Filling the values of s0 and s1
# for each index
for i in range(N):
s0[i + 1] = s0[i] + (1 if A[i] == 0 else 0);
s1[i + 1] = s1[i] + (1 if A[i] == 1 else 0);
answer = 0;
# Traversing from i = 0 to N - K and
# checking if both the conditions are
# satisfied for any index
i = 0
while(i + K <= N):
c0 = s0[i + K] - s0[i];
c1 = s1[i + K] - s1[i];
# Checking the condition that all
# the 1s are present in the current
# subarray and there are no zeroes
if (c0 == 0 and c1 == s1[N]):
answer += 1
i = i + 1
# Return the correct answer based on the
# count of indices
if (answer == 1):
return "YES";
elif (answer == 0):
return "NO";
else:
return "MULTIPLE";
# Driver code
N = 3
K = 2
A = [1, 2, 2]
# Function Call
print(isPossible(A, N, K));
# This code is contributed by Saurabh Jaiswal
C#
// C# code to implement the approach
using System;
class GFG
{
// Function to determine if there is only
// one subarray containing K 1s, after
// replacing each 2 with either 0 or 1
static string isPossible(int[] A, int N, int K)
{
// s0 stores the number of zeroes till
// each index s1 stores the number of
// ones till each index
int[] s0 = new int[N + 1];
int[] s1 = new int[N + 1];
s0[0] = s1[0] = 0;
// Filling the values of s0 and s1
// for each index
for (int i = 0; i < N; ++i) {
s0[i + 1] = s0[i] + (A[i] == 0 ? 1 : 0);
s1[i + 1] = s1[i] + (A[i] == 1 ? 1 : 0);
}
int answer = 0;
// Traversing from i = 0 to N - K and
// checking if both the conditions are
// satisfied for any index
for (int i = 0; i + K <= N; ++i) {
int c0 = s0[i + K] - s0[i];
int c1 = s1[i + K] - s1[i];
// Checking the condition that all
// the 1s are present in the current
// subarray and there are no zeroes
if (c0 == 0 && c1 == s1[N]) {
answer++;
}
}
// Return the correct answer based on the
// count of indices
if (answer == 1)
return "YES";
else if (answer == 0)
return "NO";
else
return "MULTIPLE";
}
// Driver code
public static void Main(String[] args)
{
int N = 3, K = 2;
int[] A = { 1, 2, 2 };
// Function Call
Console.Write(isPossible(A, N, K));
return;
}
}
// This code is contributed by garg28harsh.
JavaScript
// JavaScript code for the above approach
// Function to determine if there is only
// one subarray containing K 1s, after
// replacing each 2 with either 0 or 1
function isPossible(A, N, K)
{
// s0 stores the number of zeroes till
// each index s1 stores the number of
// ones till each index
let s0 = new Array(N + 1), s1 = new Array(N + 1);
s0[0] = s1[0] = 0;
// Filling the values of s0 and s1
// for each index
for (let i = 0; i < N; ++i) {
s0[i + 1] = s0[i] + (A[i] == 0 ? 1 : 0);
s1[i + 1] = s1[i] + (A[i] == 1 ? 1 : 0);
}
let answer = 0;
// Traversing from i = 0 to N - K and
// checking if both the conditions are
// satisfied for any index
for (let i = 0; i + K <= N; ++i) {
let c0 = s0[i + K] - s0[i];
let c1 = s1[i + K] - s1[i];
// Checking the condition that all
// the 1s are present in the current
// subarray and there are no zeroes
if (c0 == 0 && c1 == s1[N]) {
answer++;
}
}
// Return the correct answer based on the
// count of indices
if (answer == 1)
return "YES";
else if (answer == 0)
return "NO";
else
return "MULTIPLE";
}
// Driver code
let N = 3, K = 2;
let A = [1, 2, 2];
// Function Call
console.log(isPossible(A, N, K));
// This code is contributed by Potta Lokesh
PHP
<?php
// Function to determine if there is only
// one subarray containing K 1s, after
// replacing each 2 with either 0 or 1
function isPossible($A, $N, $K)
{
// s0 stores the number of zeroes till
// each index s1 stores the number of
// ones till each index
$s0 = array();
$s1 = array();
$s0[0] = $s1[0] = 0;
// Filling the values of s0 and s1
// for each index
for ($i = 0; $i < $N; ++$i) {
$s0[$i + 1] = $s0[$i] + ($A[$i] == 0 ? 1 : 0);
$s1[$i + 1] = $s1[$i] + ($A[$i] == 1 ? 1 : 0);
}
$answer = 0;
// Traversing from i = 0 to N - K and
// checking if both the conditions are
// satisfied for any index
for ($i = 0; $i + $K <= $N; ++$i) {
$c0 = $s0[$i + $K] - $s0[$i];
$c1 = $s1[$i + $K] - $s1[$i];
// Checking the condition that all
// the 1s are present in the current
// subarray and there are no zeroes
if ($c0 == 0 && $c1 == $s1[$N]) {
$answer++;
}
}
// Return the correct answer based on the
// count of indices
if ($answer == 1){
return "YES";
}
else if ($answer == 0){
return "NO";
}
else{
return "MULTIPLE";
}
}
// Driver code
$N = 3;
$K = 2;
$A = array(1,2,2);
// Function Call
echo isPossible($A, $N, $K);
// This code is contributed by Kanishka Gupta
?>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Check if all elements of binary array can be made 1 Given a binary array Arr and an integer K. If the value at index i is 1 you can change 0 to 1 with in the range of ( i - K ) to ( i + K ).The task is to determine whether all the elements of the array can be made 1 or not.Examples: Input: Arr = { 0, 1, 0, 1 }, K = 2 Output: 2 Input: Arr = { 1, 0, 0,
7 min read
Check if an array can be split into subsets of K consecutive elements Given an array arr[] and integer K, the task is to split the array into subsets of size K, such that each subset consists of K consecutive elements. Examples: Input: arr[] = {1, 2, 3, 6, 2, 3, 4, 7, 8}, K = 3 Output: true Explanation: The given array of length 9 can be split into 3 subsets {1, 2, 3}
5 min read
Check if Binary Array can be made palindrome after K bitwise XOR with 1 Given a binary array arr[] of size N and an integer K, the task is to check whether the binary array can be turned into a palindrome after K number of operations where in one operation, any random index can be chosen and the value stored in the index will be replaced by its bitwise XOR(^) with1. Exa
9 min read
Check if elements of a Binary Matrix can be made alternating Given a 2D array grid[][] of size N * M, consisting of the characters "1", "0", and "*", where "*" denotes an empty space and can be replaced by either a "1" or a "0". The task is to fill the grid such that "0" and "1" occur alternatively and no two consecutive characters occur together, i.e. (10101
15+ min read
Generate a Binary String without any consecutive 0's and at most K consecutive 1's Given two integers N and M, the task is to construct a binary string with the following conditions : The Binary String consists of N 0's and M 1'sThe Binary String has at most K consecutive 1's.The Binary String does not contain any adjacent 0's. If it is not possible to construct such a binary stri
7 min read
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=2Output: trueExplanation: The 0 at indices 0 and 2 can be replaced by 1. Hence 2 elements can be flipped (=K). Input
5 min read
Check whether a given array is a k sorted array or not Given an array of n distinct elements. Check whether the given array is a k sorted array or not. A k sorted array is an array where each element is at most k distances away from its target position in the sorted array. For example, let us consider k is 2, an element at index 7 in the sorted array, c
12 min read
Check if given Array can be divided into subsequences of K increasing consecutive integers Given an array arr[] of N integers and a positive integer K, the task is to check if it is possible to divide the array into increasing subsequences of K consecutive integers such each element can contribute in only a single subsequence. Example: Input: arr[] = {1, 2, 1, 3, 2, 3}, K = 3Output: YesEx
11 min read
Construct a K-length binary string from an array based on given conditions Given an array arr[] consisting of N integers, and an integer K, the task is to construct a binary string of length K satisfying the following conditions: The character at ith index is '1' if a subset with sum i can be formed from the array.Otherwise, the character at ith index is '0'. Examples: Inp
5 min read
Check if K '0's can be flipped such that Binary String contains no pair of adjacent '1's Given a binary string S of length N and an integer K, the task is to check if it is possible to flip K 0s such that the resulting string does not contain any pair of adjacent 1s. If it is possible to do so, then print "Yes". Otherwise, print "No". Input: S = "01001001000", K = 1Output: YesExplanatio
9 min read