Skip to content

Commit 2f81c09

Browse files
Merge pull request #3234 from cpinitiative/ryanchou-dev-patch-20
pov
2 parents 2eb0816 + 3419cf8 commit 2f81c09

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

solutions/cfgym-102951B

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
id: cfgym-102951B
3+
source: CF
4+
title: Studying Algorithms
5+
author: Atharv Jain
6+
---
7+
## Video Solution
8+
9+
By Atharv Jain
10+
11+
<Youtube id="wJKV92iDMpg" />
12+
13+
<Spoiler title="Video Solution Code">
14+
<LanguageSection>
15+
<CPPSection>
16+
17+
```cpp
18+
#include <bits/stdc++.h>
19+
using namespace std;
20+
21+
int main(){
22+
ios_base::sync_with_stdio(0); cin.tie(0);
23+
int n, x; cin >> n >> x;
24+
vector<int> a(n);
25+
for(int i=0; i<n; i++)
26+
cin >> a[i];
27+
sort(a.begin(), a.end());
28+
for(int i=0; i<n; i++){
29+
if(a[i]>x){
30+
cout << i << "\n";
31+
exit(0);
32+
}
33+
x-=a[i];
34+
}
35+
cout << n << "\n";
36+
}
37+
```
38+
39+
</CPPSection>
40+
<JavaSection>
41+
42+
```java
43+
import java.io.*;
44+
import java.util.*;
45+
46+
public class Main {
47+
int n, x;
48+
public static void main(String[] args) {
49+
Kattio io = new Kattio();
50+
int n = io.nextInt(), x = io.nextInt();
51+
int a[] = new int[n];
52+
for (int i = 0; i < n; i++)
53+
a[i] = io.nextInt();
54+
Arrays.sort(a);
55+
int ans=n;
56+
for (int i = 0; i < n; i++) {
57+
if(x < a[i]){
58+
ans = i;
59+
break;
60+
}
61+
x -= a[i];
62+
}
63+
io.println(ans);
64+
io.close();
65+
}
66+
//CodeSnip{Kattio}
67+
}
68+
```
69+
70+
</JavaSection>
71+
72+
<PySection>
73+
```py
74+
n, x = map(int, input().split())
75+
a = list(map(int, input().split()))
76+
a.sort()
77+
ans = n
78+
for i in range(n):
79+
if x < a[i]:
80+
ans = i
81+
break
82+
x -= a[i]
83+
print(ans)
84+
```
85+
</PySection>
86+
</LanguageSection>
87+
</Spoiler>

0 commit comments

Comments
 (0)