Skip to content

Commit 06286f4

Browse files
prateek27prateek27
authored andcommitted
added stl code
1 parent 9aa9b06 commit 06286f4

File tree

114 files changed

+2731
-11
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+2731
-11
lines changed

.DS_Store

2 KB
Binary file not shown.

03 Programming Fundamentals-II/prime_number.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,24 @@
22
using namespace std;
33

44

5-
int main(){
5+
int main() {
66

77
//Check if a given number is Prime
88
int n;
9-
cin>>n;
9+
cin >> n;
1010

1111
int i;
1212

13-
for(i=2;i<=n-1;i++){
14-
if(n%i==0){
15-
cout<<n <<"is not Prime"<<endl;
13+
for (i = 2; i <= n - 1; i++) {
14+
if (n % i == 0) {
15+
cout << n << "is not Prime" << endl;
1616
break;
1717
}
1818
}
1919
//Remember this check is important
20-
if(i==n){
21-
cout<<n <<" is prime!"<<endl;
20+
if (i == n) {
21+
cout << n << " is prime!" << endl;
2222
}
2323

24-
25-
26-
27-
2824
return 0;
2925
}

10DaysOfCode/2_trailing_zeroes

9.92 KB
Binary file not shown.

10DaysOfCode/2_trailing_zeroes.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int findZeroes(int n) {
5+
int ans = 0;
6+
for (int D = 5; n / D >= 1; D = D * 5) {
7+
ans += n / D;
8+
}
9+
return ans;
10+
}
11+
12+
int main() {
13+
14+
long long int n;
15+
cin >> n;
16+
17+
cout << findZeroes(n) << endl;
18+
19+
20+
return 0;
21+
}

10DaysOfCode/div_subarrays

10.1 KB
Binary file not shown.

10DaysOfCode/div_subarrays.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
5+
#define ll long
6+
7+
ll a[1000005], pre[1000005];
8+
9+
int main() {
10+
11+
int t;
12+
cin >> t;
13+
while (t--) {
14+
int n;
15+
cin >> n;
16+
memset(pre, 0, sizeof(pre));
17+
18+
pre[0] = 1;
19+
//read the input
20+
int sum = 0;
21+
for (int i = 0; i < n; i++) {
22+
cin >> a[i];
23+
sum += a[i];
24+
sum %= n;
25+
sum = (sum + n) % n;
26+
pre[sum]++;
27+
}
28+
ll ans = 0;
29+
for (int i = 0; i < n; i++) {
30+
int m = pre[i];
31+
ans += (m) * (m - 1) / 2;
32+
} di
33+
cout << ans << endl;
34+
}
35+
36+
return 0;
37+
}

10DaysOfCode/ganesha

18.7 KB
Binary file not shown.

10DaysOfCode/ganesha.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
5+
6+
7+
int main() {
8+
9+
int n;
10+
cin >> n;
11+
12+
//print pattern
13+
//First Component
14+
cout << "*";
15+
for (int i = 1; i <= (n - 3) / 2; i++) {
16+
cout << " ";
17+
}
18+
for (int i = 1; i <= (n + 1) / 2; i++) {
19+
cout << "*";
20+
}
21+
cout << endl;
22+
23+
//Second Component
24+
for (int rows = 1; rows <= (n - 3) / 2; rows++) {
25+
cout << "*";
26+
//spaces
27+
for (int i = 1; i <= (n - 3) / 2; i++) {
28+
cout << " ";
29+
}
30+
cout << "*" << endl;
31+
}
32+
33+
// Third Component
34+
for (int i = 1; i <= n; i++) {
35+
cout << "*";
36+
}
37+
cout << endl;
38+
39+
//Fourth Component
40+
for (int rows = 1; rows <= (n - 3) / 2; rows++) {
41+
42+
//spaces
43+
for (int i = 1; i <= (n - 3) / 2 + 1; i++) {
44+
cout << " ";
45+
}
46+
47+
48+
//star
49+
cout << "*";
50+
51+
//spaces
52+
for (int i = 1; i <= (n - 3) / 2; i++) {
53+
cout << " ";
54+
}
55+
56+
//star
57+
cout << "*" << endl;
58+
59+
}
60+
61+
//fifth component
62+
//stars
63+
for (int i = 1; i <= (n + 1) / 2; i++) {
64+
cout << "*";
65+
}
66+
67+
//spaces
68+
for (int i = 1; i <= (n - 3) / 2; i++) {
69+
cout << " ";
70+
}
71+
72+
73+
//single star
74+
cout << "*" << endl;
75+
76+
77+
78+
79+
return 0;
80+
81+
82+
}

10DaysOfCode/magical_park

19.9 KB
Binary file not shown.

10DaysOfCode/magical_park.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
void magical_park(char a[][100], int m, int n, int k, int s) {
5+
6+
7+
//piyush can get out of the mark
8+
bool success = true;
9+
10+
for (int i = 0; i < m; i++) {
11+
for (int j = 0; j < n; j++) {
12+
char ch = a[i][j];
13+
14+
//check
15+
if (s < k) {
16+
success = false;
17+
break;
18+
}
19+
if (ch == '*') {
20+
s += 5;
21+
}
22+
else if (ch == '.') {
23+
s -= 2;
24+
}
25+
else {
26+
break;
27+
}
28+
//we also loose 1 point when we move right except for the last column
29+
if (j != n - 1) {
30+
s--;
31+
}
32+
}
33+
}
34+
if (success) {
35+
cout << "Yes" << endl;
36+
cout << s << endl;
37+
}
38+
else {
39+
cout << "No" << endl;
40+
}
41+
}
42+
43+
44+
int main() {
45+
46+
int m, n, k, s;
47+
cin >> m >> n >> k >> s;
48+
49+
50+
char park[100][100];
51+
//Take Input
52+
for (int i = 0; i < m; i++) {
53+
for (int j = 0; j < n; j++) {
54+
cin >> park[i][j];
55+
}
56+
}
57+
magical_park(park, m, n, k, s);
58+
59+
return 0;
60+
}

10DaysOfCode/park.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
5+
int main() {
6+
7+
8+
9+
10+
return 0;
11+
}

10DaysOfCode/prime_numbers_sieve

18.6 KB
Binary file not shown.

10DaysOfCode/prime_numbers_sieve.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
#define ll long long
5+
6+
//Naive Approach O(sqrtN)
7+
bool isPrime(int n) {
8+
if (n == 1) {
9+
return false;
10+
}
11+
if (n == 2) {
12+
return true;
13+
}
14+
for (int i = 2; i * i < n; i++) {
15+
if (n % i == 0)
16+
return false;
17+
}
18+
return true;
19+
}
20+
21+
//Sieve Approach - Generate a array containing prime Numbers
22+
void prime_sieve(int *p) {
23+
24+
//first mark all odd number's prime
25+
for (int i = 3; i <= 1000000; i += 2) {
26+
p[i] = 1;
27+
}
28+
29+
// Sieve
30+
for (ll i = 3; i <= 1000000; i += 2) {
31+
//if the current number is not marked (it is prime)
32+
if (p[i] == 1) {
33+
//mark all the multiples of i as not prime
34+
for (ll j = i * i; j <= 1000000; j = j + i ) {
35+
p[j] = 0;
36+
}
37+
}
38+
39+
}
40+
//special case
41+
p[2] = 1;
42+
p[1] = p[0] = 0;
43+
44+
}
45+
46+
47+
48+
int main() {
49+
50+
int n;
51+
cin >> n;
52+
53+
int p[1000005] = {0};
54+
prime_sieve(p);
55+
56+
//lets print primes upto range n
57+
for (int i = 0; i <= n; i++) {
58+
if (p[i] == 1) {
59+
cout << i << " ";
60+
}
61+
}
62+
63+
64+
65+
66+
return 0;
67+
}

10DaysOfCode/prime_visits

10.1 KB
Binary file not shown.

10DaysOfCode/prime_visits.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
#define ll long long
5+
6+
7+
//Sieve Approach - Generate a array containing prime Numbers
8+
void prime_sieve(int *p) {
9+
10+
//first mark all odd number's prime
11+
for (int i = 3; i <= 1000000; i += 2) {
12+
p[i] = 1;
13+
}
14+
15+
// Sieve
16+
for (ll i = 3; i <= 1000000; i += 2) {
17+
//if the current number is not marked (it is prime)
18+
if (p[i] == 1) {
19+
//mark all the multiples of i as not prime
20+
for (ll j = i * i; j <= 1000000; j = j + i ) {
21+
p[j] = 0;
22+
}
23+
}
24+
25+
}
26+
//special case
27+
p[2] = 1;
28+
p[1] = p[0] = 0;
29+
30+
}
31+
32+
33+
34+
int main() {
35+
36+
int p[1000005] = {0};
37+
prime_sieve(p);
38+
39+
40+
int csum[1000005] = {0};
41+
42+
//precompute the primes upto an index i
43+
44+
for (int i = 1; i <= 1000000; i++) {
45+
csum[i] = csum[i - 1] + p[i];
46+
}
47+
int q;
48+
cin >> q;
49+
while (q--) {
50+
int a, b;
51+
cin >> a >> b;
52+
cout << csum[b] - csum[a - 1] << endl;
53+
54+
}
55+
56+
57+
return 0;
58+
}

10DaysOfCode/rotate_image

19.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)