Count zeros in a sorted matrix | Practice | GeeksforGeeks

Given a N X N binary Square Matrix where each row and column of the matrix is sorted in ascending order. Find the total number of zeros present in the matrix.
Example 1:
Input:
N = 3
A = {{0, 0, 0},
     {0, 0, 1},
     {0, 1, 1}}
Output: 6
Explanation: 
The first, second and third row contains 3, 2 and 1
zeroes respectively.
Example 2:
Input:
N = 2
A = {{1, 1},
     {1, 1}}
Output: 0
Explanation:
There are no zeroes in any of the rows.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countZeros() which takes a 2D matrix as input and returns the number occurring only once.
Expected Time Complexity: O(N ).
Expected Auxiliary Space: O(1).
Constraints
0 <   N  <= 103
0 <= A[i] <= 1
0
kandoihanish1 minute ago
int countZeros(vector<vector<int>>arr){    int k=arr.size();int count =0;       int m=0; for(int n=0;n<k;n++){     if(m==k){         break;     }                 if(arr[m][n]==0){         count++;     }    if(n==k-1){        m++;        n=-1;    }              } return count;  }
 
0
danish_nitdgp3 days ago
O(n) Solution without using any space


int countZeros(vector<vector<int>>A)
{
 //code here
 int j = A.size()-1;
       int res = 0;
       for(int i =0;i<A.size();i++){
           while(j>=0 && A[i][j]==1){
               j--;
           }
           res+=j+1;
       }
       return res;
}
0
vinitkarki297972 weeks ago
//JAVA SOLUTION IN O(N) TIME COMPLEXITY
 
int countZeros(int A[][], int N)   {       // Your code here       int count=0;       int col=N-1;       for(int i=0;i<N;i++){           while(col>=0){               if(A[i][col]==0){                   count += (col+1);                   break;               }               else{                   col--;               }           }       }       return count;   }
0
arunpatel3002002 months ago
EASY C++ SOLUTION
 //code here int row = A.size(); int col = A[0].size();  int count = 0;  for(int r =0 ; r < row ; r++){     for(int c = 0 ; c < col ; c++){         if(A[r][c] == 0){             count++;         }     } } return count;
0
saksham26672 months ago
JAVA SOLUTION BRUTEFORCE 
   int countZeros(int A[][], int N)
    {
        // Your code here
        int count=0;
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                if(A[i][j]==0){
                    count++;
                }
            }
        }
        return count;
0
sunghunet2 months ago
def countZeroes(self, A, N):
	cnt = 0
	for i in range(len(A)):
		cnt+=A[i].count(0)
	return cnt
0
aashima_saxena2 months ago
//C++ solution :-
 
int countZeros(vector<vector<int>>A){    int i=0;    int j=0;    int k=0; //code here if(A.size()==0)     return 0; if(A[0][0]!=0)     return 0; while(i<A.size() && j<A[0].size()){     if(A[i][j]==0){         k++;         j++;     }     if(j==A[0].size() || A[i][j]!=0){         i++;         j=0;     } } return k;}
+2
ashumishra8422 months ago
  def countZeroes(self, A, N):       i=0       j=0       count=0       while(i<n and j<n):           if A[i][j]==0:               count+=1               j+=1           else:               j+=1           if j==n:               i+=1               j=0       return count 
0
yogeshmandlik111Premium3 months ago
int countZeros(vector<vector<int>>A){ int count=0,x=0; for(auto it:A) count++; for(int i=0;i<count;i++){     for(int j=0;j<count;j++){         if(A[i][j]==0) x++;         else break;}} return x;}
0
yogeshmandlik111
This comment was deleted.
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.