Find Column with Maximum Zeros in Matrix
Last Updated :
03 Jan, 2024
Given a matrix(2D array) M of size N*N consisting of 0s and 1s only. The task is to find the column with the maximum number of 0s. If more than one column exists, print the one which comes first. If the maximum number of 0s is 0 then return -1.
Examples:
Input: N = 3, M[][] = {{0, 0, 0}, {1, 0, 1}, {0, 1, 1}}
Output: 0
Explanation: 0th column (0-based indexing) is having 2 zeros which is maximum among all columns and comes first.
Input: N = 3, M[][] = {{0, 0, 0}, {1, 0, 1}, {1, 0, 1}}
Output: 1
Explanation: 1st column (0-based indexing) is having 3 zeros which is maximum among all columns and comes first.
Approach: Follow the steps below to solve the problem:
- Traverse through each column and then each row within that column to count the zeros.
- The zero count for each column is compared against the maximum count found so far. If a column has more zeros than the previous maximum zeros, it updates maxi and records the index of that column.
- Finally, the function returns the index of the column that contains the maximum number of zeros.
Below is the implementation of the above approach:
C++14
// C++ Code for above approach
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to return index of the column
// that contains the maximum number of zeros.
int columnWithMaxZeros(vector<vector<int> >& arr, int n)
{
int maxi = 0;
int colIdx = -1;
// Traverse each column of the matrix
for (int i = 0; i < n; i++) {
int zeroCnt = 0;
// traverse each row in the current column
for (int j = 0; j < n; j++) {
if (arr[j][i] == 0)
zeroCnt++;
}
// Compare the zero count of the current
// column with the maximum
if (zeroCnt > maxi) {
maxi = zeroCnt;
colIdx = i;
}
}
return colIdx;
}
};
// Drivers code
int main()
{
vector<vector<int> > arr
= { { 0, 0, 0 }, { 1, 0, 1 }, { 0, 1, 1 } };
int n = arr.size();
Solution ob;
// Function Call
cout << ob.columnWithMaxZeros(arr, n) << endl;
}
Java
import java.util.*;
class Solution {
// Function to return index of the column
// that contains the maximum number of zeros.
int columnWithMaxZeros(int[][] arr, int n) {
int maxi = 0;
int colIdx = -1;
// Traverse each column of the matrix
for (int i = 0; i < n; i++) {
int zeroCnt = 0;
// Traverse each row in the current column
for (int j = 0; j < n; j++) {
if (arr[j][i] == 0)
zeroCnt++;
}
// Compare the zero count of the current
// column with the maximum
if (zeroCnt > maxi) {
maxi = zeroCnt;
colIdx = i;
}
}
return colIdx;
}
// Main method
public static void main(String[] args) {
int[][] arr = { { 0, 0, 0 }, { 1, 0, 1 }, { 0, 1, 1 } };
int n = arr.length;
Solution ob = new Solution();
// Function Call
System.out.println(ob.columnWithMaxZeros(arr, n));
}
}
Python3
# Python program for the above approach
class Solution:
# Function to return index of the column
# that contains the maximum number of zeros.
def columnWithMaxZeros(self, arr, n):
maxi = 0
colIdx = -1
# Traverse each column of the matrix
for i in range(n):
zeroCnt = 0
# Traverse each row in the current column
for j in range(n):
if arr[j][i] == 0:
zeroCnt += 1
# Compare the zero count of the current
# column with the maximum
if zeroCnt > maxi:
maxi = zeroCnt
colIdx = i
return colIdx
# Drivers code
if __name__ == "__main__":
arr = [[0, 0, 0], [1, 0, 1], [0, 1, 1]]
n = len(arr)
ob = Solution()
# Function Call
print(ob.columnWithMaxZeros(arr, n))
# This code is contributed by Susobhan Akhuli
C#
using System;
public class Solution
{
// Function to return index of the column
// that contains the maximum number of zeros.
public int ColumnWithMaxZeros(int[][] arr)
{
int n = arr.Length;
int maxi = 0;
int colIdx = -1;
// Traverse each column of the matrix
for (int i = 0; i < n; i++)
{
int zeroCnt = 0;
// traverse each row in the current column
for (int j = 0; j < n; j++)
{
if (arr[j][i] == 0)
zeroCnt++;
}
// Compare the zero count of the current
// column with the maximum
if (zeroCnt > maxi)
{
maxi = zeroCnt;
colIdx = i;
}
}
return colIdx;
}
}
// Drivers code
public class Program
{
public static void Main()
{
int[][] arr = new int[][]
{
new int[] { 0, 0, 0 },
new int[] { 1, 0, 1 },
new int[] { 0, 1, 1 }
};
Solution ob = new Solution();
// Function Call
Console.WriteLine(ob.ColumnWithMaxZeros(arr));
}
}
// This code is contributed by shivamgupta310570
JavaScript
// JavaScript code for the above approach:
class Solution {
// Function to return index of the column
// that contains the maximum number of zeros.
columnWithMaxZeros(arr) {
let n = arr.length;
let maxi = 0;
let colIdx = -1;
// Traverse each column of the matrix
for (let i = 0; i < n; i++) {
let zeroCnt = 0;
// Traverse each row in the current column
for (let j = 0; j < n; j++) {
if (arr[j][i] === 0) zeroCnt++;
}
// Compare the zero count of the current column with the maximum
if (zeroCnt > maxi) {
maxi = zeroCnt;
colIdx = i;
}
}
return colIdx;
}
}
// Drivers code
let arr = [ [0, 0, 0], [1, 0, 1], [0, 1, 1] ];
let ob = new Solution();
// Function Call
console.log(ob.columnWithMaxZeros(arr));
Time Complexity: O(N x N), Traversing over all the elements of the matrix, therefore N X N elements are there.
Auxiliary Space: O(1)
Similar Reads
Find column with maximum sum in a Matrix Given a N*N matrix. The task is to find the index of column with maximum sum. That is the column whose sum of elements are maximum. Examples: Input : mat[][] = { { 1, 2, 3, 4, 5 }, { 5, 3, 1, 4, 2 }, { 5, 6, 7, 8, 9 }, { 0, 6, 3, 4, 12 }, { 9, 7, 12, 4, 3 }, }; Output : Column 5 has max sum 31 Input
7 min read
Find maximum element of each row in a matrix Given a matrix mat[][], the task is to find the maximum element of each row.Examples: Input: mat[][] = [[1, 2, 3] [1, 4, 9] [76, 34, 21]]Output :3976Input: mat[][] = [[1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2]]Output :216556The idea is to run the loop for no_of_rows. Check each element inside the r
4 min read
Find row with maximum sum in a Matrix Given an N*N matrix. The task is to find the index of a row with the maximum sum. That is the row whose sum of elements is maximum. Examples: Input : mat[][] = { { 1, 2, 3, 4, 5 }, { 5, 3, 1, 4, 2 }, { 5, 6, 7, 8, 9 }, { 0, 6, 3, 4, 12 }, { 9, 7, 12, 4, 3 }, }; Output : Row 3 has max sum 35 Input :
11 min read
Find Matrix With Given Row and Column Sums Given two arrays rowSum[] and colSum[] of size n and m respectively, the task is to construct a matrix of dimensions n à m such that the sum of matrix elements in every ith row is rowSum[i] and the sum of matrix elements in every jth column is colSum[j].Note: The resultant matrix can have only non-n
15 min read
Find maximum element of each column in a matrix Given a matrix, the task is to find the maximum element of each column. Examples: Input: [1, 2, 3] [1, 4, 9] [76, 34, 21] Output: 76 34 21 Input: [1, 2, 3, 21] [12, 1, 65, 9] 1, 56, 34, 2] Output: 12 56 65 21 Approach: The idea is to run the loop for no_of_cols. Check each element inside the column
6 min read