Skip to content

Commit 3b10a70

Browse files
authored
Merge pull request #129 from dsrao711/issue128
Python solution for max-ones-in-a-row-in-the-matrix in DSA450
2 parents c7f39e7 + b6a942b commit 3b10a70

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

DSA 450 GFG/maxones.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#https://practice.geeksforgeeks.org/problems/row-with-max-1s0023/1#
2+
3+
#Steps :
4+
5+
# 1 . Initialize the count of ones in a row as max_ones_row = -1
6+
# 2 . Initialize the max count of 1's as max_ones = 0
7+
# 3 . Iterate through each row and find the ones count
8+
# 4 . Compare the max_ones with max_ones_row in each row and store the updated count in max_ones_row
9+
10+
class Solution:
11+
12+
def rowWithMax1s(self,arr, n, m):
13+
# code here
14+
j = m - 1
15+
max_ones = 0
16+
max_ones_row = -1
17+
for i in range(0 , n ):
18+
ones = self.countones(arr[i])
19+
if( ones > max_ones ):
20+
max_ones = ones
21+
max_ones_row = i
22+
23+
24+
return max_ones_row
25+
26+
def countones(self , a ):
27+
count = a.count(1)
28+
return count
29+
30+
#{
31+
# Driver Code Starts
32+
#Initial Template for Python 3
33+
34+
if __name__ == '__main__':
35+
tc = int(input())
36+
while tc > 0:
37+
n, m = list(map(int, input().strip().split()))
38+
39+
inputLine = list(map(int, input().strip().split()))
40+
arr = [[0 for j in range(m)] for i in range(n)]
41+
42+
for i in range(n):
43+
for j in range(m):
44+
arr[i][j] = inputLine[i * m + j]
45+
ob = Solution()
46+
ans = ob.rowWithMax1s(arr, n, m)
47+
print(ans)
48+
tc -= 1
49+
50+
# } Driver Code Ends

0 commit comments

Comments
 (0)