Discuss a question to answer a query on a given array. For example, for each query index, we need to find the number of 1's and 0's to the left of the index.
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
A simple way to solve this problem is to iterate through the array to the index of the query and check each element; if it is 0, Then increment the zero counter by 1, otherwise increment the zero counter by 1.
#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; }
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
In the previous method, every time we start from the 0th index to calculate the new query 1 and 0.
Another way is to count 0 and 1 first. appears to the left of each index, stores them in an array, and returns the answer based on the index written in the query.
#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; }
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
In this tutorial we discussed about returning the index left for every query in a given array The number of 1's and 0's. We discussed simple and effective ways to solve this problem. We also discussed a C program to solve this problem and we can implement it using programming languages like C, Java, Python etc. We hope you found this tutorial helpful.
The above is the detailed content of Translate the following into Chinese: C++ Query to answer the number of 1's and 0's to the left of a given index. For more information, please follow other related articles on the PHP Chinese website!