Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b5cdab8

Browse files
Update Distrributecandies.cpp
1 parent ea4117e commit b5cdab8

File tree

1 file changed

+29
-45
lines changed

1 file changed

+29
-45
lines changed

‎Searching and sorting applications/Distrributecandies.cpp‎

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,37 @@ Help Shaky in finding out what is the maximum number of candies which a student
1919
*/
2020

2121

22-
#include <bits/stdc++.h>
23-
22+
#include<bits/stdc++.h>
2423
using namespace std;
25-
int is_possible(vector<int> v, int k, int mid){
26-
int n = v.size();
27-
int givento = 0;
28-
29-
int i = 0;
30-
while(i<n){
31-
givento += v.at(i)/mid;
32-
33-
}
34-
35-
return(givento >= k);
36-
}
3724

38-
int go(vector<int> v, int k){
3925

40-
int n = v.size();
41-
sort(v.begin(), v.end());
26+
int go(int* arr, int k,int n){
27+
28+
sort(arr,arr+n);
4229

43-
int max_candies = v.at(n-1);
30+
int max_candies = arr[n-1];
4431
int min_candies = 0;
45-
int mid = (min_candies+max_candies)/2;
46-
47-
int i = 0;
48-
int ans = 0;
32+
int mid ;
33+
int ans = 1;
4934

35+
5036
while(max_candies>=min_candies){
51-
if (is_possible(v, k, mid))
52-
{
53-
ans = max(ans, mid);
54-
min_candies = mid+1;
55-
mid = (min_candies+max_candies)/2;
56-
57-
}else{
58-
max_candies = mid-1;
59-
mid = (min_candies+max_candies)/2;
60-
}
61-
}
37+
mid = (min_candies+max_candies)/2;
38+
int given=0;
39+
for(int i=0;i<n;i++)
40+
{
41+
given+=arr[i]/mid;
42+
}
43+
44+
if(given>=k){
45+
ans=max(ans,mid);
46+
min_candies=mid+1;
47+
}
48+
else{
49+
max_candies=mid-1;
50+
}
51+
52+
}
6253

6354
return ans;
6455

@@ -67,25 +58,18 @@ int go(vector<int> v, int k){
6758

6859

6960

70-
int main( int argc , char ** argv )
61+
int main( )
7162
{
72-
ios_base::sync_with_stdio(false) ;
73-
cin.tie(NULL) ;
74-
7563
int t;
7664
cin>>t;
7765

7866
while(t--){
7967
int n, k;
8068
cin>>n>>k;
81-
int m=n;
82-
std::vector<int> v;
83-
while(m--){
84-
int a;
85-
cin>>a;
86-
v.push_back(a);
87-
}
88-
cout << go(v,k) << '\n';
69+
int* arr=new int[n];
70+
for(int i=0;i<n;i++)
71+
cin>>arr[i];
72+
cout << go(arr,k,n) << endl;
8973
}
9074

9175
return 0 ;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /