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
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read
Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an
8 min read