Kth Smallest Number in Multiplication Table | Practice | GeeksforGeeks

Given three integers M, N and K. Consider a grid of M * N, where mat[i][j] = i * j (1 based index). The task is to return the Kth smallest element in the M * N multiplication table.
 
Example 1:
Input:
M = 3, N = 3
K = 5
Output: 3
Explanation: 

The 5th smallest element is 3. 



Example 2:
Input:
M = 2, N = 3
K = 6
Output: 6 


 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function KthSmallest() which takes three integers as input and returns an integer as output.
Expected Time Complexity: O(M * log(M * N))
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= M, N <= 3 * 104
1 <= K <= M * N
+1
0ehxmsy49h3ayafg2srpmc30s5h21xtq6mdt7g472 months ago
bool small(int x,int m,int n, int k){       int co=0;       for(int i=1;i<=m;i++){           co+=min(x/i,n);       }       return co>=k;   }   int KthSmallest(int m, int n, int k) {       //Write your code here       int low=1,high=m*n;       while(low<high){         int x= (low+high)/2;           if(small(x,m,n,k)){               high=x;}           else               low=x+1;       }       return low;   }
0
vikashkumarhindu42 months ago
public int KthSmallest(int m, int n, int k) {
        //Write your code here
        int low = 1, high = m*n;
         while(low < high){
             int mid = low + (high-low)/2;
             int capacity = count(mid, m,n);
             if(capacity >= k) high = mid;
             else 
                low = mid + 1;
             
             
         }
         return low;
    }
    public int count(int mid, int n, int m){
        int count = 0;
        for(int i =1 ; i <=m; i++){
            int temp = Math.min(mid/i, n);
            count += temp;
        }
        return count;
    }
}

+1
amanpandey30073 months ago
Median in a row wise sorted matrix.
 
int KthSmallest(int m, int n, int k)    {       int low=1;       int high=m*n;            while(low<=high)       {           int mid=low+(high-low)/2;           int count=0;                      for(int i=1;i<=m;i++)           count+=min(n,mid/i);                      if(count>=k)           {               high=mid-1;           }           else           {               low=mid+1;           }                             }              return low;   }
0
oox7hxhe09xezluq19rsahkk2egq06mi7bimb4ug3 months ago
136417965
0
anutiger3 months ago
int l = 1;
        int h  = m * n;
        while(l <= h){
            if(l == h) return l;
            int mid = (l + h) / 2; 
            int cnt = 0;
            for(int i = 1; i <= m ; i ++){
                if(mid >= i * n)
                    cnt += n;
                else 
                    cnt += mid / i;
            }
            if(cnt < k) l = mid + 1;
            else h = mid;
        }
        return l;
+1
sbh698405 months ago
class Solution(object):
    def findKthNumber(self, m, n, k):
        #Write your code here
        lo,hi = 0,m*n+1
        while lo<hi:
            mid = (lo+hi)//2
            val = 0
            for i in range(m):
                val+=min(n,(mid//(i+1)))
            if val>=k:
                hi = mid
            else:
                lo = mid+1
        return hi
+2
ramussq5 months ago
Java code 
class Solution {public int getLowerNum(int m, int n, int target){int row = m - 1, col = 0;int res = 0;while(row >= 0 && col < n){if((row + 1) * (col + 1) > target) row--;else{res += row + 1;col++;}}return res;}public int KthSmallest(int m, int n, int k) {//Write your code hereint low = 1, high = m * n;while(low <= high){int mid = low + (high - low) / 2;int count = getLowerNum(m, n, mid);if(count < k) low = mid + 1;else high = mid - 1;}return low;}}
+3
udaytyagi01235 months ago
 
int KthSmallest(int m, int n, int k) {       int low=1;       int high=m*n;       int ans;       while(low<=high)       {           int count=0;           int mid=(low+high)/2;           for(int i=1;i<=m;i++)//find the number of smaller element than mid in each row           {               count=count+min(mid/i,n);           }           if(count>=k)           {               high=mid-1;               ans=mid;           }           else              low=mid+1;       }       return ans;   }
+3
anshumanj955 months ago
class Solution {
    public int KthSmallest(int m, int n, int k) {
        //Write your code here
        int low=1,high=m*n;
        while(low<high){
            int mid=(low+high)/2;
            if(smaller(mid,m,n,k))
                high=mid;
            else
                low=mid+1;
        }
        return low;
    }
    public boolean smaller(int num,int m,int n,int k){
        int count=0;
        for(int i=1;i<=m;i++)
            count+=Math.min(num/i,n);
        return count>=k;
    }
   
}
0
grvb15155 months ago
Funny part is that Python Code TLEs, and C++ code runs (both have the same logic).  
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.