-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray2d.java
More file actions
136 lines (120 loc) · 3.93 KB
/
array2d.java
File metadata and controls
136 lines (120 loc) · 3.93 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
import java.util.*;
public class array2d {
public static int largest(int matrix[][]){
int large= Integer.MIN_VALUE;
for (int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
if(large<matrix[i][j]){
large=matrix[i][j];
}
}
}
return large;
}
public static int diagonalsum(int matrix[][]){
int n=matrix.length,m=matrix[0].length;
int dSum=0;
if(n==m){
for (int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(i==j){
dSum += matrix[i][j];
}
else if (i+j==matrix.length-1){
dSum += matrix[i][j];
}
}
}
}
else{
System.out.println("not a sqaure matrix ");
}
return dSum;
}
public static int noOfElement(int matrix[][],int key){
// brute force method
int n=matrix.length,m=matrix[0].length;
int count=0;
for (int i=0;i<n;i++){
for(int j=0;j<m;j++){
if (matrix[i][j]==7){
count++;
}
}
}
return count;
// only for sorted array
// int row=0,col=matrix[0].length-1;
// int count=0;
// while(row<matrix.length && col>=0){
// if(matrix[row][col]==key){
// count++;
// row++;
// }
// else if(key>matrix[row][col]){
// row++;
// }
// else{
// col--;
// }
// }
// return count;
}
public static int secondRowSum(int matrix[][]){
int col=matrix[0].length;
int sum=0;
for (int i=0;i<col;i++){
sum += matrix[1][i];
}
return sum;
}
public static void transpose(int matrix[][]){
for(int i=0;i<matrix.length;i++){
for(int j=i+1;j<matrix[0].length;j++){
int temp=matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=temp;
}
}
}
public static void main (String args[]){
Scanner sc=new Scanner(System.in);
// int matrix[][]= new int [3][3];
// int n=matrix.length,m=matrix[0].length;
// input
// 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();
// }
// int sum=diagonalsum(matrix);
// int maximum=largest(matrix);
// System.out.println(sum + "is the diagonal sum of matrix");
int matrix[][]={{1,3,5},{30,60,10},{7,9,10}};
// int key=7;
// int total=secondRowSum(matrix);
System.out.println("before" );
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
System.err.print(matrix[i][j]+ " ");
}
System.out.println();
}
System.out.println("after" );
transpose(matrix);
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
System.out.print(matrix[i][j]+ " ");
}
System.out.println();
}
sc.close();
}
}