Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 17 Dec, 2018
Geek Week

Given a matrix mat[][], the task is to count the number of columns that are sorted in descending.


Input: mat[][] = {{1, 3}, {0, 2}}
Output: 2
First column: 1 > 0
Second column: 3 > 2
Hence, the count is 2

Input: mat[][] = {{2, 2}, {1, 3}}
Output: 1

Approach: Traverse each column one by one and check if the next elementprevious 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:


# 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]] 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up
Recommended Articles
Page :