forked from avinashbest/java-coding-ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestColumnSum.java
More file actions
41 lines (36 loc) · 1.21 KB
/
Copy pathLargestColumnSum.java
File metadata and controls
41 lines (36 loc) · 1.21 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
package twoDimensionalArray;
import java.util.Scanner;
public class LargestColumnSum {
public static int largestColumnSum(int[][] arr) {
int largest = Integer.MIN_VALUE;
for (int j = 0; j < arr[0].length; j++) {
int sumOfEachRow = 0;
for (int i = 0; i < arr.length; i++) {
sumOfEachRow += arr[i][j];
}
if (sumOfEachRow > largest) {
largest = sumOfEachRow;
}
}
return largest;
}
public static int[][] takeInput() {
Scanner scan = new Scanner(System.in);
System.out.println("Enter number of rows:");
int row = scan.nextInt();
System.out.println("Enter number of columns:");
int column = scan.nextInt();
int[][] arr = new int[row][column];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
System.out.println("Enter the element at " + i + "th rows " + j + "th column:");
arr[i][j] = scan.nextInt();
}
}
return arr;
}
public static void main(String[] args) {
int[][] arr = takeInput();
System.out.println(largestColumnSum(arr));
}
}