Given a 2D binary matrix A of dimensions NxM, determine the row that contains a minimum number of 1's.
Note-The matrix contains only 1s and 0s. Also, if two or more rows contain the minimum number of 1's, the answer is the lowest of those indices.
Example 1:
Input:
N=4,M=4
A=[[1,1,1,1],[1,1,0,0],[0,0,1,1],[1,1,1,1]]
Output:
2
Explanation:
Rows 2 and 3 contain the minimum number 
of 1's(2 each).Since, 2 is less than 3.
Thus, the answer is 2.
Example 2:
Input:
N=3,M=3
A=[[0,0,0],[0,0,0],[0,0,0]]
Output:
1
Explanation:
All the rows contain the same number 
of 1's(0 each).Among them, index 1 
is the smallest, so the answer is 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minRow() which takes the two integers N, M as well as the 2D matrix A as input parameters and returns the minimum index of the row which contains the least number of 1's.
Expected Time Complexity:O(N*M)
Expected Auxillary Space:O(1)
Constraints:
1 <= N,M <= 1000
0 <= A[i][j] <= 1

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Starter code: #User function Template for python3

class Solution:
    def minRow(self,N,M,A):
        #code here

Solutions:
class Solution:

	def minRow(self, n, m, a):
		c = M
		rowindex = 0
		for i in range(N):
			count = 0
			for j in range(M):
				if A[i][j] == 1:
					count = count + 1
			if count < c:
				c = count
				rowindex = i
		return rowindex + 1


class Solution:

	def minRow(self, N, M, A):
		l = []
		for i in A:
			l.append(i.count(1))
		return l.index(min(l)) + 1


class Solution:

	def minRow(self, N, M, A):
		mini = 1001
		inde = 1
		for (i, j) in enumerate(A):
			if mini > j.count(1):
				mini = j.count(1)
				inde = i + 1
		return inde


class Solution:

	def minRow(self, N, M, A):
		cnt = 10000
		r = 0
		for i in range(len(A)):
			if cnt > A[i].count(1):
				cnt = A[i].count(1)
				r = i + 1
		return r


import numpy as np

class Solution:

	def minRow(self, N, M, A):
		sol = 0
		x = np.sum(A, axis=1)
		y = min(x)
		for i in range(len(x)):
			if x[i] == y:
				return i + 1


class Solution:

	def minRow(self, N, M, A):
		sume = float('inf')
		idx = 0
		for I in range(N):
			a = sum(A[I])
			if a < sume:
				sume = min(a, sume)
				idx = I
		return idx + 1


class Solution:

	def minRow(self, N, M, A):
		list_a = []
		for i in range(N):
			list_a.append(sum(A[i]))
		min_count = min(list_a)
		for j in range(N):
			if min_count == list_a[j]:
				return j + 1


class Solution:

	def minRow(self, N, M, Mat):
		l = []
		for i in range(len(Mat)):
			l.append(sum(Mat[i]))
		return l.index(min(l)) + 1


class Solution:

	def minRow(self, N, M, A):
		mn = 1000000
		val = 0
		c = 1
		for i in A:
			cnt = i.count(1)
			if cnt < mn:
				mn = cnt
				val = c
			c += 1
		return val


class Solution:

	def minRow(self, N, M, A):
		minn = 0
		for i in range(1, len(A)):
			if A[i].count(1) < A[minn].count(1):
				minn = i
		return minn + 1


class Solution:

	def minRow(self, N, M, A):
		maxi = 0
		c = 0
		ans = 0
		for i in range(N):
			c = 0
			for j in range(M):
				if A[i][j] == 0:
					c += 1
			if maxi < c:
				maxi = c
				ans = i
		return ans + 1


class Solution:

	def minRow(self, N, M, A):
		ind = 0
		s = sum(A[0])
		for i in range(1, N):
			ss = sum(A[i])
			if ss < s:
				ind = i
				s = ss
		return ind + 1


