From 82ebc08eb5df2c37c5436fa628a5cb42d57ad567 Mon Sep 17 00:00:00 2001 From: knight1997 Date: Fri, 2 Oct 2020 14:14:59 +0530 Subject: [PATCH] Added solution of famous binary search problem in sopj --- SPOJ/AGGRESSIVECOWS.cpp | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 SPOJ/AGGRESSIVECOWS.cpp diff --git a/SPOJ/AGGRESSIVECOWS.cpp b/SPOJ/AGGRESSIVECOWS.cpp new file mode 100644 index 0000000..286c8f2 --- /dev/null +++ b/SPOJ/AGGRESSIVECOWS.cpp @@ -0,0 +1,50 @@ +#include +#include +using namespace std; +int a[1000000]; +bool solve(int mid,int c,int n) +{sort(a,a+n); +int count1=1,i; +int prev=a[0]; +for(i=1;i=mid) +{count1++; +prev=a[i]; +if(count1==c) +return true; +} +} +return false; +} +int main() { + int t; + cin>>t; + while(t--) + {int n,c; + cin>>n>>c; + int i; + for(i=0;i>a[i]; + } + int low=0,ans=0; + int high = a[n-1]; + while(low<=high) + {int mid = low + (high-low)/2; + if(solve(mid,c,n)) + {ans = mid; + low = mid+1; + } + else + { + high = mid-1; + } + } + cout<