📌  相关文章
📜  检查二进制数组的所有元素是否可以为1

📅  最后修改于: 2022-05-13 01:57:47.848000             🧑  作者: Mango

检查二进制数组的所有元素是否可以为1

给定一个二进制数组Arr和一个整数K 。如果索引i处的值为1 ,您可以在(i – K) 到 (i + K)的范围内将0更改为1
任务是确定数组的所有元素是否可以设为1
例子:

方法:
如果我们可以达到该索引,则此处使用另一个数组将其标记为1
对于1 到 N范围内的每个索引,如果 Arr[i] 的值为1 ,则进行从(i – K) 到 (i + K)的循环并将 b[i] 更新为1
最后检查 b[i] 的条目,每个i都应该是1 ,如果是则 print 1 else print 0
下面是上述方法的实现:

C++
// C++ implementation
#include 
using namespace std;
 
// Function to print 1 if the
// it is possible to make all array
// element equal to 1 else 0
void checkAllOnes(int arr[], int n,
                 int k)
{
 
    int brr[n];
 
    // Iterating over the array
    for (int i = 0; i < n; i++) {
 
        // If element is 1
        if (arr[i] == 1) {
 
            int h = k + 1;
            int j = i;
 
            // Put b[j...j-k] = 0
            while (j >= 0 && (h--)) {
                brr[j] = 1;
                j--;
            }
 
            h = k + 1;
            j = i;
 
            // Put b[j...j+k] = 0
            while (j < n && (h--)) {
                brr[j] = 1;
                j++;
            }
        }
    }
 
    int flag = 0;
 
    // If any value in aux
    // array equal to 0
    // then set flag
    for (int i = 0; i < n; i++) {
        if (brr[i] == 0) {
            flag = 1;
            break;
        }
    }
 
    // If flag is set this
    // means array after
    // conversion contains
    // 0 so print 0
    if (flag == 1)
        cout << "0";
 
    // else print 1
    else
        cout << "1\n";
}
 
// Driver Code
int main()
{
 
    int arr[] = { 1, 0, 1, 0 };
    int k = 2;
    int n = sizeof(arr) / sizeof(arr[0]);
 
    checkAllOnes(arr, n, k);
 
    return 0;
}


Java
// Java implementation of above approach
class GFG
{
         
// Function to print 1 if the
// it is possible to make all array
// element equal to 1 else 0
static void checkAllOnes(int arr[],
                         int n, int k)
{
    int brr[] = new int[n];
 
    // Iterating over the array
    for (int i = 0; i < n; i++)
    {
 
        // If element is 1
        if (arr[i] == 1)
        {
            int h = k + 1;
            int j = i;
 
            // Put b[j...j-k] = 0
            while ((j >= 0) && (h-- != 0))
            {
                brr[j] = 1;
                j--;
            }
 
            h = k + 1;
            j = i;
 
            // Put b[j...j+k] = 0
            while ((j < n) && (h-- != 0))
            {
                brr[j] = 1;
                j++;
            }
        }
    }
 
    int flag = 0;
 
    // If any value in aux
    // array equal to 0
    // then set flag
    for (int i = 0; i < n; i++)
    {
        if (brr[i] == 0)
        {
            flag = 1;
            break;
        }
    }
 
    // If flag is set this
    // means array after
    // conversion contains
    // 0 so print 0
    if (flag == 1)
        System.out.println("0");
 
    // else print 1
    else
        System.out.println("1");
}
 
// Driver Code
public static void main (String[] args)
{
    int arr[] = { 1, 0, 1, 0 };
    int k = 2;
    int n = arr.length;
 
    checkAllOnes(arr, n, k);
}
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation
 
# Function to print 1 if the
# it is possible to make all array
# element equal to 1 else 0
def checkAllOnes(arr, n, k):
 
    brr = [0 for i in range(n)]
 
    # Iterating over the array
    for i in range(n):
 
        # If element is 1
        if (arr[i] == 1):
 
            h = k + 1
            j = i
 
            # Put b[j...j-k] = 0
            while (j >= 0 and (h)):
                brr[j] = 1
                h -= 1
                j -= 1
 
            h = k + 1
            j = i
 
            # Put b[j...j+k] = 0
            while (j < n and (h)):
                brr[j] = 1
                j += 1
                h -= 1
 
    flag = 0
 
    # If any value in aux
    # array equal to 0
    # then set flag
    for i in range(n):
        if (brr[i] == 0):
            flag = 1
            break
 
    # If flag is set this
    # means array after
    # conversion contains
    # 0 so pr0
    if (flag == 1):
        print("0")
 
    # else pr1
    else:
        print("1")
 
# Driver Code
arr = [1, 0, 1, 0]
k = 2
n = len(arr)
 
checkAllOnes(arr, n, k)
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of above approach
using System;
                     
class GFG
{
         
// Function to print 1 if the
// it is possible to make all array
// element equal to 1 else 0
static void checkAllOnes(int []arr,
                         int n, int k)
{
    int []brr = new int[n];
 
    // Iterating over the array
    for (int i = 0; i < n; i++)
    {
 
        // If element is 1
        if (arr[i] == 1)
        {
            int h = k + 1;
            int j = i;
 
            // Put b[j...j-k] = 0
            while ((j >= 0) && (h-- != 0))
            {
                brr[j] = 1;
                j--;
            }
 
            h = k + 1;
            j = i;
 
            // Put b[j...j+k] = 0
            while ((j < n) && (h-- != 0))
            {
                brr[j] = 1;
                j++;
            }
        }
    }
 
    int flag = 0;
 
    // If any value in aux
    // array equal to 0
    // then set flag
    for (int i = 0; i < n; i++)
    {
        if (brr[i] == 0)
        {
            flag = 1;
            break;
        }
    }
 
    // If flag is set this
    // means array after
    // conversion contains
    // 0 so print 0
    if (flag == 1)
        Console.WriteLine("0");
 
    // else print 1
    else
        Console.WriteLine("1");
}
 
// Driver Code
public static void Main (String[] args)
{
    int []arr = { 1, 0, 1, 0 };
    int k = 2;
    int n = arr.Length;
 
    checkAllOnes(arr, n, k);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:

1