Advertisement
Fastrail08

Count zeroes in sorted matrix

Aug 16th, 2022
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include <iostream>
  2. #include<vector>
  3. using namespace std;
  4.  
  5. int countZeroes(vector<vector<int> > &v){
  6.   int count = 0;
  7.   int i = 0, j = v.size() - 1;
  8.   while(i < (int) v.size() && j >= 0){
  9.     if(v[i][j] == 0){
  10.       count += j + 1;
  11.       i++;
  12.     }
  13.     else{
  14.       j--;
  15.     }
  16.   }
  17.   return count;
  18. }
  19.  
  20. int main()
  21. {
  22.     int n;
  23.     cin >> n;
  24.     vector<vector<int> > v(n, vector<int>(n));
  25.     for(int i = 0; i < n; i++)
  26.       for(int j = 0; j < n; j++){
  27.         cin >> v[i][j];
  28.       }
  29.     cout << countZeroes(v) << '\n';
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement