dimanche 18 avril 2021

Getting Wrong Answer In a Geeks for geesk question

Here is the link of the problem Click Here

This is my code that is getting error

int KthLargest(int arr[], int n, int k)
{
    
   vector<int>v(arr,arr+k);
   
   
   
   
   // I made a minHeap time required = o(n)
   make_heap(v.begin(),v.end(),greater<int>());
   
   for(int i=k;i<n;i++)
   {
     if(arr[i]<v.front())
      continue;
     else
      {
        v.front() = arr[i];
        push_heap(v.begin(),v.end(),greater<int>());
      }
      
   }
   
 
  
   int ans = v.front();
   
   return ans;
}

Test case for which it is showing error is :

5 4 3 5 4 2 9

Aucun commentaire:

Enregistrer un commentaire