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 n, long long a[]){priority_queue<long long> q;for (long long i = 0; i < n; i++){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] = {1, 2, 3, 4, 5};multiplication(n, a);return 0;}
No comments:
Post a Comment