class Solution:

	def minRow(self, N, M, A):
		p = []
		for i in range(len(A)):
			p.append((i + 1, A[i].count(1)))
		min1 = p[0][1]
		for i in p:
			if i[1] < min1:
				min1 = i[1]
				pos = i[0]
		for i in p:
			if i[1] == min1:
				return i[0]
		return pos


class Solution:

	def minRow(self, N, M, A):
		d = []
		for i in range(N):
			sum = 0
			for j in range(M):
				sum += A[i][j]
			d.append(sum)
		mx = min(d)
		cnt = 0
		for i in range(len(d)):
			if d[i] == mx:
				return i + 1
		return 0


class Solution:

	def minRow(self, N, M, A):
		mc = 0
		index = -1
		for i in range(N):
			c = 0
			for j in range(M):
				if A[i][j] == 0:
					c += 1
				else:
					continue
			if c > mc:
				mc = c
				index = i
		return index + 1


class Solution:

	def minRow(self, N, M, A):
		minimum = 10 ** 9 + 7
		ans = 0
		for i in range(N):
			count = 0
			for j in range(M):
				if A[i][j] == 1:
					count = count + 1
			if count < minimum:
				minimum = count
				ans = i
		return ans + 1


class Solution:

	def minRow(self, N, M, A):
		l = []
		for i in range(N):
			c = 0
			for j in range(M):
				if A[i][j] == 1:
					c += 1
			l.append(c)
		a = min(l)
		for i in range(0, len(l)):
			if l[i] == a:
				return i + 1


class Solution:

	def minRow(self, N, M, A):
		minm = A[0].count(1)
		row = 0
		for i in A[1:]:
			if i.count(1) < minm:
				minm = i.count(1)
				row = A.index(i)
			elif i.count(1) == minm:
				continue
		return row + 1


class Solution:

	def minRow(self, N, M, A):
		d = [0] * N
		m = 32676
		r = 0
		for i in range(0, N):
			s = 0
			for j in range(0, M):
				if A[i][j] == 1:
					s = s + 1
			if s < m:
				r = i
				m = s
		return r + 1


class Solution:

	def minRow(self, N, M, A):
		d = [0] * N
		for i in range(0, N):
			s = 0
			for j in range(0, M):
				if A[i][j] == 1:
					s = s + 1
			d[i] = s
		mini = d[0]
		m = 0
		for i in range(1, len(d)):
			if d[i] < mini:
				m = i
				mini = d[i]
		return m + 1


class Solution:

	def minRow(self, N, M, A):
		ind = 1
		minn = len(A[0])
		pos = 1
		for ar in A:
			summ = sum(ar)
			if summ < minn:
				pos = ind
				minn = summ
			ind += 1
		return pos


class Solution:

	def minRow(self, N, M, A):
		mini = M
		for (i, j) in enumerate(A):
			x = j.count(1)
			if x < mini:
				index = i + 1
			mini = min(x, mini)
		return index


class Solution:

	def minRow(self, N, M, A):
		mini = 0
		row = 0
		for i in range(N):
			c = 0
			for j in range(M):
				if A[i][j] == 1:
					c += 1
				if i == 0:
					mini = c
			if c < mini:
				mini = c
				row = i
		return row + 1


class Solution:

	def minRow(self, N, M, A):
		min1 = M
		res = 0
		for i in range(N):
			c = A[i].count(1)
			if c < min1:
				res = i
				min1 = c
		return res + 1


class Solution:

	def minRow(self, N, M, A):
		ma = float('inf')
		ans = 0
		for i in range(N):
			cnt = 0
			for j in range(M):
				if A[i][j] == 1:
					cnt += 1
			if cnt < ma:
				ma = cnt
				ans = i
		return ans + 1


class Solution:

	def minRow(self, N, M, A):
		m = N + M
		r = 1
		ansr = 1
		for i in A:
			if i.count(1) < m:
				m = i.count(1)
				ansr = r
			r += 1
		return ansr


class Solution:

	def minRow(self, N, M, A):
		val = M
		ind = 1
		for i in range(N):
			if A[i].count(1) < val:
				val = A[i].count(1)
				ind = i + 1
		return ind


