-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_15_P_4.py
159 lines (115 loc) · 2.6 KB
/
Day_15_P_4.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'''
There are N players, played a game.
You are given the scores of the N players as scores[] array,
where i-th player score is score[i].
You are given P chances to modify the score of the players.
In each chance you can modify the score of i-th player as follows:
- You are allowed increment the score of i-th player by 1.
You have to perform these increments, in order to maximize the occurences of any score.
Your task is to maximize occurences of a score, after utlizing atmost P chances.
Input Format:
-------------
Line-1: Two space separated integers N and P, Players count, and number of chances.
Line-2: N space separated integers, scores of N players scores[].
Output Format:
--------------
Print an integer, maximum occurences of a score!
Sample Input-1:
---------------
5 3
2 3 5 6 9
Sample Output-1:
----------------
2
Sample Input-2
---------------
6 5
2 3 4 6 8 9
/*
Explination
1
3 3 4 6 8 9
2
4 3 4 6 8 9
3
4 4 4 6 8 9
4
4 4 4 7 8 9
5
4 4 4 8 8 9
*/
Sample Output-2:
----------------
3
'''
#SOLUTION
#CPP
'''
#include<bits/stdc++.h>
using namespace std;
int n,p;
int check(int mid,int i,vector<int>& temp,vector<int>& prefix){
if(mid==0){
if((temp[i]*(i-mid))-prefix[i-1]<=p){
return 1;
}
return 0;
}
if((temp[i]*(i-mid))-(prefix[i-1]-prefix[mid-1])<=p){
return 1;
}
return 0;
}
int main(){
cin>>n>>p;
vector<int> temp(n);
for(int i=0;i<n;i++){
cin>>temp[i];
}
sort(temp.begin(),temp.end());
vector<int> prefix(n,0);
prefix[0]=temp[0];
for(int i=1;i<n;i++){
prefix[i]=prefix[i-1]+temp[i];
}
int fans=1;
for(int i=1;i<n;i++){
int l=0;
int r=i-1;
int ans=-1;
while(l<=r){
int mid = (l+r)/2;
if(check(mid,i,temp,prefix)){
ans=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
if(ans!=-1){
fans=max(fans,i-ans+1);
}
}
cout<<fans<<endl;
}
'''
n,k=map(int,input().split())
nums=list(map(int,input().split()))[:k]
len_nums = len(nums)
nums.sort()
max_freq = 1
freq = 1
left = 0
ops = k
for right in range(1, len_nums):
ops -= (nums[right] - nums[right - 1]) * freq
freq += 1
if ops >= 0:
max_freq = max(max_freq, freq)
else:
while ops < 0:
ops += nums[right] - nums[left]
left += 1
freq -= 1
print(max_freq)