Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
528 B
0
Indexable
Never
/* 
firsr put k subarrays sum into min heap. then put subarray sum only if it's greater than top of heap */
#include<queue>
#include<vector> // SAME LOGIC AS KTH LARGEST

int getKthLargest(vector<int> &arr, int k)
{
	priority_queue<int,vector<int>,greater<int>> pq; 
	for(int i=0;i<arr.size();i++){
		int sum=0;
		for(int j=i;j<arr.size();j++){
			sum+=arr[j];
		    if(pq.size()<k){
				pq.push(sum);
			}else{
				if(sum>pq.top()){
					pq.pop();
					pq.push(sum);
				}
			}
		}
	}
	return pq.top();
}