-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLargestRoworColumn.py
90 lines (51 loc) · 1.64 KB
/
LargestRoworColumn.py
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
# 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.
# Sample Input 1:
# 1
# 3 2
# 6 9
# 8 5
# 9 2
# Sample Output 1:
# column 0 23
from sys import stdin
def findLargest(arr, nRows, mCols):
#for column max index and number
row_max = -2147483648
row_max_index = 0
for i in range(nRows):
sum = 0
for j in range(mCols):
sum += mat[i][j]
if sum > row_max:
row_max = sum
row_max_index = i
col_max = -2147483648
col_max_index = 0
for j in range(mCols):
sum = 0
for i in range(nRows):
sum += mat[i][j]
if sum > col_max:
col_max = sum
col_max_index = j
if col_max > row_max:
print("column", col_max_index, col_max)
else:
print("row", row_max_index, row_max)
#Taking Input Using Fast I/O
def take2DInput() :
li = stdin.readline().rstrip().split(" ")
nRows = int(li[0])
mCols = int(li[1])
if nRows == 0 :
return list(), 0, 0
mat = [list(map(int, input().strip().split(" "))) for row in range(nRows)]
return mat, nRows, mCols
#main
t = int(stdin.readline().rstrip())
while t > 0 :
mat, nRows, mCols = take2DInput()
findLargest(mat, nRows, mCols)
t -= 1