-
Notifications
You must be signed in to change notification settings - Fork 0
/
twodarray.java
152 lines (140 loc) · 4.25 KB
/
twodarray.java
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
//import java.util.Scanner;
public class twodarray {
/*Program to find whether an element is present or not
// 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 key at cell:(" + i + "," + j + ")");
// return true;
// }
// }
// }
// System.out.println("key not found");
// return false;
// }
*/
// public static void spiralmatrix(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;j--){
// if(startrow==endrow){
// break;
// }
// System.out.print(matrix[endrow][j]+" ");
// }
// //left
// for(int i=endrow-1;i>=startrow+1;i--){
// if(startrow==endrow){
// break;
// }
// System.out.print(matrix[i][startcol]+" ");
// }
// startcol++;
// startrow++;
// endcol--;
// endrow--;
// }
// System.out.println();
// }
// public static void diagonalsum(int matrix[][]) {
// int sum = 0;
// O(n^2)
// for(int i=0;i<matrix.length;i++){
// for(int j=0;j<matrix[0].length;j++){
// if(i==j){//primary diagonal
// sum += matrix[i][j];
// }
// else if(i+j == matrix.length-1){
// sum+=matrix[i][j];
// }
// }
// }//return sum;
// System.out.println(sum);
/*for(int i=0;i<matrix.length;i++){
//primary
sum += matrix[i][i];
//secondary
if(i != matrix.length-1-i){
sum+=matrix[i][matrix.length-i-1];
}
}
System.out.println(sum);
}*/
public static boolean searchinsortedmatrix(int matrix[][],int key) {
//search for a key in row wise and col wise sorted matrix(staircase search)
/*for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
if(key == matrix[i][j]){
System.out.println("key found at: ("+i+","+j+")");
}
else if(key!=matrix[i][j]){
System.out.println("not found");
}
}
}*/
int row=0, col=matrix[0].length-1;
while(row < matrix.length && col>=0){
if(matrix[row][col]==key){
System.out.println("key found 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 main(String[] args) {
/*
* matrices
* cells = row x col;
*
* 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, 88);
* sc.close();
int matrix[][] = {{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}};*/
//spiralmatrix(matrix);
//diagonalsum(matrix);
int matrix[][] = {{10,20,30,40},
{15,25,35,45},
{27,29,37,48},{32,33,39,50}
};
int key=133;
searchinsortedmatrix(matrix, key);
}
}