- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathLargestRowColumn.java
104 lines (93 loc) · 3.41 KB
/
LargestRowColumn.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
// Largest Row or Column
// For a given two-dimensional integer array/list of size (N x M), you need to find out which row or column has the largest sum(sum of all the elements in a row/column) amongst all the rows and columns.
// Note :
// If there are more than one rows/columns with maximum sum, consider the row/column that comes first. And if ith row and jth column has the same largest sum, consider the ith row as answer.
// Input Format :
// The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
// First line of each test case or query contains two integer values, 'N' and 'M', separated by a single space. They represent the 'rows' and 'columns' respectively, for the two-dimensional array/list.
// Second line onwards, the next 'N' lines or rows represent the ith row values.
// Each of the ith row constitutes 'M' column values separated by a single space.
// Output Format :
// For each test case, If row sum is maximum, then print: "row" <row_index> <row_sum>
// OR
// If column sum is maximum, then print: "column" <col_index> <col_sum>
// It will be printed in a single line separated by a single space between each piece of information.
// Output for every test case will be printed in a seperate line.
// Consider :
// If there doesn't exist a sum at all then print "row 0 -2147483648", where -2147483648 or -2^31 is the smallest value for the range of Integer.
// Constraints :
// 1 <= t <= 10^2
// 0 <= N <= 10^3
// 0 <= M <= 10^3
// Time Limit: 1sec
// Sample Input 1 :
// 1
// 2 2
// 1 1
// 1 1
// Sample Output 1 :
// row 0 2
// Sample Input 2 :
// 2
// 3 3
// 3 6 9
// 1 4 7
// 2 8 9
// 4 2
// 1 2
// 90 100
// 3 40
// -10 200
// Sample Output 2 :
// column 2 25
// column 1 342
packageTwo_Dimensional_Array;
importjava.util.Scanner;
publicclassLargestRowColumn {
publicstaticvoidlargestRowColumn(int [] [] arr2) {
intcols = arr2[0].length;
introws = arr2.length;
intmax = Integer.MIN_VALUE;
Stringstr = " ";
for (inti = 0; i < rows; i++) {
intsum = 0;
for (intj = 0; j < cols; j++) {
sum = sum + arr2[i][j];
}
if( sum > max) {
max = sum;
str = "Row ";
}
}
for (inti = 0; i < cols; i++) {
intsum = 0;
for (intj = 0; j < rows; j++) {
sum = sum + arr2[j][i];
}
if ( sum > max) {
max = sum;
str = "Column ";
}
}
System.out.println(str + max);
}
publicstaticint [] [] takingInput() {
Scannersc = newScanner(System.in);
System.out.println("Enter the number of rows: ");
introws = sc.nextInt();
System.out.println("Enter the number of columns: ");
intcols = sc.nextInt();
int [] [] arr = newint[rows][cols];
for (inti = 0; i < rows; i++) {
for (intj = 0; j < cols; j++) {
System.out.println("Enter the element at " + i + " rows " + j + " columns: ");
arr[i][j] = sc.nextInt();
}
}
returnarr;
}
publicstaticvoidmain(String[] args) {
int [] [] arr2 = takingInput();
largestRowColumn(arr2);
}
}