We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Btw, it's also possible to pass test cases in this perticular problem using Binary Search. just find the max possible i between 1 and n/2, where, i.(n-i) <= n.k and the answer is two times that. also need to take care of the edge case of n/2 . (n - n/2) <= n . k.
#include<iostream>#define forn(i,e) for(ll i = 0; i < e; i++)#define forsn(i,s,e) for(ll i = s; i < e; i++)#define rforn(i,s) for(ll i = s; ~i; i--)#define pb push_back#define ff first#define ss secondusingnamespacestd;typedeflonglongll;constllMOD=1000000007;voidsolve(){lln,k;cin>>n>>k;if(n/2*(n-n/2)<=n*k||n==1){cout<<n-1<<'\n';return;}lll=1,r=n/2;while(l<=r){llmid=l+(r-l)/2;if(mid*(n-mid)<=n*k)l=mid+1;elser=mid-1;}cout<<2*r<<'\n';}intmain(){#ifdef k4droid3freopen("input.txt","r",stdin);#endifios_base::sync_with_stdio(false);cin.tie(NULL);intt;cin>>t;while(t--)solve();return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Counting
You are viewing a single comment's thread. Return to all comments →
Btw, it's also possible to pass test cases in this perticular problem using Binary Search. just find the max possible i between 1 and n/2, where, i.(n-i) <= n.k and the answer is two times that. also need to take care of the edge case of n/2 . (n - n/2) <= n . k.