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 **K ^{th} Smallest element** in the given Matrix.

**Examples:**

Input:N = 2, M = 3, K = 5Output:4Explanation:

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 5^{th}smallest element is4.Input:N = 1, M = 10, K = 8Output:8Explanation:

The matrix possible for given dimensions is {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}

Therefore, the 8^{th}smallest element is8.

**Naive Approach: **The simplest approach is to store all the elements of the matrix in an array and then find the **K ^{th}** smallest element by sorting the array.

**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
as**low****1**andas**high****N×M,**as the**K**smallest element lies between^{th}**1**and**N×M.** - Find the
**mid****low**elements**high***.* - If the number of elements
*less*thanis**mid***greater*than or equal to**K,**then updateto**high**as the**mid-1****Kth**smallest element lies betweenand**low****mid.** - If the number of elements
*less*thanis**mid***less*than**K,**then updateto**low**as the**mid+1****Kth**smallest element lies betweenand**mid****high.** - As the elements in the
row are the multiple of**ith**, the number of elements less than**i**in the**mid**row can be calculated easily by**ith**So, the time complexity to find the number of elements less than**min(mid/i, M).**can be done in only**mid**.**O(N)** - Perform binary search till
is**low***less*than or equal toand return**high**as the**high + 1****Kth**smallest element of the matrix**N×M.**

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define LL long long ` `// Function that returns true if the ` `// count of elements is less than mid ` `bool` `countLessThanMid(LL mid, LL N, ` ` ` `LL M, LL K) ` `{ ` ` ` `// To store count of elements ` ` ` `// less than mid ` ` ` `LL count = 0; ` ` ` `// Loop through each row ` ` ` `for` `(` `int` `i = 1; ` ` ` `i <= min((LL)N, mid); ++i) { ` ` ` `// Count elements less than ` ` ` `// mid in the ith row ` ` ` `count = count + min(mid / i, M); ` ` ` `} ` ` ` `if` `(count >= K) ` ` ` `return` `false` `; ` ` ` `else` ` ` `return` `true` `; ` `} ` `// Function that returns the Kth ` `// smallest element in the NxM ` `// Matrix after sorting in an array ` `LL findKthElement(LL N, LL M, LL K) ` `{ ` ` ` `// Initialize low and high ` ` ` `LL low = 1, high = N * M; ` ` ` `// Perform binary search ` ` ` `while` `(low <= high) { ` ` ` `// Find the mid ` ` ` `LL mid = low + (high - low) / 2; ` ` ` `// Check if the count of ` ` ` `// elements is less than mid ` ` ` `if` `(countLessThanMid(mid, N, M, K)) ` ` ` `low = mid + 1; ` ` ` `else` ` ` `high = mid - 1; ` ` ` `} ` ` ` `// Return Kth smallest element ` ` ` `// of the matrix ` ` ` `return` `high + 1; ` `} ` `// Driver Code ` `int` `main() ` `{ ` ` ` `LL N = 2, M = 3; ` ` ` `LL ` `int` `K = 5; ` ` ` `cout << findKthElement(N, M, K) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `class` `GFG{` ` ` `// Function that returns true if the ` `// count of elements is less than mid ` `public` `static` `boolean` `countLessThanMid(` `int` `mid, ` `int` `N, ` ` ` `int` `M, ` `int` `K) ` `{ ` ` ` ` ` `// To store count of elements ` ` ` `// less than mid ` ` ` `int` `count = ` `0` `; ` ` ` `// Loop through each row ` ` ` `for` `(` `int` `i = ` `1` `; ` ` ` `i <= Math.min(N, mid); ++i) ` ` ` `{ ` ` ` ` ` `// Count elements less than ` ` ` `// mid in the ith row ` ` ` `count = count + Math.min(mid / i, M); ` ` ` `} ` ` ` `if` `(count >= K) ` ` ` `return` `false` `; ` ` ` `else` ` ` `return` `true` `; ` `} ` `// Function that returns the Kth ` `// smallest element in the NxM ` `// Matrix after sorting in an array ` `public` `static` `int` `findKthElement(` `int` `N, ` `int` `M, ` `int` `K) ` `{ ` ` ` ` ` `// Initialize low and high ` ` ` `int` `low = ` `1` `, high = N * M; ` ` ` `// Perform binary search ` ` ` `while` `(low <= high) ` ` ` `{ ` ` ` ` ` `// Find the mid ` ` ` `int` `mid = low + (high - low) / ` `2` `; ` ` ` `// Check if the count of ` ` ` `// elements is less than mid ` ` ` `if` `(countLessThanMid(mid, N, M, K)) ` ` ` `low = mid + ` `1` `; ` ` ` `else` ` ` `high = mid - ` `1` `; ` ` ` `} ` ` ` `// Return Kth smallest element ` ` ` `// of the matrix ` ` ` `return` `high + ` `1` `; ` `} ` `// Driver code` `public` `static` `void` `main(String[] args) ` `{` ` ` `int` `N = ` `2` `, M = ` `3` `; ` ` ` `int` `K = ` `5` `; ` ` ` `System.out.println(findKthElement(N, M, K));` `}` `}` `// This code is contributed by divyeshrabadiya07` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` `# Function that returns true if the ` `# count of elements is less than mid ` `def` `countLessThanMid(mid, N, M, K):` ` ` ` ` `# To store count of elements ` ` ` `# less than mid ` ` ` `count ` `=` `0` ` ` `# Loop through each row ` ` ` `for` `i ` `in` `range` `(` `1` `, ` `min` `(N, mid) ` `+` `1` `):` ` ` `# Count elements less than ` ` ` `# mid in the ith row ` ` ` `count ` `=` `count ` `+` `min` `(mid ` `/` `/` `i, M)` ` ` ` ` `if` `(count >` `=` `K):` ` ` `return` `False` ` ` `else` `:` ` ` `return` `True` `# Function that returns the Kth ` `# smallest element in the NxM ` `# Matrix after sorting in an array ` `def` `findKthElement(N, M, K):` ` ` `# Initialize low and high ` ` ` `low ` `=` `1` ` ` `high ` `=` `N ` `*` `M` ` ` `# Perform binary search ` ` ` `while` `(low <` `=` `high):` ` ` `# Find the mid ` ` ` `mid ` `=` `low ` `+` `(high ` `-` `low) ` `/` `/` `2` ` ` `# Check if the count of ` ` ` `# elements is less than mid ` ` ` `if` `(countLessThanMid(mid, N, M, K)):` ` ` `low ` `=` `mid ` `+` `1` ` ` `else` `:` ` ` `high ` `=` `mid ` `-` `1` ` ` `# Return Kth smallest element ` ` ` `# of the matrix ` ` ` `return` `high ` `+` `1` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `N ` `=` `2` ` ` `M ` `=` `3` ` ` `K ` `=` `5` ` ` `print` `(findKthElement(N, M, K))` ` ` `# This code is contributed by Chitranayal` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approach ` `using` `System;` `class` `GFG{` ` ` `// Function that returns true if the ` `// count of elements is less than mid ` `public` `static` `bool` `countLessThanMid(` `int` `mid, ` `int` `N, ` ` ` `int` `M, ` `int` `K) ` `{ ` ` ` ` ` `// To store count of elements ` ` ` `// less than mid ` ` ` `int` `count = 0; ` ` ` `// Loop through each row ` ` ` `for` `(` `int` `i = 1; ` ` ` `i <= Math.Min(N, mid); ++i) ` ` ` `{ ` ` ` ` ` `// Count elements less than ` ` ` `// mid in the ith row ` ` ` `count = count + Math.Min(mid / i, M); ` ` ` `} ` ` ` `if` `(count >= K) ` ` ` `return` `false` `; ` ` ` `else` ` ` `return` `true` `; ` `} ` `// Function that returns the Kth ` `// smallest element in the NxM ` `// Matrix after sorting in an array ` `public` `static` `int` `findKthElement(` `int` `N, ` `int` `M,` ` ` `int` `K) ` `{ ` ` ` ` ` `// Initialize low and high ` ` ` `int` `low = 1, high = N * M; ` ` ` `// Perform binary search ` ` ` `while` `(low <= high) ` ` ` `{ ` ` ` ` ` `// Find the mid ` ` ` `int` `mid = low + (high - low) / 2; ` ` ` `// Check if the count of ` ` ` `// elements is less than mid ` ` ` `if` `(countLessThanMid(mid, N, M, K)) ` ` ` `low = mid + 1; ` ` ` `else` ` ` `high = mid - 1; ` ` ` `} ` ` ` `// Return Kth smallest element ` ` ` `// of the matrix ` ` ` `return` `high + 1; ` `} ` `// Driver code` `public` `static` `void` `Main(String[] args) ` `{` ` ` `int` `N = 2, M = 3; ` ` ` `int` `K = 5; ` ` ` `Console.WriteLine(findKthElement(N, M, K));` `}` `}` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

**Output: **

4

**Time Complexity:** O(N×log(N×M)) **Auxiliary Space: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the element at the specified index of a Spirally Filled Matrix
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Number of ways of cutting a Matrix such that atleast one cell is filled in each part
- Kth smallest element in a row-wise and column-wise sorted 2D array | Set 1
- Find the Kth smallest element in the sorted generated array
- Kth smallest element in the array using constant space when array can't be modified
- Kth smallest or largest element in unsorted Array | Set 4
- Kth Smallest element in a Perfect Binary Search Tree
- Lexicographically smallest string which differs from given strings at exactly K indices
- Find the dimensions of Right angled triangle
- Multiplication table till N rows where every Kth row is table of K upto Kth term
- Program to check if tank will overflow, underflow or filled in given time
- Number of containers that can be filled in the given time
- Count of vessels completely filled after a given time
- Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix
- Form smallest number using indices of numbers chosen from Array with sum less than S
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- Smallest element from all square submatrices of size K from a given Matrix
- Find all rectangles filled with 0
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.