Open In App

Find Column with Maximum Zeros in Matrix

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:

Below is the implementation of the above approach:




// 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;
}




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));
    }
}




# 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




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 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));

Output
0








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)


Article Tags :