class Solution:

	def minRow(self, N, M, A):
		sm = 0
		s = 10000000
		for t in range(len(A)):
			if sum(A[t]) < s:
				sm = t
				s = sum(A[t])
		return sm + 1


class Solution:

	def minRow(self, N, M, A):
		mi = M
		re = 0
		for i in range(N):
			c = 0
			for j in A[i]:
				if j == 1:
					c += 1
			if c < mi:
				mi = c
				re = i + 1
		return re


class Solution:

	def minRow(self, N, M, A):
		(lst, idx) = ([], 0)
		min = M
		for i in range(N):
			lst.append(A[i].count(1))
		for j in range(N):
			if lst[j] < min:
				min = lst[j]
				idx = j
		return idx + 1


class Solution:

	def minRow(self, N, M, A):
		min = M
		res = 0
		for i in range(N):
			count = 0
			for j in range(M):
				if A[i][j] == 1:
					count += 1
			if count < min:
				min = count
				res = i + 1
		return res


class Solution:

	def minRow(self, N, M, A):
		ind = 0
		c1 = 9999
		for i in range(N):
			c = 0
			for j in range(M):
				if A[i][j] == 1:
					c += 1
			if c < c1:
				c1 = c
				ind = i
		if ind == 0:
			return ind + 1
		else:
			return ind + 1


class Solution:

	def minRow(self, N, M, A):
		mini = float('inf')
		output = 0
		for (i, row) in enumerate(A):
			summa = sum(row)
			if summa < mini:
				mini = summa
				output = i + 1
		return output


from collections import Counter

class Solution:

	def minRow(self, N, M, A):
		c = M
		m = []
		for i in range(N):
			if A[i].count(1) < c:
				c = A[i].count(1)
				m = i
		return m + 1


class Solution:

	def minRow(self, N, M, A):
		s = M
		for i in range(0, N):
			p = A[i].count(1)
			if p < s:
				s = p
				l = i
		return l + 1


class Solution:

	def minRow(self, N, M, A):
		return sorted({k: v for (k, v) in enumerate([x.count(1) for x in A])}.items(), key=lambda x: x[1])[0][0] + 1


class Solution:

	def minRow(self, N, M, A):
		ans = 1000000000.0
		for i in range(N):
			count = 0
			for j in range(M):
				if A[i][j] == 1:
					count += 1
			if count < ans:
				ans = count
				a = i + 1
		return a


class Solution:

	def minRow(self, N, M, A):
		a = []
		for i in A:
			a.append(sum(i))
		r = min(a)
		return a.index(r) + 1


class Solution:

	def minRow(self, N, M, A):
		list = []
		for i in range(N):
			count = 0
			for j in range(M):
				if A[i][j] == 1:
					count += 1
				else:
					pass
			list.append(count)
		if len(list) > 1:
			return list.index(min(list)) + 1
		else:
			return len(list)


class Solution:

	def minRow(self, N, M, A):
		(row, count) = (0, 0)
		for i in A[0]:
			if i == 1:
				count += 1
		for r in range(1, N):
			c = 0
			for v in A[r]:
				if v == 1:
					c += 1
			if c < count:
				count = c
				row = r
		return row + 1


class Solution:

	def minRow(self, N, M, A):
		return min(range(N), key=lambda x: sum(A[x])) + 1


class Solution:

	def minRow(self, N, M, A):
		mi = 9876543218765432
		for i in range(len(A)):
			if A[i].count(1) < mi:
				mi = A[i].count(1)
				ind = i + 1
		return ind


class Solution:

	def minRow(self, N, M, A):
		count = 0
		l = []
		for i in range(0, N):
			for j in range(0, M):
				if A[i][j] == 1:
					count += 1
			l.append(count)
			count = 0
		return l.index(min(l)) + 1


