
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 elements smaller than or equal to x in a sorted matrix in C++
In this article, we are given a matrix of size m x n and an integer variable X. The row elements and the first column of the matrix are sorted in increasing order. Our task is to count the number of elements that are equal to or less than the given X value.
Counting Elements Smaller Than X in a Sorted Matrix
Here are the approaches for counting elements smaller than X in a sorted matrix:
Using Linear Search
To count number of elements smaller than or equal to given target element in a sorted matrix, we have used the linear search. The nested for loop compares each element of the matrix with the target element. If the matrix element is smaller or equal to the target element, the count is increased by 1. After traversing the whole matrix the count is returned.
Example
Here is an example of counting the matrix elements smaller than the given target value:
#include <iostream> using namespace std; int minEle(int mat[][4], int m, int n, int target) { int count = 0; for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) if (mat[i][j] <= target) count++; return count; } int main() { int mat[3][4] = { {1, 3, 5, 7}, {2, 4, 6, 8}, {9, 10, 11, 12}}; int target = 6; cout << "Given matrix is:" << endl; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 4; ++j) { cout << mat[i][j] << " "; } cout << endl; } cout << "Number of elements less than or equal to " << target << " is: "<< minEle(mat, 3, 4, target); return 0; }
The output of the above code is as follows:
Given matrix is: 1 3 5 7 2 4 6 8 9 10 11 12 Number of elements less than or equal to 6 is: 6
Using Staircase Search
The staircase search technique is used in a 2d matrix where each row and column are sorted in increasing order. We start the search from the top right corner of the matrix, and based on the comparison result, we either move left in the column or down in the row.
If the current element of the matrix is less than the target element then we move down in row. If the current element is greater than the target element we move left in the column. Here are the steps to implement the staircase search for counting the number of elements less than the target element X:
- We start the comparison from the top right corner of the matrix. First, we compare the top right element of the matrix with the target.
- If the matrix element at the top right corner is greater than the target element, then we move left in the matrix by decreasing the col by 1.
- If the matrix element is less than the target element then all the row elements are less than the target element till this col value as the row is sorted.
- We increase the row by 1 and store the (col+1) value in the count variable.
- We repeat the above three steps until we have traversed all rows or columns.
Example
The following example implements the above steps of staircase search to count the elements less than the target element in a 2d sorted matrix:
#include <iostream> using namespace std; int minEle(int mat[][4], int m, int n, int target) { int row = 0, col = n - 1, count = 0; while (row < m && col >= 0) { if (mat[row][col] <= target) { count += (col + 1); row++; } else col--; } return count; } int main() { int mat[3][4] = { {1, 3, 5, 7}, {2, 4, 6, 8}, {9, 10, 11, 12}}; int target = 6, row =3, col = 4; cout << "Given matrix is:" << endl; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 4; ++j) { cout << mat[i][j] << " "; } cout << endl; } cout << "Number of elements less than or equal to " << target << " is: " << minEle(mat, row, col, target); return 0; }
The output of the above code is as follows:
Given matrix is: 1 3 5 7 2 4 6 8 9 10 11 12 Number of elements less than or equal to 6 is: 6
Here is a detailed explanation of the above code:
Assume a Matrix: 1 2 5 7 3 5 8 9 4 6 9 11 Here, initially row = 0, col = 3, count = 0 Iteration 1: mat[0][3] = 7 > target = 6, => col-- = 2 Iteration 2: mat[0][2] = 5 <= target = 6, => count += (col + 1) = 3, row++ = 1 Iteration 3: mat[1][2] = 8 > target = 6, => col-- = 1 Iteration 4: mat[1][1] = 5 <= target = 6, count = 3 + (1 + 1) = 5, row++ = 2 Iteration 5: mat[2][1] = 6 <= target = 6, count = 5 + (1 + 1) = 7 Output: 7
Complexity Comparison
Here is a comparison of the time and space complexity of all the above approaches.
Approach | Time Complexity | Space Complexity |
---|---|---|
Linear Search | O(m * n) | O(1) |
Staircase Search | O(m + n) | O(1) |