Count elements smaller than or equal to x in a sorted matrix
Last Updated :
29 Mar, 2024
Given a n x n strictly sorted matrix and a value x. The problem is to count the elements smaller than or equal to x in the given matrix. Here strictly sorted matrix means that matrix is sorted in a way such that all elements in a row are sorted in increasing order and for row ‘i’, where 1 <= i <= n-1, first element of row ‘i’ is greater than or equal to the last element of row ‘i-1’. Examples:
Input : mat[][] = { {1, 4, 5},
{6, 10, 11},
{12, 15, 20} }
x = 9
Output : 4
The elements smaller than '9' are:
1, 4, 5 and 6.
Input : mat[][] = { {1, 4, 7, 8},
{10, 10, 12, 14},
{15, 26, 30, 31},
{31, 42, 46, 50} }
x = 31
Output : 13
Naive Approach: Traverse the matrix using two nested loops and count the elements smaller than or equal to x. Time Complexity is of O(n^2). Efficient Approach: As matrix is strictly sorted, use the concept of binary search technique. First apply the binary search technique on the elements of the first column to find the row index number of the largest element smaller than equal to ‘x’. For duplicates get the last row index no of occurrence of required element ‘x’. Let it be row_no.. If no such row exists then return 0. Else apply the concept of binary search technique to find the column index no of the largest element smaller than or equal to ‘x’ in the row represented by row_no. Let it col_no. Finally return ((row_no) * n) + (col_no + 1). The concept of binary search technique can be understood by the binary_search function of this post.
C++
#include <bits/stdc++.h>
using namespace std;
#define SIZE 100
int binarySearchOnRow( int mat[SIZE][SIZE],
int l, int h, int x)
{
while (l <= h) {
int mid = (l + h) / 2;
if (mat[mid][0] <= x)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
int binarySearchOnCol( int mat[SIZE][SIZE],
int l, int h, int x, int row)
{
while (l <= h) {
int mid = (l + h) / 2;
if (mat[row][mid] <= x)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
int countSmallElements( int mat[SIZE][SIZE],
int n, int x)
{
int row_no = binarySearchOnRow(mat, 0, n - 1, x);
if (row_no == -1)
return 0;
int col_no = binarySearchOnCol(mat, 0, n - 1, x, row_no);
return ((row_no)*n) + (col_no + 1);
}
int main()
{
int mat[SIZE][SIZE] = { { 1, 4, 7, 8 },
{ 10, 10, 12, 14 },
{ 15, 26, 30, 31 },
{ 31, 42, 46, 50 } };
int n = 4;
int x = 31;
cout << "Count = "
<< countSmallElements(mat, n, x);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int SIZE= 100 ;
static int binarySearchOnRow( int [][] mat,
int l, int h, int x)
{
while (l <= h) {
int mid = (l + h) / 2 ;
if (mat[mid][ 0 ] <= x)
l = mid + 1 ;
else
h = mid - 1 ;
}
return h;
}
static int binarySearchOnCol( int [][] mat,
int l, int h, int x, int row)
{
while (l <= h) {
int mid = (l + h) / 2 ;
if (mat[row][mid] <= x)
l = mid + 1 ;
else
h = mid - 1 ;
}
return h;
}
static int countSmallElements( int [][] mat,
int n, int x)
{
int row_no = binarySearchOnRow(mat,
0 , n - 1 , x);
if (row_no == - 1 )
return 0 ;
int col_no = binarySearchOnCol(mat,
0 , n - 1 , x, row_no);
return ((row_no) * n) + (col_no + 1 );
}
public static void main (String[] args) {
int mat[][] = { { 1 , 4 , 7 , 8 },
{ 10 , 10 , 12 , 14 },
{ 15 , 26 , 30 , 31 },
{ 31 , 42 , 46 , 50 } };
int n = 4 ;
int x = 31 ;
System.out.println( "Count = "
+ countSmallElements(mat, n, x));
}
}
|
Python3
SIZE = 100
def binarySearchOnRow(mat, l, h, x):
while l < = h:
mid = (l + h) / / 2
if mat[mid][ 0 ] < = x:
l = mid + 1
else :
h = mid - 1
return h
def binarySearchOnCol(mat, l, h, x, row):
while l < = h:
mid = (l + h) / / 2
if mat[row][mid] < = x:
l = mid + 1
else :
h = mid - 1
return h
def countSmallElements(mat, n, x):
row_no = binarySearchOnRow(mat, 0 , n - 1 , x)
if row_no = = - 1 :
return 0
col_no = binarySearchOnCol(mat, 0 ,
n - 1 , x, row_no)
return ((row_no) * n) + (col_no + 1 )
mat = [ [ 1 , 4 , 7 , 8 ],
[ 10 , 10 , 12 , 14 ],
[ 15 , 26 , 30 , 31 ],
[ 31 , 42 , 46 , 50 ] ]
n = 4
x = 31
print ( "Count = " + str (countSmallElements(mat, n, x)))
|
C#
using System;
class GFG {
static int binarySearchOnRow( int [,] mat, int l,
int h, int x)
{
while (l <= h) {
int mid = (l + h) / 2;
if (mat[mid,0] <= x)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
static int binarySearchOnCol( int [,]mat, int l,
int h, int x, int row)
{
while (l <= h) {
int mid = (l + h) / 2;
if (mat[row,mid] <= x)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
static int countSmallElements( int [,] mat,
int n, int x)
{
int row_no = binarySearchOnRow(mat,
0, n - 1, x);
if (row_no == -1)
return 0;
int col_no = binarySearchOnCol(mat,
0, n - 1, x, row_no);
return ((row_no) * n) + (col_no + 1);
}
public static void Main () {
int [,]mat = { { 1, 4, 7, 8 },
{ 10, 10, 12, 14 },
{ 15, 26, 30, 31 },
{ 31, 42, 46, 50 } };
int n = 4;
int x = 31;
Console.WriteLine( "Count = "
+ countSmallElements(mat, n, x));
}
}
|
PHP
<?php
function binarySearchOnRow( $mat , $l ,
$h , $x )
{
while ( $l <= $h ) {
$mid = floor (( $l + $h ) / 2);
if ( $mat [ $mid ][0] <= $x )
$l = $mid + 1;
else
$h = $mid - 1;
}
return $h ;
}
function binarySearchOnCol( $mat , $l ,
$h , $x , $row )
{
while ( $l <= $h ) {
$mid = floor (( $l + $h ) / 2);
if ( $mat [ $row ][ $mid ] <= $x )
$l = $mid + 1;
else
$h = $mid - 1;
}
return $h ;
}
function countSmallElements( $mat ,
$n , $x )
{
$row_no = binarySearchOnRow( $mat , 0,
$n - 1, $x );
if ( $row_no == -1)
return 0;
$col_no = binarySearchOnCol( $mat , 0,
$n - 1, $x , $row_no );
return floor (( $row_no ) * $n ) +
( $col_no + 1);
}
$mat = array ( array (1, 4, 7, 8),
array (10, 10, 12, 14),
array (15, 26, 30, 31),
array (31, 42, 46, 50));
$n = 4;
$x = 31;
echo "Count = " , countSmallElements ( $mat , $n , $x );
?>
|
Javascript
<script>
const SIZE = 100
function binarySearchOnRow(mat, l, h, x){
while (l <= h){
let mid = Math.floor((l + h) / 2)
if (mat[mid][0] <= x)
l = mid + 1
else
h = mid - 1
}
return h
}
function binarySearchOnCol(mat, l, h, x, row){
while (l <= h){
let mid = Math.floor((l + h) / 2)
if (mat[row][mid] <= x)
l = mid + 1
else
h = mid - 1
}
return h
}
function countSmallElements(mat, n, x){
let row_no = binarySearchOnRow(mat, 0, n - 1, x)
if (row_no == -1)
return 0
let col_no = binarySearchOnCol(mat, 0,
n - 1, x, row_no)
return ((row_no)*n) + (col_no + 1)
}
let mat = [ [ 1, 4, 7, 8 ],
[ 10, 10, 12, 14 ],
[ 15, 26, 30, 31 ],
[ 31, 42, 46, 50 ] ]
let n = 4
let x = 31
document.write( "Count = " + countSmallElements(mat, n, x))
</script>
|
Output:
Count = 13
Time Complexity: O(Log2n).
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...