class Solution:

	def minRow(self, N, M, A):
		row = 0
		col = 0
		ls = []
		while row < N:
			r = row
			c = col
			count = 0
			while c < M:
				if A[r][c] == 1:
					count += 1
				c += 1
			ls.append(count)
			row += 1
		x = ls.index(min(ls))
		return x + 1


class Solution:

	def minRow(self, N, M, A):
		maxi = M
		row = 0
		d = {}
		for i in range(N):
			count = 0
			for j in range(M):
				if A[i][j] == 1:
					count += 1
			if maxi > count:
				maxi = count
				row = i
		return row + 1


class Solution:

	def minRow(self, N, M, A):
		a = float('inf')
		c = 0
		for i in range(N):
			if sum(A[i]) < a:
				a = sum(A[i])
				c = i
		return c + 1


class Solution:

	def minRow(self, N, M, A):
		ans = []
		for v in A:
			ans.append(v.count(1))
		return ans.index(min(ans)) + 1


class Solution:

	def minRow(self, N, M, A):
		mini = 1000 + 1
		ans = 0
		for i in range(N):
			count = 0
			for j in range(M):
				if A[i][j] == 1:
					count = count + 1
			if count < mini:
				mini = count
				ans = i
		return ans + 1


class Solution:

	def minRow(self, N, M, A):
		self.N = N
		self.M = M
		self.A = A
		least_sum = [sum(i) for i in A]
		min_sum = min(least_sum)
		for i in range(len(A)):
			if sum(A[i]) == min_sum:
				return i + 1
				break


class Solution:

	def minRow(self, N, M, A):
		min_1 = math.inf
		min_i = 0
		for i in range(N):
			cur = sum(A[i])
			if cur < min_1:
				min_1 = cur
				min_i = i
		return min_i + 1


class Solution:

	def minRow(self, N, M, A):
		maxi = 99999999
		indi = 999999999
		for i in range(len(A)):
			x = A[i]
			u = x.count(1)
			if u < maxi:
				maxi = u
				indi = i + 1
		return indi


class Solution:

	def minRow(self, N, M, A):
		min_sum = M
		row_index = 0
		for i in range(N):
			sums = 0
			for j in range(M):
				sums += A[i][j]
			if sums < min_sum:
				min_sum = sums
				row_index = i
		return row_index + 1


class Solution:

	def minRow(self, N, M, A):
		minv = M
		r = 0
		for i in range(N):
			v = 0
			for j in range(M):
				v += A[i][j]
			if minv > v:
				minv = v
				r = i
		return r + 1


class Solution:

	def minRow(self, N, M, A):
		minOne = A[0].count(1)
		ind = 0
		for i in range(N):
			t = A[i].count(1)
			if minOne > t:
				minOne = t
				ind = i
		return ind + 1


class Solution:

	def minRow(self, N, M, A):
		row = -1
		c = 0
		for i in range(N):
			A[i].sort()
			while c < M and A[i][c] == 0:
				c += 1
				row = i
		return row + 1


class Solution:

	def minRow(self, N, M, A):
		minn = M + 1
		ind = -1
		for i in range(N):
			if minn > sum(A[i]):
				minn = sum(A[i])
				ind = i
		return ind + 1


class Solution:

	def minRow(self, n, m, mat):
		res = -1
		count = float('inf')
		for i in range(n):
			countone = 0
			for j in range(m):
				if mat[i][j] == 1:
					countone += 1
			if countone < count:
				count = countone
				res = i
		return res + 1


class Solution:

	def minRow(self, N, M, A):
		b = -1
		min = 100000
		for i in range(N):
			c = 0
			for j in range(M):
				if A[i][j] == 1:
					c += 1
			if c < min:
				min = c
				b = i + 1
		return b


import math

class Solution:

	def count_number_ones(self, num_array):
		local_count = 0
		for each in num_array:
			if each == 1:
				local_count += 1
		return local_count

	def minRow(self, N, M, A):
		row_num = 0
		global_count = math.inf
		for (i, row) in enumerate(A):
			count = self.count_number_ones(row)
			if count < global_count:
				row_num = i + 1
				global_count = count
		if count == 0:
			return 1
		return row_num


