-
Notifications
You must be signed in to change notification settings - Fork 0
/
p13_2D_arrays.cpp
180 lines (160 loc) · 3.14 KB
/
p13_2D_arrays.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// 2D Arrays/Matrices
//https://www.simplilearn.com/tutorials/data-structure-tutorial/two-dimensional-arrays
//Thus, simple 1D array can be used to store 2D data by mapping the each element in the array to a position in the matrix
// P-1 I/P & O/P a 2D array
/*
#include<iostream>
using namespace std;
int main()
{
int arr[3][4];
//input 2d array
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
cin>>arr[i][j];
}
}
//output 2d array
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
}
*/
// O/P
// 1 2 3 4
// 5 6 7 8
// 9 10 11 12
// for column wise i/p
/*
#include<iostream>
using namespace std;
int main()
{
int arr[3][4];
//input 2d array
for(int j=0;j<4;j++){
for(int i=0;i<3;i++){
cin>>arr[i][j];
}
}
//output 2d array
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
}*/
// o/p
// 1 4 7 10
// 2 5 8 11
// 3 6 9 12
//static initialisation- arr[3][3]={1,2,3,4,5,6,7,8,9} -- default row wise arrangement
// OR arr[4][2]={{1,2},{3,4},{7,6},{7,8}} -- -//-
// totalRow= arr.size()
// totalCol= arr[0].size()
//P-2 search in matrix
/*
#include<iostream>
using namespace std;
bool search(int ip, int arr[][2], int a, int b){ //must provide atleast noof colms
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
if (arr[i][j]==ip){
return true;
}
}
}
return false;
}
int main()
{
int arr[4][2]={{1,2},{3,4},{7,6},{7,8}};
//i/p
int ip;
cin>>ip;
cout<<search(ip,arr,4,2);
}
*/
//TC- O(row*col)
//P-3 sum of each row
/*
#include<iostream>
using namespace std;
int main()
{
int arr[4][2]={{1,2},{3,4},{7,6},{7,8}};
int rowSum;
for(int i=0;i<4;i++){
rowSum=0;
for(int j=0;j<2;j++){
rowSum+= arr[i][j];
}
cout<<rowSum<<endl;
}
}*/
//sum of each column
/*
#include<iostream>
using namespace std;
int main()
{
int arr[4][2]={{1,2},{3,4},{7,6},{7,8}};
int colSum;
for(int j=0;j<2;j++){
colSum=0;
for(int i=0;i<4;i++){
colSum+= arr[i][j];
}
cout<<colSum<<endl;
}
}
*/
//P-4 row with largest sum
/*
#include<iostream>
using namespace std;
int main()
{
int arr[4][2]={{1,2},{3,4},{7,6},{7,8}};
int rowSum,maxRow,maxSum;
for(int i=0;i<4;i++){
rowSum=0;
maxSum=-1;
for(int j=0;j<2;j++){
rowSum+= arr[i][j];
}
if(rowSum>maxSum){
maxSum=rowSum;
maxRow=i;
}
}
cout<<maxRow;
}
*/
//P-5 print like a wave
//link- https://www.codingninjas.com/studio/problems/print-like-a-wave_893268?utm_source=youtube&utm_medium=affiliate&utm_campaign=love_babbar_6
//TC- O(row*col) i.e. O(N)
//P-6 spiral matrix
//LC-54
// beautiful prob, beautiful soln
//TC- O(count) i.e. O(N)
//P-7 Rotate matrix
//LC= 48
//TC- O(row*col) + O(row*column/2) i.e. O(N)
// ^ for reverse
//P-8 Binary search in 2D array
//LC- 74
//TC- O(log(row*col))
//P-9 Binary Search in 2D array ||
//LC- 240
//TC- O(log(row*col))
//P-10 Pouring problem
//Link- https://www.geeksforgeeks.org/find-water-in-a-glass/
//LC-118 Pascal's Triangle
//P-11 Set Matrix Zeroes
//LC-73
//⭐LC-1074. Number of Submatrices That Sum to Target