- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathLargestColSum.java
44 lines (40 loc) · 1.34 KB
/
LargestColSum.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
//Find largest colum sum
packageTwo_Dimensional_Array;
importjava.util.Scanner;
publicclassLargestColSum {
publicstaticintlargestcolsum(int [] [] arr2) {
introws = arr2.length;
intcols = arr2[0].length;
intmax = Integer.MIN_VALUE;
for (inti = 0; i < cols; i++) {
intsum = 0;
for (intj = 0; j < rows; j++) {
sum = sum + arr2[j][i];
}
if ( sum > max) {
max = sum;
}
}
returnmax;
}
publicstaticint [] [] takeInput() {
Scannersc = newScanner(System.in);
System.out.println("Enter the number of rows: ");
introws = sc.nextInt();
System.out.println("Enter the number of colums: ");
intcols = sc.nextInt();
int [][] arr2 = newint[rows][cols];
for (inti = 0; i < rows; i++) {
for (intj = 0; j < cols; j++) {
System.out.println("Enter the element at " + i + " th row and " + j + " th columns.");
arr2[i][j] = sc.nextInt();
}
}
returnarr2;
}
publicstaticvoidmain(String[] args) {
int [] [] arr2 = takeInput();
intn = largestcolsum(arr2);
System.out.println("Largest column sum is " + n);
}
}