Monk and Multiplication

The Monk learned about priority queues recently and asked his teacher for an interesting problem. So his teacher came up with a simple problem. He now has an integer array A. For each index i, he wants to find the product of the largest, second largest and the third-largest integer in the range [1,i].

Note: Two numbers can be the same value-wise but they should be distinct index-wise.

Example:

Input:  n = 5, a = [1,2,3,4,5]

Output:

-1 -1 6 24 60

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

void multiplication(long long nlong long a[])
{

    priority_queue<long longq;
    for (long long i = 0i < ni++)
    {
        q.push(a[i]);
        if (i < 2)
            cout << -1 << "\n";
        else
        {
            long long x = q.top();
            q.pop();
            long long y = q.top();
            q.pop();
            long long z = q.top();
            q.push(x);
            q.push(y);
            cout << x * y * z << "\n";
        }
    }
}
int main()
{
    long long n = 5;

    long long a[n] = {12345};

    multiplication(na);

    return 0;
}


No comments:

Post a Comment