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++
// C++ implementation to count elements smaller than
// or equal to x in a sorted matrix
#include <bits/stdc++.h>
using namespace std;
#define SIZE 100
// function returns the row index no of largest element
// smaller than equal to 'x' in first column of mat[][].
// For duplicates it returns the last row index no of
// occurrence of required element 'x'. If no such element
// exits then it returns -1.
int binarySearchOnRow(int mat[SIZE][SIZE],
int l, int h, int x)
{
while (l <= h) {
int mid = (l + h) / 2;
// if 'x' is greater than or equal to mat[mid][0],
// then search in mat[mid+1...h][0]
if (mat[mid][0] <= x)
l = mid + 1;
// else search in mat[l...mid-1][0]
else
h = mid - 1;
}
// required row index number
return h;
}
// function returns the column index no of largest element
// smaller than equal to 'x' in mat[row][].
// For duplicates it returns the last column index no of
// occurrence of required element 'x'.
int binarySearchOnCol(int mat[SIZE][SIZE],
int l, int h, int x, int row)
{
while (l <= h) {
int mid = (l + h) / 2;
// if 'x' is greater than or equal to mat[row][mid],
// then search in mat[row][mid+1...h]
if (mat[row][mid] <= x)
l = mid + 1;
// else search in mat[row][l...mid-1]
else
h = mid - 1;
}
// required column index number
return h;
}
// function to count elements smaller than
// or equal to x in a sorted matrix
int countSmallElements(int mat[SIZE][SIZE],
int n, int x)
{
// to get the row index number of the largest element
// smaller than equal to 'x' in mat[][]
int row_no = binarySearchOnRow(mat, 0, n - 1, x);
// if no such row exists
if (row_no == -1)
return 0;
// to get the column index number of the largest element
// smaller than equal to 'x' in mat[row_no][]
int col_no = binarySearchOnCol(mat, 0, n - 1, x, row_no);
// required count of elements
return ((row_no)*n) + (col_no + 1);
}
// Driver program to test above
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
//Java implementation to count elements
// smaller than or equal to x in a sorted matrix
import java.io.*;
class GFG {
static int SIZE=100;
// function returns the row index
//no of largest element smaller than
// equal to 'x' in first column of
//mat[][].For duplicates it returns
//the last row index no of
// occurrence of required element
// 'x'. If no such element
// exits then it returns -1.
static int binarySearchOnRow(int[][] mat,
int l, int h, int x)
{
while (l <= h) {
int mid = (l + h) / 2;
// if 'x' is greater than or
// equal to mat[mid][0],then
// search in mat[mid+1...h][0]
if (mat[mid][0] <= x)
l = mid + 1;
// else search in mat[l...mid-1][0]
else
h = mid - 1;
}
// required row index number
return h;
}
// function returns the column index
// no of largest element
// smaller than equal to 'x' in
// mat[row][].For duplicates it returns
// the last column index no of
// occurrence of required element 'x'.
static int binarySearchOnCol(int[][] mat,
int l, int h, int x, int row)
{
while (l <= h) {
int mid = (l + h) / 2;
// if 'x' is greater than or
// equal to mat[row][mid], then
// search in mat[row][mid+1...h]
if (mat[row][mid] <= x)
l = mid + 1;
// else search in mat[row][l...mid-1]
else
h = mid - 1;
}
// required column index number
return h;
}
// function to count elements smaller than
// or equal to x in a sorted matrix
static int countSmallElements(int[][] mat,
int n, int x)
{
// to get the row index number of the largest
// element smaller than equal to 'x' in mat[][]
int row_no = binarySearchOnRow(mat,
0, n - 1, x);
// if no such row exists
if (row_no == -1)
return 0;
// to get the column index number of
// the largest element smaller than
// equal to 'x' in mat[row_no][]
int col_no = binarySearchOnCol(mat,
0, n - 1, x, row_no);
// required count of elements
return ((row_no) * n) + (col_no + 1);
}
// Driver code
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));
}
}
// This code is contributed by Gitanjali.
Python3
# Python implementation to
# count elements smaller than
# or equal to x in a sorted matrix
SIZE = 100
# function returns the row index
# no of largest element
# smaller than equal to 'x' in
# first column of mat[][].
# For duplicates it returns the
# last row index no of
# occurrence of required element
# 'x'. If no such element
# exits then it returns -1.
def binarySearchOnRow(mat, l, h, x):
while l <= h:
mid = (l + h) // 2
# if 'x' is greater than or
# equal to mat[mid][0],
# then search in mat[mid+1...h][0]
if mat[mid][0] <= x:
l = mid + 1
# else search in mat[l...mid-1][0]
else:
h = mid - 1
# required row index number
return h
# function returns the column
# index no of largest element
# smaller than equal to 'x'
# in mat[row][].
# For duplicates it returns the
# last column index no of
# occurrence of required element 'x'.
def binarySearchOnCol(mat, l, h, x, row):
while l <= h:
mid = (l + h) // 2
# if 'x' is greater than or
# equal to mat[row][mid],
# then search in mat[row][mid+1...h]
if mat[row][mid] <= x:
l = mid + 1
# else search in mat[row][l...mid-1]
else:
h = mid - 1
# required column index number
return h
# function to count elements smaller than
# or equal to x in a sorted matrix
def countSmallElements(mat, n, x):
# to get the row index number
# of the largest element
# smaller than equal to 'x' in mat[][]
row_no = binarySearchOnRow(mat, 0, n - 1, x)
# if no such row exists
if row_no == -1:
return 0
# to get the column index number
# of the largest element
# smaller than equal to 'x' in mat[row_no][]
col_no = binarySearchOnCol(mat, 0,
n - 1, x, row_no)
# required count of elements
return ((row_no)*n) + (col_no + 1)
# Driver program to test above
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)))
# This code is contributed by Ansu Kumari.
C#
//Java implementation to count elements
// smaller than or equal to x in a sorted matrix
using System;
class GFG {
//static int SIZE=100;
// function returns the row index
//no of largest element smaller than
// equal to 'x' in first column of
//mat[][].For duplicates it returns
//the last row index no of
// occurrence of required element
// 'x'. If no such element
// exits then it returns -1.
static int binarySearchOnRow(int [,] mat, int l,
int h, int x)
{
while (l <= h) {
int mid = (l + h) / 2;
// if 'x' is greater than or
// equal to mat[mid][0],then
// search in mat[mid+1...h][0]
if (mat[mid,0] <= x)
l = mid + 1;
// else search in mat[l...mid-1][0]
else
h = mid - 1;
}
// required row index number
return h;
}
// function returns the column index
// no of largest element
// smaller than equal to 'x' in
// mat[row][].For duplicates it returns
// the last column index no of
// occurrence of required element 'x'.
static int binarySearchOnCol(int [,]mat, int l,
int h, int x, int row)
{
while (l <= h) {
int mid = (l + h) / 2;
// if 'x' is greater than or
// equal to mat[row][mid], then
// search in mat[row][mid+1...h]
if (mat[row,mid] <= x)
l = mid + 1;
// else search in mat[row][l...mid-1]
else
h = mid - 1;
}
// required column index number
return h;
}
// function to count elements smaller than
// or equal to x in a sorted matrix
static int countSmallElements(int[,] mat,
int n, int x)
{
// to get the row index number of the largest
// element smaller than equal to 'x' in mat[][]
int row_no = binarySearchOnRow(mat,
0, n - 1, x);
// if no such row exists
if (row_no == -1)
return 0;
// to get the column index number of
// the largest element smaller than
// equal to 'x' in mat[row_no][]
int col_no = binarySearchOnCol(mat,
0, n - 1, x, row_no);
// required count of elements
return ((row_no) * n) + (col_no + 1);
}
// Driver code
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));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP implementation to count
// elements smaller than or
// equal to x in a sorted matrix
// function returns the row index
// no of largest element smaller
// than equal to 'x' in first
// column of mat[][]. For duplicates
// it returns the last row index no.
// of occurrence of required element
// 'x'. If no such element exits then
// it returns -1.
function binarySearchOnRow($mat, $l,
$h, $x)
{
while ($l <= $h) {
$mid = floor(($l + $h) / 2);
// if 'x' is greater than
// or equal to mat[mid][0],
// then search in
// mat[mid+1...h][0]
if ($mat[$mid][0] <= $x)
$l = $mid + 1;
// else search in
// mat[l...mid-1][0]
else
$h = $mid - 1;
}
// required row index number
return $h;
}
// function returns the column
// index no of largest element
// smaller than equal to 'x'
// in mat[row][]. For duplicates
// it returns the last column
// index no of occurrence of
// required element 'x'.
function binarySearchOnCol($mat, $l,
$h, $x, $row)
{
while ($l <= $h) {
$mid = floor(($l + $h) / 2);
// if 'x' is greater than or
// equal to mat[row][mid],
// then search in
// mat[row][mid+1...h]
if ($mat[$row][$mid] <= $x)
$l = $mid + 1;
// else search in
// mat[row][l...mid-1]
else
$h = $mid - 1;
}
// required column
// index number
return $h;
}
// function to count elements
// smaller than or equal to x
// in a sorted matrix
function countSmallElements($mat,
$n, $x)
{
// to get the row index number
// of the largest element
// smaller than equal to 'x'
// in mat[][]
$row_no = binarySearchOnRow($mat, 0,
$n - 1, $x);
// if no such row exists
if ($row_no == -1)
return 0;
// to get the column index
// number of the largest element
// smaller than equal to 'x'
// in mat[row_no][]
$col_no = binarySearchOnCol($mat, 0,
$n - 1, $x, $row_no);
// required count of elements
return floor(($row_no) * $n) +
($col_no + 1);
}
// Driver Code
$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);
// This code is contributed by nitin mittal.
?>
JavaScript
<script>
// JavaScript implementation to
// count elements smaller than
// or equal to x in a sorted matrix
const SIZE = 100
// function returns the row index
// no of largest element
// smaller than equal to 'x' in
// first column of mat[][].
// For duplicates it returns the
// last row index no of
// occurrence of required element
// 'x'. If no such element
// exits then it returns -1.
function binarySearchOnRow(mat, l, h, x){
while(l <= h){
let mid = Math.floor((l + h) / 2)
// if 'x' is greater than or
// equal to mat[mid][0],
// then search in mat[mid+1...h][0]
if(mat[mid][0] <= x)
l = mid + 1
// else search in mat[l...mid-1][0]
else
h = mid - 1
}
// required row index number
return h
}
// function returns the column
// index no of largest element
// smaller than equal to 'x'
// in mat[row][].
// For duplicates it returns the
// last column index no of
// occurrence of required element 'x'.
function binarySearchOnCol(mat, l, h, x, row){
while(l <= h){
let mid = Math.floor((l + h) / 2)
// if 'x' is greater than or
// equal to mat[row][mid],
// then search in mat[row][mid+1...h]
if(mat[row][mid] <= x)
l = mid + 1
// else search in mat[row][l...mid-1]
else
h = mid - 1
}
// required column index number
return h
}
// function to count elements smaller than
// or equal to x in a sorted matrix
function countSmallElements(mat, n, x){
// to get the row index number
// of the largest element
// smaller than equal to 'x' in mat[][]
let row_no = binarySearchOnRow(mat, 0, n - 1, x)
// if no such row exists
if(row_no == -1)
return 0
// to get the column index number
// of the largest element
// smaller than equal to 'x' in mat[row_no][]
let col_no = binarySearchOnCol(mat, 0,
n - 1, x, row_no)
// required count of elements
return ((row_no)*n) + (col_no + 1)
}
// Driver program to test above
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))
// This code is contributed by shinjanpatra
</script>
Output:
Count = 13
Time Complexity: O(Log2n).
Auxiliary space: O(1)
Similar Reads
Count of smaller or equal elements in sorted array Given a sorted array of size n. Find a number of elements that are less than or equal to a given element. Examples: Input : arr[] = {1, 2, 4, 5, 8, 10} key = 9 Output : 5 Elements less than or equal to 9 are 1, 2, 4, 5, 8 therefore result will be 5. Input : arr[] = {1, 2, 2, 2, 5, 7, 9} key = 2 Outp
15+ min read
Count elements less than or equal to a given value in a sorted rotated array Given a sorted array of n distinct integers rotated at some point. Given a value x. The problem is to count all the elements in the array which are less than or equal to x. Examples: Input : arr[] = {4, 5, 8, 1, 3}, x = 6 Output : 4 Input : arr[] = {6, 10, 12, 15, 2, 4, 5}, x = 14 Output : 6 Naive A
15 min read
Count entries equal to x in a special matrix You are given a square matrix (matrix[][]) of order n, where matrix[i][j] = i*j. Find the number of cells which have entry equal to a given number x. NOte : Indexing of matrix starts from 1, i.e. 1<= i,j <= n. Examples : Input : matrix[][] = {1, 2, 3, 4, 2, 4, 6, 8, 3, 6, 9, 12, 4, 8, 12, 16}
6 min read
Count sub-arrays which have elements less than or equal to X Given an array of n elements and an integer X. Count the number of sub-arrays of this array which have all elements less than or equal to X. Examples: Input : arr[] = {1, 5, 7, 8, 2, 3, 9} X = 6 Output : 6 Explanation : Sub-arrays are {1}, {5}, {2}, {3}, {1, 5}, {2, 3} Input : arr[] = {1, 10, 12, 4,
15 min read
Search element in a sorted matrix Given a sorted matrix mat[][] of size nxm and an element x, the task is to find if x is present in the matrix or not. 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, the first element of row i is greater than or equal
13 min read
Count of submatrix with sum X in a given Matrix Given a matrix mat[][] of size n*m and an integer x, Find the number of sub-squares in the matrix with sum of elements equal to x.Examples: Input: mat[][] = [[2, 4, 7, 8, 10], x = 10 [3, 1, 1, 1, 1], [9, 11, 1, 2, 1], [12, -17, 1, 1, 1]]Output: 3 Explanation: The sub-squares are colored in the matri
13 min read