Skip to content

Commit 0991129

Browse files
Merge pull request #26 from naaam-h-siddhu/main
organized CPP code and added new folder wise code
2 parents e7bcc89 + 9f7d267 commit 0991129

File tree

139 files changed

+3342
-365
lines changed

Some content is hidden

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

139 files changed

+3342
-365
lines changed
File renamed without changes.
File renamed without changes.

CPP/maze_game.c renamed to CPP/Problems/maze_game.c

Lines changed: 365 additions & 365 deletions
Large diffs are not rendered by default.

CPP/array-2d/all_pairs.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int all_pair(int arr[], int n){
4+
for (int i = 0; i < n; ++i)
5+
{
6+
for (int j=i+1 ; j < n; ++j)
7+
{
8+
cout<<arr[i]<<", "<<arr[j]<<endl;
9+
}
10+
}
11+
return 0;
12+
}
13+
14+
int main(){
15+
int n;
16+
cin>>n;
17+
int arr[n];
18+
for (int i = 0; i < n; ++i)
19+
{
20+
cin>>arr[i];
21+
}
22+
all_pair(arr, n);
23+
24+
return 0;
25+
}

CPP/array-2d/array.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int main(){
4+
int n,m;
5+
cin>>n>>m;
6+
// char arr[n];
7+
// for (int i = 0; i < n; ++i)
8+
// {
9+
// cin>>arr[i];
10+
// }
11+
// for (int i = 0; i < n; ++i)
12+
// {
13+
// cout<<arr[i];
14+
// }
15+
16+
17+
//2D array
18+
int arr[n][m];
19+
for (int i = 0; i < n; ++i)
20+
{
21+
for (int j = 0; j < m; ++j)
22+
{
23+
cin>>arr[i][j];
24+
}
25+
}
26+
for (int i = 0; i < n; ++i)
27+
{
28+
for (int j = 0; j < m; ++j)
29+
{
30+
cout<<arr[i][j]<<" ";
31+
}
32+
cout<<endl;
33+
}
34+
return 0;
35+
}

CPP/array-2d/brute_force.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int largestsubarray_sum(int arr[],int n){
4+
int largest_sum;
5+
for (int i = 0; i < n; ++i)
6+
{
7+
for (int j = i; j < n; ++j)
8+
{
9+
int super_sum=0;
10+
for (int k = i; k <= j; ++k)
11+
{
12+
super_sum+=arr[k];
13+
}
14+
largest_sum= max(largest_sum,super_sum);
15+
16+
}
17+
}
18+
return largest_sum;
19+
}
20+
void initial(){
21+
#ifndef ONLINE_JUDGE
22+
freopen("input.txt", "r", stdin);
23+
freopen("output.txt", "w", stdout);
24+
#endif
25+
}
26+
int main(){
27+
initial();
28+
int n;
29+
cin>>n;
30+
int arr[n];
31+
for (int i = 0; i < n; ++i)
32+
{
33+
cin>>arr[i];
34+
}
35+
cout<<largestsubarray_sum(arr,n);
36+
37+
return 0;
38+
}

CPP/array-2d/inpout.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define ll long long
5+
#define pb push_back
6+
#define mod 1000000007
7+
#define endl '\n'
8+
void printarr(int arr[][1000],int n,int m){
9+
for (int i = 0; i < n; i++) {
10+
for (int j = 0; j < m; j++) {
11+
cout<<arr[i][j]<<" ";
12+
}
13+
cout<<endl;
14+
}
15+
}
16+
int main(){
17+
cin.tie(0)->sync_with_stdio(0);
18+
int arr[1000][1000];
19+
int n,m;
20+
cin >>n>>m;
21+
for (int i = 0; i < n; i++) {
22+
for (int j = 0; j < m; j++) {
23+
cin>>arr[i][j];
24+
}
25+
26+
}
27+
printarr(arr,n,m);
28+
return 0;
29+
}
30+
31+

CPP/array-2d/kadane_algo.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int kadane(int arr[], int n){
4+
int current=0;
5+
int largest=0;
6+
for (int i = 0; i < n; ++i)
7+
{
8+
current+=arr[i];
9+
if(current<0){
10+
current=0;
11+
}
12+
largest= max(current,largest);
13+
}
14+
return largest;
15+
}
16+
void initial(){
17+
#ifndef ONLINE_JUDGE
18+
freopen("input.txt", "r", stdin);
19+
freopen("output.txt", "w", stdout);
20+
#endif
21+
}
22+
int main(){
23+
24+
int n;
25+
cin>>n;
26+
int arr[n];
27+
for (int i = 0; i < n; ++i)
28+
{
29+
cin>>arr[i];
30+
}
31+
cout<<kadane(arr, n)<<endl;
32+
33+
34+
35+
36+
return 0;
37+
}

