

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix using Python?
In this section we are going to see a python program that counts negative numbers in Row-wise and Column-wise sorted matrix with optimal solution.
Row-wise and column-wise sorted array means, each value at any index is small or equal to the value at the index in next column and next row.
For example in below matrix M
M = [[-40, -12, 1, 5], [-20, -2, 5, 15], [-22, -1, 13, 18], [-12, 0, 15, 38]]
In above matrix M, the first column of first row is -40, which is smaller than the value at next column value in same row i.e. -12 and is also smaller than the value in next row in the same column i.e. -20 and so on.
Example 2
# The matrix must be sorted in ascending order. If not, the algorithm will not work properly matrix = [ [-40, -12, 1, 5], [-20, -2, 5, 15], [-22, -1, 13, 18], [-12, 0, 15, 38]] # To obtain the number of row rowCount = len(matrix) columnCount = 0 # To obtain the number of column for i in matrix[0]: columnCount += 1 a = 0 b = 0 count_Of_Negative_Integer = 0 while a < rowCount and b < columnCount: if matrix[a][b] >= 0: a += 1 b = 0 else: count_Of_Negative_Integer += 1 b += 1 print("Count of Negative Integers in sorted Matrix is: ",count_Of_Negative_Integer)
Result
Count of Negative Integers in sorted Matrix is: 7
In above program,
>=0: first we try to find the count of negative integer, less than 0.
Because in above program, we are trying to get negative integers, however, same program can be used to find the count of integer that is less than any particular integers(n). For example to find the count of integer that is less than or equal to 5 using >5.
- Related Questions & Answers
- How to search in a row wise and column wise increased matrix using C#?
- Row-wise vs column-wise traversal of matrix in C++
- To print all elements in sorted order from row and column wise sorted matrix in Python
- Find median in row wise sorted matrix in C++
- How can a specific operation be applied row wise or column wise in Pandas Python?
- . Find a common element in all rows of a given row-wise sorted matrix
- How to search in a row wise increased matrix using C#?
- Find a common element in all rows of a given row-wise sorted matrix in C++
- Python – Element wise Matrix Difference
- How to find the row-wise mode of a matrix in R?
- Display the Numerical positive and negative element-wise in Numpy
- Row-wise common elements in two diagonals of a square matrix in C++
- Count positive and negative numbers in a list in Python program
- Python program to count positive and negative numbers in a list
- Test element-wise for positive or negative infinity in Numpy