Given a matrix mat[][], the task is to count the number of columns that are sorted in descending.
Examples:
Input: mat[][] = {{1, 3}, {0, 2}}
Output: 2 First column: 1 > 0 Second column: 3 > 2 Hence, the count is 2Input: mat[][] = {{2, 2}, {1, 3}}
Output: 1
Approach:
Traverse each column one by one and check if the next element ? previous element in the same column. If the condition is valid for all the possible elements then increment the count by 1. After all the columns have been traversed, print the count.
Below is the implementation of the above approach:
C++
#include <iostream> #include <vector> using namespace std; // Function to count the number of columns // in a matrix that are sorted in descending int countDescCol(vector<vector< int >> A) { int countOfCol = 0; for ( int j = 0; j < A[0].size(); j++) { bool isDescending = true ; for ( int i = 0; i < A.size() - 1; i++) { if (A[i][j] < A[i + 1][j]) { isDescending = false ; break ; } } if (isDescending) { countOfCol++; } } return countOfCol; } // Driver code int main() { vector<vector< int >> A = {{1, 3}, {0, 2}}; cout << countDescCol(A) << endl; return 0; } |
Java
// Java code to count the number of columns in // a matrix that are sorted in descending import java.util.ArrayList; import java.util.List; public class Main { public static int countDescCol(List<List<Integer>> A) { int countOfCol = 0 ; for ( int j = 0 ; j < A.get( 0 ).size(); j++) { boolean isDescending = true ; for ( int i = 0 ; i < A.size() - 1 ; i++) { if (A.get(i).get(j) < A.get(i + 1 ).get(j)) { isDescending = false ; break ; } } if (isDescending) { countOfCol++; } } return countOfCol; } public static void main(String[] args) { List<List<Integer>> A = new ArrayList<>(); A.add( new ArrayList<Integer>() {{ add( 1 ); add( 3 ); }}); A.add( new ArrayList<Integer>() {{ add( 0 ); add( 2 ); }}); System.out.println(countDescCol(A)); } } |
Python3
# Python3 program to count the number of columns # in a matrix that are sorted in descending # Function to count the number of columns # in a matrix that are sorted in descending def countDescCol(A): countOfCol = 0 for col in zip ( * A): if all (col[i] > = col[i + 1 ] for i in range ( len (col) - 1 )): countOfCol + = 1 return countOfCol # Driver code A = [[ 1 , 3 ], [ 0 , 2 ]] print (countDescCol(A)) |
C#
// Java code to count the number of columns in // a matrix that are sorted in descending using System; using System.Collections.Generic; public class MainClass { public static int countDescCol(List<List< int >> A) { int countOfCol = 0; for ( int j = 0; j < A[0].Count; j++) { bool isDescending = true ; for ( int i = 0; i < A.Count - 1; i++) { if (A[i][j] < A[i + 1][j]) { isDescending = false ; break ; } } if (isDescending) { countOfCol++; } } return countOfCol; } public static void Main() { List<List< int >> A = new List<List< int >>(); A.Add( new List< int > {1, 3}); A.Add( new List< int > {0, 2}); Console.WriteLine(countDescCol(A)); } } |
Javascript
// Function to count the number of columns in // a matrix that are sorted in descending order function countDescCol(A) { let countOfCol = 0; // Transpose the matrix and iterate over its rows (original columns) A[0].map((_, colIndex) => A.map(row => row[colIndex])).forEach(col => { // Check if the column is sorted in descending order if (col.slice(1).every((item, i) => col[i] >= item)) { countOfCol += 1; } }); return countOfCol; } // Driver code let A = [[1, 3], [0, 2]]; console.log(countDescCol(A)); |
2
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!