-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxGroups.cpp
More file actions
54 lines (47 loc) · 796 Bytes
/
MaxGroups.cpp
File metadata and controls
54 lines (47 loc) · 796 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
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;
bool valid(int a[],int x,int k,int n)
{
long long s=0;
for(int i=1;i<=n;i++)
{
if(x<a[i])
s+=x;
else
s+=a[i];
}
if(s>=(x*k))
return true;
else
return false;
}
int maxgroup(int a[],int n,int k)
{
int group=1,mid;
int l=1;
long long t=0;
for(int i=1;i<=n;i++)
t+=a[i];
int h=t/k;
while(l<=h)
{
mid=(l+h)/2;
if(valid(a,mid,k,n))
{
group=mid;
l=mid+1;
}
else
h=mid-1;
}
return group;
}
int main()
{
int a[100],n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
cout<< maxgroup(a,n,k);
return 0;
}