-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargestRowOrColumn.java
141 lines (124 loc) · 4.41 KB
/
LargestRowOrColumn.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
// Largest Row or Column
// Send Feedback
// 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
// 1 <= N <= 10^3
// 1 <= M <= 10^3
// Time Limit: 1sec
// Sample Input 1:
// 1
// 3 2
// 6 9
// 8 5
// 9 2
// Sample Output 1:
// column 0 23
// Sample Input 2:
// 1
// 4 4
// 6 9 8 5
// 9 2 4 1
// 8 3 9 3
// 8 7 8 6
// Sample Output 2:
// column 0 31
import java.util.Scanner;
public class LargestRowOrColumn {
public static void takeInput(int arr[][], Scanner s) {
System.out.println("Enter Elements of the array : ");
int rows = arr.length;
int columns = 0;
if (rows != 0) {
columns = arr[0].length;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.println("Enter " + i + "th row " + j + "th column Element");
arr[i][j] = s.nextInt();
}
}
System.out.println();
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of test cases here : ");
int t = s.nextInt();
for (int i = 1; i <= t; i++) {
System.out.println("Enter the rows of the " + i + " Array");
int rows = s.nextInt();
System.out.println("Enter the columns of the " + i + " Array");
int columns = s.nextInt();
int arr[][] = new int[rows][columns];
takeInput(arr, s);
findLargest(arr);
}
}
public static void findLargest(int arr[][]) {
int rows = arr.length;
int columns = 0;
if (rows != 0) {
columns = arr[0].length;
}
int largest = Integer.MIN_VALUE;
int index = -1;
String name = "";
int maxRowSum = Integer.MIN_VALUE;
int maxRowIndex = 0;
int maxColSum = Integer.MIN_VALUE;
int maxColIndex = 0;
System.out.println("The Output : ");
// Row Sum
for (int i = 0; i < rows; i++) {
int rowSum = 0;
for (int j = 0; j < columns; j++) {
rowSum += arr[i][j];
}
if (rowSum > maxRowSum) {
maxRowSum = rowSum;
maxRowIndex = i;
}
}
// Column Sum
for (int j = 0; j < columns; j++) {
int colSum = 0;
for (int i = 0; i < rows; i++) {
colSum += arr[i][j];
}
if (colSum > maxColSum) {
maxColSum = colSum;
maxColIndex = j;
}
}
if (maxRowSum > maxColSum) {
largest = maxRowSum;
index = maxRowIndex;
name = "row";
} else if (maxRowSum == maxColSum) {
largest = maxRowSum;
index = maxRowIndex;
name = "row";
}
else {
largest = maxColSum;
index = maxColIndex;
name = "column";
}
System.out.println(name + " " + index + " " + largest);
}
}