Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Experiment-1.

3
Student Name: Ujjwal. UID: 21BCS5375
Branch: BE-CSE Section/Group:902/A
th
Semester: 6
Subject Name: Advanced Programming lab-2 Subject Code:21CSP-351

Aim:

1. Problem statement - Design a class to find the kth largest element in a stream. Note that it is
the kth largest element in the sorted order, not the kth distinct element.

2. Problem statement – You are given an array of integers stones where stones[i] is the weight of the ith
stone. We are playing a game with the stones. On each turn, we choose the heaviest two stones and
smas them together.

Objective:
• KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of
integers nums. int add(int val) Appends the integer val to the stream and returns the element
representing the kth largest element in the stream
• Suppose the heaviest two stones have weights x and y with x <= y. The result of this smash
is: If x == y, both stones are destroyed, and If x != y, the stone of weight x is destroyed, and
the stone of weight y has new weight y - x. At the end of the game, there is at most one
stone left.
Code(A):
class KthLargest {

public:

priority_queue<int> minHeap; int


key;
KthLargest(int k, vector<int>& nums) {
cout<<"Shivam Kumar";
key=k; for(auto
elm: nums)
{ minHeap.push(-elm);
}
}
int add(int val) {
minHeap.push(-val);
while(minHeap.size()>key)
{ minHeap.pop(); }
return -minHeap.top();
}
};

Output(A):

Code(B):
class Solution { public: unordered_set<int>
helper(vector<int>& stones,int i,int j){ if(j>i){
if(j==i+1){ return {abs(stones[j]-
stones[i]),stones[j]+stones[i]};
} int mid=(i+j)/2;
unordered_set<int> p1=helper(stones,i,mid);
unordered_set<int> p2=helper(stones,mid+1,j);
unordered_set<int> ans; int maxi=0;
for(int num1:p1){ for(int num2:p2){
ans.insert(num1+num2);
ans.insert(abs(num1-num2));
}
} return
ans;
} return
{stones[i]};
} int lastStoneWeightII(vector<int>& stones) {
cout<<"Shivam kumar"; unordered_set<int>
ans=helper(stones,0,stones.size()-1); int sol=INT_MAX;
for(int num:ans)sol=min(sol,num); return sol;
} };
Output(B):

You might also like