class Solution:

	def minRow(self, N, M, A):

		def binarySearch(rowNo):
			low = 0
			high = M - 1
			while low <= high:
				mid = low + high >> 1
				if A[mid] == 1:
					ans = mid
					low = mid + 1
		Min = M
		ans = 0
		for i in range(N):
			count = 0
			for j in range(M):
				if A[i][j] == 1:
					count += 1
			if count < Min:
				ans = i
				Min = count
		if Min == M:
			return 0
		return ans + 1


class Solution:

	def minRow(self, N, M, A):
		c = M
		index = 0
		for i in range(N):
			count = 0
			for j in range(M):
				if A[i][j] == 1:
					count = count + 1
			if count < c:
				c = count
				index = i
		return index + 1


class Solution:

	def minRow(self, N, M, A):
		mc = 10 ** 9
		row = -1
		for i in range(N):
			c = 0
			for j in range(M):
				if A[i][j] == 1:
					c = c + 1
			if c < mc:
				mc = c
				row = i
		return row + 1


class Solution:

	def minRow(self, N, M, A):
		res = 0
		mx = 100000
		for i in range(N):
			c = 0
			for j in range(M):
				if A[i][j] == 1:
					c += 1
			if c < mx:
				mx = c
				res = i
		return res + 1


class Solution:

	def minRow(self, N, M, A):
		row = 0
		minimum = M
		for i in range(N):
			if minimum > sum(A[i]):
				row = i
				minimum = sum(A[i])
		return row + 1


class Solution:

	def minRow(self, N, M, A):

		def binary_search(row):
			l = 0
			h = M - 1
			while l <= h:
				mid = (l + h) // 2
				if A[row][mid] == 0:
					l = mid + 1
				else:
					h = mid - 1
			return (l, row)
		for i in range(N):
			A[i].sort()
		ans = 0
		ans_row = 0
		for i in range(N):
			(cnt, row) = binary_search(i)
			if cnt > ans:
				ans = cnt
				ans_row = row
		return ans_row + 1


class Solution:

	def minRow(self, N, M, A):
		max_s = float('inf')
		ans = 1
		for i in range(N):
			s = sum(A[i])
			if max_s > s:
				max_s = s
				ans = i + 1
		return ans


class Solution:

	def minRow(self, N, M, A):
		b = []
		for i in range(len(A)):
			a = [A[i].count(1), i]
			b.append(a)
		res = sorted(b, key=lambda x: (x[0], x[1]))
		return res[0][1] + 1


class Solution:

	def minRow(self, N, M, A):
		mincnt = 10 ** 9
		ans = 0
		for i in range(N):
			cnt = sum(A[i])
			if cnt < mincnt:
				ans = i + 1
				mincnt = cnt
		return ans


class Solution:

	def minRow(self, N, M, A):
		l = []
		for i in A:
			l.append(sum(i))
		x = min(l)
		for i in range(len(l)):
			if l[i] == x:
				return i + 1


class Solution:

	def minRow(self, N, M, A):
		m = 10000
		c = 0
		for i in A:
			z = i.count(1)
			c += 1
			if z < m:
				k = c
				m = z
		return k


class Solution:

	def minRow(self, N, M, A):
		x = []
		for i in range(N):
			x.append(A[i].count(1))
			m = x.index(min(x))
		return m + 1


class Solution:

	def minRow(self, N, M, A):
		c1 = max(N, M)
		ans = -1
		for i in A:
			c = i.count(1)
			if c < c1:
				c1 = c
				ans = A.index(i)
		return ans + 1


class Solution:

	def minRow(self, N, M, A):
		c = M
		ind = N
		for i in range(N):
			tc = 0
			for j in A[i]:
				if j == 1:
					tc += 1
			if tc < c:
				c = tc
				ind = i
		return ind + 1


class Solution:

	def minRow(self, N, M, A):
		c = 9999999
		for i in range(N):
			d = A[i].count(1)
			if d < c:
				c = d
				e = i
		return e + 1