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
#include <bits/stdc++.h>
using namespace std;
class Solution {
public :
int columnWithMaxZeros(vector<vector< int > >& arr, int n)
{
int maxi = 0;
int colIdx = -1;
for ( int i = 0; i < n; i++) {
int zeroCnt = 0;
for ( int j = 0; j < n; j++) {
if (arr[j][i] == 0)
zeroCnt++;
}
if (zeroCnt > maxi) {
maxi = zeroCnt;
colIdx = i;
}
}
return colIdx;
}
};
int main()
{
vector<vector< int > > arr
= { { 0, 0, 0 }, { 1, 0, 1 }, { 0, 1, 1 } };
int n = arr.size();
Solution ob;
cout << ob.columnWithMaxZeros(arr, n) << endl;
}
|
Java
import java.util.*;
class Solution {
int columnWithMaxZeros( int [][] arr, int n) {
int maxi = 0 ;
int colIdx = - 1 ;
for ( int i = 0 ; i < n; i++) {
int zeroCnt = 0 ;
for ( int j = 0 ; j < n; j++) {
if (arr[j][i] == 0 )
zeroCnt++;
}
if (zeroCnt > maxi) {
maxi = zeroCnt;
colIdx = i;
}
}
return colIdx;
}
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();
System.out.println(ob.columnWithMaxZeros(arr, n));
}
}
|
Python3
class Solution:
def columnWithMaxZeros( self , arr, n):
maxi = 0
colIdx = - 1
for i in range (n):
zeroCnt = 0
for j in range (n):
if arr[j][i] = = 0 :
zeroCnt + = 1
if zeroCnt > maxi:
maxi = zeroCnt
colIdx = i
return colIdx
if __name__ = = "__main__" :
arr = [[ 0 , 0 , 0 ], [ 1 , 0 , 1 ], [ 0 , 1 , 1 ]]
n = len (arr)
ob = Solution()
print (ob.columnWithMaxZeros(arr, n))
|
C#
using System;
public class Solution
{
public int ColumnWithMaxZeros( int [][] arr)
{
int n = arr.Length;
int maxi = 0;
int colIdx = -1;
for ( int i = 0; i < n; i++)
{
int zeroCnt = 0;
for ( int j = 0; j < n; j++)
{
if (arr[j][i] == 0)
zeroCnt++;
}
if (zeroCnt > maxi)
{
maxi = zeroCnt;
colIdx = i;
}
}
return colIdx;
}
}
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();
Console.WriteLine(ob.ColumnWithMaxZeros(arr));
}
}
|
Javascript
class Solution {
columnWithMaxZeros(arr) {
let n = arr.length;
let maxi = 0;
let colIdx = -1;
for (let i = 0; i < n; i++) {
let zeroCnt = 0;
for (let j = 0; j < n; j++) {
if (arr[j][i] === 0) zeroCnt++;
}
if (zeroCnt > maxi) {
maxi = zeroCnt;
colIdx = i;
}
}
return colIdx;
}
}
let arr = [ [0, 0, 0], [1, 0, 1], [0, 1, 1] ];
let ob = new Solution();
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)
|