Open In App

Check if given Array can be made a binary Array with K consecutive 1s

Last Updated : 06 Dec, 2022
Comments
Improve
Suggest changes
3 Likes
Like
Report

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 Python3 C# JavaScript PHP

Output
YES

Time Complexity: O(N)
Auxiliary Space: O(N)


Next Article

Similar Reads