
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
- Related Articles
- Queries for bitwise AND in the index range [L, R] of the given Array using C++
- Create an array with ones at and below the given diagonal and zeros elsewhere in Numpy
- Queries to update a given index and find gcd in range in C++
- Return a 2-D array with ones on the diagonal and zeros elsewhere and also set the number of columns in Numpy
- Queries for Bitwise OR in the Index Range [L, R] of the Given Array using C++
- Maximum difference of zeros and ones in binary string in C++
- Queries to update a given index and find gcd in range in C++ Program
- Check if the given array can be reduced to zeros with the given operation performed given number of times in Python
- Move all zeros to start and ones to end in an Array of random integers in C++
- Number of Ones in the Smallest repunit
- MySQL number-string formatting to pad zeros on the left of a string with numbers after a slash
- Write the number of thousands, hundreds, tens and ones .ThousandsHundredstensones4625583468318743
- 8085 program to count the number of ones in contents of register B
- Number of leading zeros in binary representation of a given number in C++
- Create an array with ones at and below the given diagonal and zeros elsewhere with a different output type in Numpy
