-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrices.java
More file actions
178 lines (147 loc) · 4.75 KB
/
Matrices.java
File metadata and controls
178 lines (147 loc) · 4.75 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
import java.util.*;
public class Matrices {
// Search key in matrix
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("Found at cell (" + i + "," + j + ")");
return true;
}
}
}
System.out.println("Key not found");
return false;
}
// Spiral print method
public static void printSpiral(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
if (startRow < endRow) {
for (int j = endCol - 1; j >= startCol; j--) {
System.out.print(matrix[endRow][j] + " ");
}
}
// left
if (startCol < endCol) {
for (int i = endRow - 1; i > startRow; i--) {
System.out.print(matrix[i][startCol] + " ");
}
}
startCol++;
startRow++;
endCol--;
endRow--;
}
System.out.println();
}
public static int diagonalSum(int matrix[][]) {
int sum = 0;
// O(n^2) 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];
// }
// }
// }
// O(n) Approach
for(int i=0; i<matrix.length; i++) {
sum += matrix[i][i];
if (i != matrix.length - 1 - i) {
sum += matrix[i][matrix.length - 1 - i];
}
}
return sum;
}
public static boolean staircaseSearch(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;
}
public static void noofvalue(int matrix[][], int target) {
int count = 0;
for(int i=0; i<matrix.length; i++) {
for(int j=0; j<matrix[0].length; j++){
if(matrix[i][j] == target) {
count++;
}
}
}
System.out.println("Number of occurrences of " + target + " : " + count);
}
public static void sumofnumbers(int matrix[][]) {
int sum = 0;
for(int j=0; j<matrix[0].length; j++) {
sum += matrix[1][j];
}
System.out.println(sum);
}
public static void trasnsposematrix(int matrix[][]) {
int rows = matrix.length;
int cols = matrix[0].length;
int transposed[][] = new int[cols][rows];
for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++) {
transposed[j][i] = matrix[i][j];
}
}
System.out.println("Transposed Matrix:");
for(int i=0; i<cols; i++) {
for(int j=0; j<rows; j++) {
System.out.print(transposed[i][j] + " ");
}
System.out.println();
}
}
public static void main(String args[]) {
int matrix[][] = {
{11, 12, 13},
{21, 22, 23}
};
int target = 8;
int key = 33;
// Spiral print
// printSpiral(matrix);
// Search example
// search(matrix, 10);
// Diagonal sum
// int sum = diagonalSum(matrix);
// System.out.println("Diagonal sum : " + sum);
// Staircase search
// staircaseSearch(matrix, key);
// Count occurrences of a value
// noofvalue(matrix, target);
// Sum of numbers in the matrix excluding the first row
// sumofnumbers(matrix);
// Transpose the matrix
trasnsposematrix(matrix);
}
}