C++ Queries to Answer the Number of Ones and Zeros to the Left of Given Index


Discuss a problem to answer queries to the given array. For each query index, we need to find the number of ones and zeros to the left of the index, for example.

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

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

Approach to Find the Solution

Naive Approach

The simple solution to this problem is traversing through the array to the index of the query and checking each element; if it is 0, then increment zero counter by 1 else, increment the ones counter by 1.

Example

#include <bits/stdc++.h>
using namespace std;
int main(){
    int nums[] = {1, 0, 0, 1, 1, 0, 0, 1, 0, 0};
    int queries[] =  { 2, 4, 1, 0, 5 };
    int qsize = sizeof(queries) / sizeof(queries[0]);
    int zeros=0,ones=0;
    // loop for running each query.
    for(int i = 0;i<qsize;i++){
        //counting zeros and ones
        for(int j = 0;j<queries[i];j++){
            if(nums[j]==0)
            zeros++;
            else
            ones++;
        }
        cout << "\nquery " << i+1 << ": zeros = " << zeros << ",ones = " << ones;
        zeros=0;
        ones=0;
    }
    return 0;
}

Output

query 1: zeros = 1,ones = 1
query 2: zeros = 2,ones = 2
query 3: zeros = 0,ones = 1
query 4: zeros = 0,ones = 0
query 5: zeros = 2,ones = 3

Efficient Approach

In the previous approach, every time, we were calculating ones and zeros for new queries from the 0th index.

Another approach is first to calculate zeros and ones present on the left of every index, store them in an array, and return the answer according to the index written in the query.

Example

#include <bits/stdc++.h>
using namespace std;
int main(){
    int nums[] = {1, 0, 0, 1, 1, 0, 0, 1, 0, 0};
    int queries[] =  { 2, 4, 1, 0, 5 };
    int n = sizeof(nums) / sizeof(nums[0]);
    int arr[n][2];
    int zeros = 0, ones = 0;
    // traverse through the nums array.
    for (int i = 0; i < n; i++) {
        // store the number of zeros and ones in arr.
        arr[i][0] = zeros;
        arr[i][1] = ones;
        // increment variable according to condition
        if (nums[i]==0)
            zeros++;
        else
            ones++;
    }
    int qsize = sizeof(queries) / sizeof(queries[0]);
        for (int i = 0; i < qsize; i++)
        cout << "\nquery " << i+1 << ": zeros = " << arr[queries[i]][0] << ",ones ="    << arr[queries[i]][1];
    return 0;
}

Output

query 1: zeros = 1,ones =1
query 2: zeros = 2,ones =2
query 3: zeros = 0,ones =1
query 4: zeros = 0,ones =0
query 5: zeros = 2,ones =3

Conclusion

In this tutorial, we discussed returning the number of ones and zeros to the left of the index for each query in the given array. We discussed a simple approach and an efficient approach to solve this problem. We also discussed the C++ program for this problem which we can do with programming languages like C, Java, Python, etc. We hope you find this tutorial helpful.

Updated on: 26-Nov-2021

134 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements