Predict the Column | Practice | GeeksforGeeks

Given a matrix(2D array) M of size N*N consisting of 0s and 1s only. The task is to find the column with maximum number of 0s.
Example:
Input:N = 3M[][] = {{1, 1, 0},        {1, 1, 0},        {1, 1, 0}}Output:2Explanation:2nd column (0-based indexing) is having 3 zeros which is maximum among all columns.
Your Task:Your task is to complete the function columnWithMaxZero() which should return the column number with maximum number of zeros. If more than one column exists, print the one which comes first.
Constraints:1 <= N <= 1020 <= A[i][j] <= 1
0
mannukhurana103976 days ago
Java
 
 int columnWithMaxZeros(int arr[][], int N)
    {
    // time: O(n*n)
    // space: O(1)
    int max=0;
    int index=-1;
    
        for(int i=0; i<N; i++){
            int temp=0;
            for(int j=0; j<N; j++){
                temp += arr[j][i]==0?1:0;
            }
            if(max<temp) {max = temp; index=i;}
        }
        return index;
    }
+1
kartikeyashokgautam4 weeks ago
Easy to understand JAVA Solution :- 
 
 int columnWithMaxZeros(int arr[][], int N)   {       // 3M[][] = {{1, 1, 0},        {1, 1, 0},        {1, 1, 0}}
       HashMap<Integer,Integer> hm = new HashMap<>();              for(int i=0;i<N;i++) // 0-0 , 1-0 , 2-0  initializing each column as containing 0 zeroes       {           hm.put(i,0);       }       
   // if any column contains zero then increase its value by one       // in this way we will stores no. of zeroes  with corresponding columns       for(int i=0;i<N;i++)        {            for(int j=0;j<N;j++)       {                      if(arr[i][j]==0)           {               hm.put(j,hm.get(j)+1);           }       }              }              // here we will check which column contains max zeroes       int max=0;       int columnWithMaxZero = 0;              for(int i=0;i<N;i++)       {           if(hm.get(i)>max)           {               max=hm.get(i);               columnWithMaxZero=i;           }       }                     return columnWithMaxZero;   }
0
hgaur7011 month ago
TIME COMPLEXITY : O(N*N),   TIME TAKEN : 0.1
int columnWithMaxZeros(vector<vector<int>>arr,int N){
        
     int highNum = 0;
    int column = 0;

    for (int i = 0; i < N; i++)
    {
        int temp = 0;
        for (int j = 0; j < N; j++)
        {
            if (arr[j][i] == 0)
            {
                temp++;
            }
        }
        if (temp > highNum)
        {
            highNum = temp;
            column = i;
        }
    }

    return column;
        
        
    }
0
yakash2220002 months ago
class Solution{   int columnWithMaxZeros(int arr[][], int N)   {       int m=0,c=0,res=0,j;       for (int i=0;i<N;i++){           for (j=0;j<N;j++){               if(arr[j][i]==0){                   c++;                                  }           }           if(m<c){               res=i;           }           m=Math.max(c,m);           c=0;       }        return res;   }}
0
dangrio2 months ago
int columnWithMaxZeros(vector<vector<int>>arr,int N){              // Your code here       int max=INT_MIN, count=0, col;       for(int i=0;i<N;i++){           count=0;           for(int j=0;j<N;j++){               if(arr[j][i]==0) count++;           }           if(max<count){               col = i;               max = count;           }       }       return col;   }
0
mulangacanon
This comment was deleted.
0
harshitshivam001
This comment was deleted.
0
bhagyeshreddy29113 months ago
     int columnWithMaxZeros(vector<vector<int>>arr,int N){       int maxi = 0; // intialising maximum count        int ans = 0; // intialising column index       for(int col = 0; col < N; col++) { // traverse through the column and row           int temp = 0;           for(int row = 0; row < N; row++) {               if(arr[row][col] == 0) {                   temp++;               }               if(temp > maxi) {                   maxi = temp;                   ans = col;               }           }       }       return ans;   }};
0
hitentandon3 months ago
Python:
        mcc,mc = 0,0;
       for i in range(N):
           cc=0;
           for j in range(N):
               if arr[j][i]==0:
                   cc+=1;
           if cc > mc:
               mcc,mc = i,cc;
       return mcc; 
C++:
        int mcc=0, mc=0;
       for(int i = 0; i < N; i++)
       {
           int cc=0;
           for(int j = 0; j < N; j++)
               if(!arr[j][i])
                   cc++;
           if(cc>mc)
           {
               mc = cc;
               mcc = i;
           }
       }
       return mcc; 
JS:
    columnWithMaxZeros(M,N){
      let mcc=0, mc=0;
       for(let i = 0; i < N; i++)
       {
           let cc=0;
           for(let j = 0; j < N; j++)
               if(M[j][i]===0)
                   cc++;
           if(cc>mc)
           {
               mc = cc;
               mcc = i;
           }
       }
       return mcc;
   }
Java:
        int mcc=0, mc=0;
       for(int i = 0; i < N; i++)
       {
           int cc=0;
           for(int j = 0; j < N; j++)
               if(arr[j][i]==0)
                   cc++;
           if(cc>mc)
           {
               mc = cc;
               mcc = i;
           }
       }
       return mcc;
0
sudhirswain1203 months ago
class Solution:   def columnWithMaxZeros(self,arr,N):       # code here        max_count,ans=0,0       for i in range(N):           temp=0           for j in range(N):               if (arr[j][i]==0):                                      temp+=1           if temp > max_count:               max_count=temp               ans=i       return ans  
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.