Kth Smallest Element of a Matrix of given dimensions filled with product of indices
Last Updated :
18 Sep, 2023
Given an integer K and a matrix of size N x M, where each matrix element is equal to the product of its indices(i * j), the task is to find the Kth Smallest element in the given Matrix.
Examples:
Input: N = 2, M = 3, K = 5
Output: 4
Explanation:
The matrix possible for given dimensions is {{1, 2, 3}, {2, 4, 6}}
Sorted order of the matrix elements: {1, 2, 2, 3, 4, 6}
Therefore, the 5th smallest element is 4.
Input: N = 1, M = 10, K = 8
Output: 8
Explanation:
The matrix possible for given dimensions is {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}
Therefore, the 8th smallest element is 8.
Naive Approach: The simplest approach is to store all the elements of the matrix in an array and then find the Kth smallest element by sorting the array.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int kthSmallest(vector<vector< int >> &mat, int n, int m, int k) {
int arr[n * m];
int index = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
arr[index++] = mat[i][j];
}
}
sort(arr, arr + n * m);
return arr[k - 1];
}
int main() {
int n = 2, m = 3, k = 5;
vector<vector< int >> mat(n, vector< int >(m,0));
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
mat[i][j] = (i + 1) * (j + 1);
}
}
cout << kthSmallest(mat, n, m, k) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int kthSmallest( int [][] mat, int n, int m,
int k)
{
int [] arr = new int [n * m];
int index = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < m; j++) {
arr[index++] = mat[i][j];
}
}
Arrays.sort(arr);
return arr[k - 1 ];
}
public static void main(String[] args)
{
int n = 2 , m = 3 , k = 5 ;
int [][] mat = new int [n][m];
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < m; j++) {
mat[i][j] = (i + 1 ) * (j + 1 );
}
}
System.out.println(kthSmallest(mat, n, m, k));
}
}
|
Python
def GFG(mat, n, m, k):
arr = []
for i in range (n):
for j in range (m):
arr.append(mat[i][j])
arr.sort()
return arr[k - 1 ]
if __name__ = = "__main__" :
n = 2
m = 3
k = 5
mat = [[ 0 for j in range (m)] for i in range (n)]
for i in range (n):
for j in range (m):
mat[i][j] = (i + 1 ) * (j + 1 )
print (GFG(mat, n, m, k))
|
C#
using System;
using System.Linq;
class Program {
static int kthSmallest( int [][] mat, int n, int m, int k)
{
int [] arr = new int [n * m];
int index = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
arr[index++] = mat[i][j];
}
}
Array.Sort(arr);
return arr[k - 1];
}
static void Main( string [] args)
{
int n = 2, m = 3, k = 5;
int [][] mat = new int [n][];
for ( int i = 0; i < n; i++) {
mat[i] = new int [m];
for ( int j = 0; j < m; j++) {
mat[i][j] = (i + 1) * (j + 1);
}
}
Console.WriteLine(kthSmallest(mat, n, m, k));
}
}
|
Javascript
function kthSmallest(mat, n, m, k) {
let arr = new Array(n * m);
let index = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
arr[index++] = mat[i][j];
}
}
arr.sort((a, b) => a - b);
return arr[k - 1];
}
let n = 2, m = 3, k = 5;
let mat = [];
for (let i = 0; i < n; i++) {
mat.push( new Array(m));
for (let j = 0; j < m; j++) {
mat[i][j] = (i + 1) * (j + 1);
}
}
console.log(kthSmallest(mat, n, m, k));
|
Time Complexity: O(N×M×log(N×M))
Auxiliary Space: O(N×M)
Efficient Approach:
To optimize the naive approach the idea is to use the Binary Search algorithm. Follow the steps below to solve the problem:
- Initialize low as 1 and high as N×M, as the Kth smallest element lies between 1 and N×M.
- Find the mid element between the low and high elements.
- If the number of elements less than mid is greater than or equal to K, then update high to mid-1 as the Kth smallest element lies between low and mid.
- If the number of elements less than mid is less than K, then update low to mid+1 as the Kth smallest element lies between mid and high.
- As the elements in the ith row are the multiple of i, the number of elements less than mid in the ith row can be calculated easily by min(mid/i, M). So, the time complexity to find the number of elements less than mid can be done in only O(N).
- Perform binary search till low is less than or equal to high and return high + 1 as the Kth smallest element of the matrix N×M.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define LL long long
bool countLessThanMid(LL mid, LL N,
LL M, LL K)
{
LL count = 0;
for ( int i = 1;
i <= min((LL)N, mid); ++i) {
count = count + min(mid / i, M);
}
if (count >= K)
return false ;
else
return true ;
}
LL findKthElement(LL N, LL M, LL K)
{
LL low = 1, high = N * M;
while (low <= high) {
LL mid = low + (high - low) / 2;
if (countLessThanMid(mid, N, M, K))
low = mid + 1;
else
high = mid - 1;
}
return high + 1;
}
int main()
{
LL N = 2, M = 3;
LL int K = 5;
cout << findKthElement(N, M, K) << endl;
return 0;
}
|
Java
class GFG{
public static boolean countLessThanMid( int mid, int N,
int M, int K)
{
int count = 0 ;
for ( int i = 1 ;
i <= Math.min(N, mid); ++i)
{
count = count + Math.min(mid / i, M);
}
if (count >= K)
return false ;
else
return true ;
}
public static int findKthElement( int N, int M, int K)
{
int low = 1 , high = N * M;
while (low <= high)
{
int mid = low + (high - low) / 2 ;
if (countLessThanMid(mid, N, M, K))
low = mid + 1 ;
else
high = mid - 1 ;
}
return high + 1 ;
}
public static void main(String[] args)
{
int N = 2 , M = 3 ;
int K = 5 ;
System.out.println(findKthElement(N, M, K));
}
}
|
Python3
def countLessThanMid(mid, N, M, K):
count = 0
for i in range ( 1 , min (N, mid) + 1 ):
count = count + min (mid / / i, M)
if (count > = K):
return False
else :
return True
def findKthElement(N, M, K):
low = 1
high = N * M
while (low < = high):
mid = low + (high - low) / / 2
if (countLessThanMid(mid, N, M, K)):
low = mid + 1
else :
high = mid - 1
return high + 1
if __name__ = = "__main__" :
N = 2
M = 3
K = 5
print (findKthElement(N, M, K))
|
C#
using System;
class GFG{
public static bool countLessThanMid( int mid, int N,
int M, int K)
{
int count = 0;
for ( int i = 1;
i <= Math.Min(N, mid); ++i)
{
count = count + Math.Min(mid / i, M);
}
if (count >= K)
return false ;
else
return true ;
}
public static int findKthElement( int N, int M,
int K)
{
int low = 1, high = N * M;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (countLessThanMid(mid, N, M, K))
low = mid + 1;
else
high = mid - 1;
}
return high + 1;
}
public static void Main(String[] args)
{
int N = 2, M = 3;
int K = 5;
Console.WriteLine(findKthElement(N, M, K));
}
}
|
Javascript
<script>
function countLessThanMid(mid, N, M, K)
{
let count = 0;
for (let i = 1;
i <= Math.min(N, mid); ++i) {
count = count + Math.min(parseInt(mid / i), M);
}
if (count >= K)
return false ;
else
return true ;
}
function findKthElement(N, M, K)
{
let low = 1, high = N * M;
while (low <= high) {
let mid = low + parseInt((high - low) / 2);
if (countLessThanMid(mid, N, M, K))
low = mid + 1;
else
high = mid - 1;
}
return high + 1;
}
let N = 2, M = 3;
let K = 5;
document.write(findKthElement(N, M, K));
</script>
|
Output:
4
Time Complexity: O(N×log(N×M))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...