Count all the columns in a matrix which are sorted in descending in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to find the number of columns in a matrix which are sorted in descending.

For this we will be provided with a matrix. Our task is to count the number of columns in the matrix having elements sorted in descending order.

Example

 Live Demo

#include <bits/stdc++.h>
#define MAX 100
using namespace std;
//counting columns sorted in descending order
int count_dcolumns(int mat[][MAX], int r, int c){
   int result = 0;
   for (int i=0; i<c; i++){
      int j;
      for (j=r-1; j>0; j--)
         if (mat[i][j-1] >= mat[i][j])
            break;
      if (c > 1 && j == 0)
         result++;
   }
   return result;
}
int main(){
   int m = 2, n = 2;
   int mat[][MAX] = {{1, 3}, {0, 2,}};
   cout << count_dcolumns(mat, m, n);
   return 0;
}

Output

2
raja
Published on 17-Feb-2020 10:22:27
Advertisements