Determine the row index with minimum number of ones. The given 2D matrix has only zeroes and ones and also the matrix is sorted row wise . If two or more rows have same number of 1's than print the row with smallest index.
Note: If there is no '1' in any of the row than print '-1'. Input: The first line of input contains an integer T denoting the number of test cases.
The first line of each test case consists of two integer n and m.
The next line consists of n*m spaced integers. Output: Print the index of the row with minimum number of 1's. Constraints: 1<=T<=200 1<=n,m<=100
Output:
2 3 3 0 0 0 0 0 0 0 0 0 4 4 0 0 0 1 0 1 1 1 0 0 1 1 0 0 1 1 | minus1 0 |
Subscribe Our Newsletter
t = int(input())#No of test cases
ReplyDeletefor i in range(t):
n = input()#No of rows and cols
t = n.split()
r = int(t[0])
c = int(t[1])
tot = r * c
mat = []
inp = input()
form = inp.split()
#print(form)
inx = 0
for i in range(r):
tmp = []
for j in range(c):
e = int(form[inx])
inx += 1
tmp.append(e)
mat.append(tmp)
#Matrix ready
#print(mat)
flag = -999
mi = 999
#Check for row with min 1s
res = -1
index = 0
for sub in mat:
cnt = sub.count(1)
if cnt !=0 :
if cnt < mi:
mi = cnt
res = index
flag = 0
index += 1
if flag == -999:
print("minus1")
else:
print(res)