-
Notifications
You must be signed in to change notification settings - Fork 0
/
22_row-with-minimum-number-of-1s.java
65 lines (59 loc) · 1.7 KB
/
22_row-with-minimum-number-of-1s.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
// 04-2024(April)/22_row-with-minimum-number-of-1s.java
/**
* Date : 22-Apr-24
* Repo : https://github.com/ankitsamaddar/daily-geeksforgeeks
*
* Problem : Row with minimum number of 1s
* Difficulty: 🟢Easy
*
* GeeksforGeeks : https://www.geeksforgeeks.org/problems/row-with-minimum-number-of-1s5430/1
*/
import java.io.*;
// User function Template for Java
class Solution {
// SIMULATION APPROACH
int minRow(int n, int m, int[][] a) {
int minCount = Integer.MAX_VALUE;
int minRowIndex = -1;
// Traverse the matrix
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < m; j++) {
// count number of 1s in current row
if (a[i][j] == 1) {
count++;
}
}
// if current row has minimum 1s so far, update minRowIndex
if (count < minCount) {
minCount = count;
minRowIndex = i;
}
}
// index starting at 1
return minRowIndex + 1;
}
}
//{ Driver Code Starts
// Initial Template for Java
class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
String s[] = read.readLine().split(" ");
int N = Integer.parseInt(s[0]);
int M = Integer.parseInt(s[1]);
int A[][] = new int[N][M];
for (int i = 0; i < N; i++) {
String S[] = read.readLine().split(" ");
for (int j = 0; j < M; j++) {
A[i][j] = Integer.parseInt(S[j]);
}
}
Solution ob = new Solution();
System.out.println(ob.minRow(N, M, A));
}
}
}
// } Driver Code Ends