Queries to answer the number of ones and zero to the left of given index

Given a binary array and Q queries. Every query consists of a number K, the task is to print the number of ones and zeros to the left of the index K.

Examples:

Input: arr[] = {1, 1, 1, 0, 0, 1, 0, 1, 1}, Q[] = {0, 1, 2, 4}
Output:
0 ones 0 zeros
1 ones 0 zeros
2 ones 0 zeros
3 ones 1 zeros



Input: arr[] = {1, 0, 1, 0, 1, 1}, Q[] = {2, 3}
Output:
1 ones 1 zeros
2 ones 1 zeros

Approach: The following steps can be followed to solve the above problem:

  • Declare an array of pairs(say left[]) which will be used to pre-calculate the number of ones and zeros to the left.
  • Iterate on the array and in every step initialize left[i] with the number of ones and zeros to the left.
  • The number of ones for every query will be left[k].first and number of zeros will be left[k].second

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to pre-calculate the left[] array
void preCalculate(int binary[], int n, pair<int, int> 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<int, int> 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach 
  
// Function to pre-calculate the left[] array 
function preCalculate($binary, $n
    $left = array();
    $count1 = 0; $count0 = 0; 
  
    // Iterate in the binary array 
    for ($i = 0; $i < $n; $i++)
    
  
        // Initialize the number 
        // of 1 and 0 
        $left[$i] = array($count1
                          $count0);
  
        // Increase the count 
        if ($binary[$i]) 
            $count1++; 
        else
            $count0++; 
    
    return $left;
  
// Driver code 
$binary = array( 1, 1, 1, 0, 0, 1, 0, 1, 1 ); 
$n = count($binary);
  
$left = preCalculate($binary, $n); 
  
// Queries 
$queries = array( 0, 1, 2, 4 ); 
$q = count($queries);
  
// Solve queries 
for ($i = 0; $i < $q; $i++) 
    echo $left[$queries[$i]][0], " ones "
         $left[$queries[$i]][1], " zeros\n"
  
// This code is contributed by Ryuga 
?>

chevron_right


Output:

0 ones 0 zeros
1 ones 0 zeros
2 ones 0 zeros
3 ones 1 zeros

Time Complexity: O(N) for pre-computation and O(1) for every query.



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.