You are given an N size list of prices of deo where the ith price denotes the price of ith deo. You are assigned the task of calculating the sum of prices.
You have been given queries. In each query, you will be given two integers and .
For each query, you have to find the sum of all prices of deos, which lies between to ( and inclusive).
Example:
Input: n = 10, a[n] = {5, 49837107, 7, 1480689971, 8, 1061802477, 4, 1408912643, 9, 1610949990},q = 10,queries = {{4, 6}, {6, 7}, {3, 5}, {7, 9}, {3, 6}, {4, 4}, {3, 7}, {4, 5}, {2, 6}, {6, 8}}
Output:
9 7 9 24 9 4 16 9 9 15
Approach:
C++
#include <bits/stdc++.h>using namespace std;void specialPrice(int n, int a[], int q,vector<vector<int>> &queries){long long arr[100001] = {0};for (int i = 0; i < n; i++){int val = a[i];if (val < 100001){arr[val] += val;}}for (int i = 1; i <= 100000; i++){arr[i] += arr[i - 1];}for (int i = 0; i < q; i++){int p1 = queries[i][0];int p2 = queries[i][1];if (p1 < 1)cout << arr[p2] << "\n";elsecout << (arr[p2] - arr[p1 - 1]) << "\n";}}int main(){int n = 10;int a[n] = {5, 49837107, 7, 1480689971,8, 1061802477, 4, 1408912643,9, 1610949990};int q = 10;vector<vector<int>> queries = {{4, 6},{6, 7},{3, 5},{7, 9},{3, 6},{4, 4},{3, 7},{4, 5},{2, 6},{6, 8}};specialPrice(n, a, q, queries);return 0;}
No comments:
Post a Comment