CPP/array-2d/max_array.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// #include<bits/stdc++.h>
2+
// using namespace std;
3+
// void initial(){
4+
// #ifndef ONLINE_JUDGE
5+
// freopen("input.txt", "r", stdin);
6+
// freopen("output.txt", "w", stdout);
7+
// #endif
8+
// }
9+
// int main(){
10+
// initial();
11+
// int a[]={112,113,44,555,77665,787654};
12+
// int n= sizeof(a)/ sizeof(int);
13+
// cout<<"maximum ="<<*max_element(a,a+n);
14+
// return 0;
15+
// }
16+
17+
18+
19+
20+
#include<bits/stdc++.h>
21+
using namespace std;
22+
int max_arr(int arr[],int n){
23+
int max=arr[0];
24+
25+
for (int i = 1; i < n; ++i)
26+
{
27+
if (max<arr[i])
28+
{
29+
max=arr[i];
30+
}
31+
32+
}
33+
cout<<max<<endl;
34+
35+
return 0 ;
36+
}
37+
void initial(){
38+
#ifndef ONLINE_JUDGE
39+
freopen("input.txt", "r", stdin);
40+
freopen("output.txt", "w", stdout);
41+
#endif
42+
}
43+
int main(){
44+
initial();
45+
int arr[]={12,13,14,15,17776,177};
46+
int n = sizeof(arr)/ sizeof(int );
47+
48+
49+
50+
51+
52+
max_arr(arr,n);
53+
return 0;
54+
}

CPP/array-2d/prefix_2d_lr.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
const int n= 1e3+ 10;
4+
int arr[n][n];
5+
long long pr[n][n];
6+
int main(){
7+
int arr[n][n];
8+
9+
int n;
10+
cin>>n;
11+
for (int i = 1; i <= n; ++i)
12+
{
13+
for (int j = 1; j <=n; ++j)
14+
{
15+
cin>>arr[i][j];
16+
pr[i][j]=arr[i][j]+pr[i-1][j]+pr[i][j-1]-pr[i-1][j-1];
17+
}
18+
}
19+
int t;
20+
cin>>t;
21+
while(t--){
22+
int a,b,c,d;
23+
cin>>a>>b>>c>>d;
24+
25+
cout<<pr[c][d]-pr[a-1][d]-pr[c][b-1]+pr[a-1][b-1]<<endl;
26+
27+
}
28+
29+
30+
return 0;
31+
}

CPP/array-2d/prefix_lr.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<bits/stdc++.h>
2+
#define ll long long
3+
#define pb push_back
4+
#define fr(a,b) for(int i=a;i<b;i++)
5+
#define vfr(i,a,b) for(int i=a;i<b;i++)
6+
#define mod 1000000007
7+
#define read(x) int x;cin>>x
8+
const int N=1e5+10;
9+
int arr[N];
10+
11+
using namespace std;
12+
int main(){
13+
read(n);
14+
for (int i = 1; i <=n; ++i)
15+
{
16+
cin>>arr[i];
17+
}
18+
read(t);
19+
while(t--){
20+
read(l);read(r);
21+
ll sum=0;
22+
for (int i = l; i <= r; ++i)
23+
{
24+
sum+=arr[i];
25+
}
26+
cout<<sum<<endl;
27+
}
28+
29+
return 0;
30+
}

CPP/array-2d/prefix_sum.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int prefix_sum(int arr[],int n){
4+
int largest_sum;
5+
for (int i = 0; i < n; ++i)
6+
{
7+
for (int j = i; j < n; ++j)
8+
{
9+
int super_sum=0;
10+
for (int k = i; k <= j; ++k)
11+
{
12+
super_sum+=arr[k];
13+
}
14+
largest_sum= max(largest_sum,super_sum);
15+
16+
}
17+
}
18+
return largest_sum;
19+
}ii
20+
void initial(){
21+
#ifndef ONLINE_JUDGE
22+
freopen("input.txt", "r", stdin);
23+
freopen("output.txt", "w", stdout);
24+
#endif
25+
}
26+
int main(){
27+
initial();
28+
int n;
29+
cin>>n;
30+
int arr[n];
31+
for (int i = 0; i < n; ++i)
32+
{
33+
cin>>arr[i];
34+
}
35+
cout<<prefix_sum(arr,n);
36+
37+
return 0;
38+
}

CPP/array-2d/reverse_array.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int reverse(int arr[], int n){
4+
int s=0;
5+
int e= n-1;
6+
while(s<=e){
7+
swap(arr[s],arr[e]);
8+
s+=1;
9+
e-=1;
10+
}
11+
return 0;
12+
}
13+
void initial(){
14+
#ifndef ONLINE_JUDGE
15+
freopen("input.txt", "r", stdin);
16+
freopen("output.txt", "w", stdout);
17+
#endif
18+
}
19+
int main(){
20+
initial();
21+
int arr[]={12,55,66,77,97,987};
22+
int n= sizeof(arr)/ sizeof(int);
23+
reverse(arr,n);
24+
for (int i = 0; i < n; ++i)
25+
{
26+
cout<<arr[i]<<endl;
27+
}
28+
29+
30+
return 0;
31+
}

0 commit comments

Comments
 (0)