-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray2D.java
More file actions
243 lines (208 loc) · 6.47 KB
/
Array2D.java
File metadata and controls
243 lines (208 loc) · 6.47 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* 2d array
import java.util.*;
public class Array2D {
public static boolean search(int matrix[][],int key){
for(int i=0;i<matrix.length;i++){
for(int j=0; j<matrix[0].length;j++){
if(matrix[i][j] == key){
System.out.println("founded at cell("+ i+","+ j+")");
return true;
}
}
}
System.out.println("key not found");
return false;
}
public static void main(String args[]){
int matrix[][]= new int[3][3];
int n =matrix.length, m = matrix[0].length;
Scanner sc = new Scanner(System.in);
for(int i=0;i<n;i++){
for(int j=0; j<m;j++){
matrix[i][j] = sc.nextInt();
}
}
//output
for(int i=0;i<n;i++){
for(int j=0; j<m;j++){
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
search(matrix, 7);
}
}
*/
/*
//Spiral matrix.....imp faang time complexity = O(N*M)
public class Array2D {
public static void printSpiral(int matrix[][]){
int startRow=0;
int startCol=0;
int endRow =matrix.length-1, endCol= matrix[0].length-1;
while(startRow<= endRow && startCol<= endCol){
//top
for(int j=startCol;j<=endCol;j++){
System.out.print(matrix[startRow][j]+" ");
}
//right
for(int i=startRow+1;i<=endRow;i++){
System.out.print(matrix[i][endCol]+" ");
}
//botttom
for(int j=endCol-1;j>=startCol;j--){
if(startRow==endRow){
break;
}
System.out.print(matrix[endRow][j]+" ");
}
//left
for(int i=endRow-1;i>=startRow+1;i--){
if(startCol==endCol){
break;
}
System.out.print(matrix[i][startCol]+" ");
}
startCol++;
startRow++;
endCol--;
endRow--;
}
System.out.println();
}
public static void main(String[] args) {
int matrix[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
printSpiral(matrix);
}
}
*/
/*
//Diagonal sum.......
public class Array2D {
public static int diagonalSum(int[][] matrix){
int sum=0;
//Brute force approach
// for(int i=0;i<matrix.length;i++){
// for(int j=0;j<matrix[0].length;j++){
// if(i==j){
// sum += matrix[i][j];
// }
// else if(i+j == matrix.length-1){
// sum += matrix[i][j];
// }
// }
// }
for(int i=0;i<matrix.length;i++){
//PD
sum += matrix[i][i];
//SD
if(i != matrix.length-1-i){
sum += matrix[i][matrix.length-1-i];
}
}
return sum;
}
public static void main(String[] args) {
int matrix[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
System.out.println(diagonalSum(matrix));
}
}
*/
// search in sorted matrix 2D array .....TC= O(N+M)
public class Array2D {
public static boolean stairCase(int matrix[][],int key){
// int row =0, col = matrix[0].length-1;
// while(row < matrix.length && col>=0){
// if(matrix[row][col]==key){
// System.out.println("found key at ("+row+","+col+")");
// return true;
// }
// else if(key < matrix[row][col]){
// col--;
// }
// else{
// row++;
// }
// }
// System.out.println("key not found");
// return false;
//another method...
int row = matrix.length-1, col = 0;
while (row>=0 & col < matrix[0].length){
if(matrix[row][col]==key){
System.out.println("found key at ("+row+","+col+")");
return true;
}
else if(key < matrix[row][col]){
row--;
}
else{
col++;
}
}
System.out.println("key not found");
return false;
}
public static void main(String[] args) {
int matrix[][]={{10,20,30,40},{15,25,35,45},{27,29,37,48},{32,33,39,50}};
stairCase(matrix, 30);
}
}
//3643. Flip Square Submatrix Vertically
class Solution {
public int[][] reverseSubmatrix(int[][] grid, int x, int y, int k) {
int sc = y, ec = y + k - 1, sr = x;
for (int j = sc; j <= ec; j++) {
for (int i = 0; i < k / 2; i++) {
int tmp = grid[sr+i][j];
grid[sr+i][j] = grid[sr+k-i-1][j];
grid[sr+k-i-1][j] = tmp;
}
}
return grid;
}
}
//LeetCode 1886 – Determine Whether Matrix Can Be Obtained By Rotation ✅
//Idea
//You can rotate the matrix 0°, 90°, 180°, 270° and check if it equals target.
//👉 If any rotation matches → return true
class Solution {
public boolean findRotation(int[][] mat, int[][] target) {
for (int i = 0; i < 4; i++) {
if (match(mat, target)) return true;
rotate(mat);
}
return false;
}
private void rotate(int[][] mat) {
int n = mat.length;
// transpose
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
// reverse rows
for (int i = 0; i < n; i++) {
int l = 0, r = n - 1;
while (l < r) {
int temp = mat[i][l];
mat[i][l] = mat[i][r];
mat[i][r] = temp;
l++;
r--;
}
}
}
private boolean match(int[][] a, int[][] b) {
int n = a.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] != b[i][j]) return false;
}
}
return true;
}
}