📌  相关文章
📜  查询以回答给定索引左侧的1和零的数目

📅  最后修改于: 2021-05-05 01:14:15             🧑  作者: Mango

给定一个二进制数组和Q查询。每个查询都由数字K组成,任务是在索引K的左侧打印1和0的数量。

例子:

方法:可以按照以下步骤解决上述问题:

  • 声明一个对对数组(例如left [] ),该对数组将用于预先计算左边的1和0的数量。
  • 在数组上进行迭代,并在每一步中使用左侧的1和0的数量初始化left [i]
  • 者的每个查询的数目将被留[K]。首先和零的数目将被留[K]。第二

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to pre-calculate the left[] array
void preCalculate(int binary[], int n, pair left[])
{
    int count1 = 0, count0 = 0;
  
    // Iterate in the binary array
    for (int i = 0; i < n; i++) {
  
        // Initialize the number
        // of 1 and 0
        left[i].first = count1;
        left[i].second = count0;
  
        // Increase the count
        if (binary[i])
            count1++;
        else
            count0++;
    }
}
  
// Driver code
int main()
{
  
    int binary[] = { 1, 1, 1, 0, 0, 1, 0, 1, 1 };
    int n = sizeof(binary) / sizeof(binary[0]);
    pair left[n];
    preCalculate(binary, n, left);
  
    // Queries
    int queries[] = { 0, 1, 2, 4 };
    int q = sizeof(queries) / sizeof(queries[0]);
  
    // Solve queries
    for (int i = 0; i < q; i++)
        cout << left[queries[i]].first << " ones "
             << left[queries[i]].second << " zeros\n";
  
    return 0;
}


Java
// Java implementation of the approach
class GFG {
  
    // pair class
    static class pair {
        int first, second;
        pair(int a, int b)
        {
            first = a;
            second = b;
        }
    }
  
    // Function to pre-calculate the left[] array
    static void preCalculate(int binary[], int n,
                             pair left[])
    {
        int count1 = 0, count0 = 0;
  
        // Iterate in the binary array
        for (int i = 0; i < n; i++) {
  
            // Initialize the number
            // of 1 and 0
            left[i].first = count1;
            left[i].second = count0;
  
            // Increase the count
            if (binary[i] != 0)
                count1++;
            else
                count0++;
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
  
        int binary[] = { 1, 1, 1, 0, 0, 1, 0, 1, 1 };
        int n = binary.length;
        pair left[] = new pair[n];
  
        for (int i = 0; i < n; i++)
            left[i] = new pair(0, 0);
  
        preCalculate(binary, n, left);
  
        // Queries
        int queries[] = { 0, 1, 2, 4 };
        int q = queries.length;
  
        // Solve queries
        for (int i = 0; i < q; i++)
            System.out.println(left[queries[i]].first + " ones "
                               + left[queries[i]].second + " zeros\n");
    }
}
  
// This code is contributed by Arnab Kundu


Python3
# Python3 implementation of the approach
  
# Function to pre-calculate the left[] array
def preCalculate(binary, n, left):
  
    count1, count0 = 0, 0
  
    # Iterate in the binary array
    for i in range(n):
  
        # Initialize the number
        # of 1 and 0
        left[i][0] = count1
        left[i][1] = count0
  
        # Increase the count
        if (binary[i]):
            count1 += 1
        else:
            count0 += 1
  
# Driver code
binary = [1, 1, 1, 0, 0, 1, 0, 1, 1]
  
n = len(binary)
  
left = [[ 0 for i in range(2)]
            for i in range(n)]
  
preCalculate(binary, n, left)
  
queries = [0, 1, 2, 4 ]
  
q = len(queries)
  
# Solve queries
for i in range(q):
    print(left[queries[i]][0], "ones", 
          left[queries[i]][1], "zeros")
  
# This code is contributed 
# by mohit kumar


C#
// C# implementation of the approach
using System;
  
class GFG {
  
    // pair class
    public class pair {
        public int first, second;
        public pair(int a, int b)
        {
            first = a;
            second = b;
        }
    }
  
    // Function to pre-calculate the left[] array
    static void preCalculate(int[] binary, int n,
                             pair[] left)
    {
        int count1 = 0, count0 = 0;
  
        // Iterate in the binary array
        for (int i = 0; i < n; i++) {
  
            // Initialize the number
            // of 1 and 0
            left[i].first = count1;
            left[i].second = count0;
  
            // Increase the count
            if (binary[i] != 0)
                count1++;
            else
                count0++;
        }
    }
  
    // Driver code
    public static void Main(String[] args)
    {
  
        int[] binary = { 1, 1, 1, 0, 0, 1, 0, 1, 1 };
        int n = binary.Length;
        pair[] left = new pair[n];
  
        for (int i = 0; i < n; i++)
            left[i] = new pair(0, 0);
  
        preCalculate(binary, n, left);
  
        // Queries
        int[] queries = { 0, 1, 2, 4 };
        int q = queries.Length;
  
        // Solve queries
        for (int i = 0; i < q; i++)
            Console.WriteLine(left[queries[i]].first + " ones "
                              + left[queries[i]].second + " zeros\n");
    }
}
  
// This code contributed by Rajput-Ji


PHP


输出:
0 ones 0 zeros
1 ones 0 zeros
2 ones 0 zeros
3 ones 1 zeros

时间复杂度:预计算为O(N),每个查询为O(1)。