Special Price

You are given an 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 Q queries. In each query, you will be given two integers P1 and P2.
For each query, you  have to find the sum of all prices of deos, which lies between P1 to P2 (P1 and P2 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 nint a[], int q,
                  vector<vector<int>> &queries)
{
    long long arr[100001] = {0};

    for (int i = 0i < ni++)
    {

        int val = a[i];
        if (val < 100001)
        {
            arr[val] += val;
        }
    }
    for (int i = 1i <= 100000i++)
    {
        arr[i] += arr[i - 1];
    }
    for (int i = 0i < qi++)
    {
        int p1 = queries[i][0];
        int p2 = queries[i][1];

        if (p1 < 1)
            cout << arr[p2<< "\n";
        else
            cout << (arr[p2] - arr[p1 - 1]) << "\n";
    }
}
int main()
{

    int n = 10;

    int a[n] = {54983710771480689971,
                8106180247741408912643,
                91610949990};

    int q = 10;
    vector<vector<int>> queries = {{46},
                                   {67},
                                   {35},
                                   {79},
                                   {36},
                                   {44},
                                   {37},
                                   {45},
                                   {26},
                                   {68}};

    specialPrice(naqqueries);

    return 0;
}


No comments:

Post a Comment