rishiilluri

Untitled

Aug 10th, 2024
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int fun(int n, int limit, vector<int>& v){
  7.     int low =1;
  8.     int high=v.size();
  9.     int ans=0;
  10.    
  11.     while(low<high){
  12.         int mid = low+(high-low)/2;
  13.         int idx=0;
  14.         float temp= (limit*1.0)/mid;
  15.         int flag=0;
  16.         // cout<<"mid "<<mid<<" "<<temp<<endl;
  17.         while(idx<v.size()){
  18.             int c=0;
  19.             while((1.0*v[idx])>temp){
  20.                 idx++;
  21.                 c++;
  22.                 // cout<<c<<endl;
  23.             }
  24.             if(c>=mid){
  25.                 flag=1;
  26.                 break;
  27.             }
  28.             idx++;
  29.         }
  30.        
  31.         if(flag){
  32.             ans=max(ans,mid);
  33.             low=mid+1;
  34.         }
  35.         else{
  36.             high=mid-1;
  37.         }
  38.         // cout<<low<<" " <<high<<endl;
  39.    
  40.     }
  41.     cout<<ans;
  42.     if(ans==0){
  43.         return -1;    
  44.     }
  45.     return ans;
  46. }
  47. int main()
  48. {
  49.     vector<int> v = {2};
  50.     fun(1,7,v);
  51.    
  52.    
  53.    
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment