-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatricesCC.java
More file actions
135 lines (121 loc) · 3.8 KB
/
Copy pathMatricesCC.java
File metadata and controls
135 lines (121 loc) · 3.8 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
public class MatricesCC {
public static void 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("Key found at " + i + "," + j);
}
}
}
}
public static void Spiral(int matrix[][]) {
int startRow = 0;
int startCol = 0;
int endRow = matrix.length - 1;
int 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] + " ");
}
// bottom
for (int j = endCol - 1; j >= startCol + 1; 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] + " ");
}
startRow++;
startCol++;
endRow--;
endCol--;
}
System.out.println();
}
public static int DiagonalSum1(int matrix[][]) {
int sum = 0;
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];
}
}
}
return sum;
}
public static int DiagonalSum2(int matrix[][]) {
int sum = 0;
for (int i = 0; i < matrix.length; i++) {
// pd
sum += matrix[i][i];
// sd
if (i != matrix.length - i - 1) {
sum += matrix[i][matrix.length - 1 - i];
}
}
return sum;
}
public static boolean StairSearch(int matrix[][], int key) { //O(n+m)
int row = 0, col = matrix[0].length - 1;
while (row < matrix.length && col > 0) {
if (matrix[row][col] == key) {
System.err.print("Key found at " + "(" + row + "," + col + ")");
return true;
}
else if (key < matrix[row][col]) {
col--;
}
else {
row++;
}
}
System.out.print("Key not found!");
return false;
}
public static void main(String[] args) {
/*
* int matrix1[][] = new int[3][3];
* int n = matrix1.length, m = matrix1[0].length;
*
* // input
* Scanner sc = new Scanner(System.in);
* for (int i = 0; i < m; i++) {
* for (int j = 0; j < n; j++) {
* matrix1[i][j] = sc.nextInt();
* }
* }
*
* System.out.print("Enter key:");
* int key = sc.nextInt();
*
* // output
* for (int i = 0; i < m; i++) {
* for (int j = 0; j < n; j++) {
* System.out.print(matrix1[i][j] + " ");
* }
* System.out.println();
* }
* search(matrix1, key);
*/
int matrix2[][] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
// Spiral(matrix2);
// System.out.print(DiagonalSum2(matrix2));
StairSearch(matrix2, 11);
}
}