forked from sanghaisubham/Algorithmic-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscrete_BinarySearch.cpp
More file actions
42 lines (41 loc) · 828 Bytes
/
Discrete_BinarySearch.cpp
File metadata and controls
42 lines (41 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,c;
cin>>n>>c;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
int ans=0;
int start=0;
int ends=a[n-1];
while(start<=ends)
{
int mid=(start+ends)/2;
int k=a[0];
int count1=1;
for(int i=1;i<n;i++)
{
if((a[i]-k)>=mid)
{
count1++;
k=a[i];
}
}
if(count1>=c)
{
ans=max(ans,mid);
start=mid+1;
}
else
ends=mid-1;
}
cout<<ans<<endl;
}
}