# 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

 `// C++ implementation of the approach ` `#include ` `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); ` `    ``pair<``int``, ``int``> left[n]; ` `    ``preCalculate(binary, n, left); ` ` `  `    ``// Queries ` `    ``int` `queries[] = { 0, 1, 2, 4 }; ` `    ``int` `q = ``sizeof``(queries) / ``sizeof``(queries); ` ` `  `    ``// 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

 ` `

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.