-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinZeroes_Array.java
More file actions
50 lines (40 loc) · 1.43 KB
/
MinZeroes_Array.java
File metadata and controls
50 lines (40 loc) · 1.43 KB
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
import java.util.Scanner;
public class MinZeroes_Array{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.println("Enter the number of columns: ");
int columns = scanner.nextInt();
int[][] matrix = new int[rows][columns];
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = scanner.nextInt();
}
}
scanner.close();
int minZeroColumn = findMinZeroColumn(matrix);
System.out.println("Column with the minimum number of zeros: " + minZeroColumn);
}
public static int findMinZeroColumn(int[][] matrix) {
int columns = matrix[0].length;
int[] zeroCount = new int[columns];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < columns; j++) {
if (matrix[i][j] == 0) {
zeroCount[j]++;
}
}
}
int minZeroes = Integer.MAX_VALUE;
int minColumn = -1;
for (int j = 0; j < columns; j++) {
if (zeroCount[j] < minZeroes) {
minZeroes = zeroCount[j];
minColumn = j;
}
}
return minColumn;
}
}