Problem
You are given a sorted integer array arr
containing 1
and prime numbers, where all the integers of arr
are unique. You are also given an integer k
.
For every i
and j
where 0 <= i < j < arr.length
, we consider the fraction arr[i] / arr[j]
.
Return the kth
smallest fraction considered. Return your answer as an array of integers of size 2
, where answer[0] == arr[i]
and answer[1] == arr[j]
.
Examples
Example 1:
|
|
Example 2:
|
|
Solution
Video Explanation
Video solution here:
Method 1 - Creating all fractions and use Minheap
Code
|
|
Complexity
- ⏰ Time complexity:
O(N^2 logn)
- 🧺 Space complexity:
O(n^2)
Method 2 - Using provided sorted array efficiently and minHeap 🏆
The optimized method aims to leverage the point that array is sorted. So, we can find the k-th smallest fraction from all possible pairs in a sorted array without generating all combinations upfront.
Algorithm
- Initialize minHeap based on the fractional value. We don’t have. to double compare. Let nr represents numerator and dr represents denominator, this implies if
nr0/dr0 > nr1/dr1
⇨nr0*dr1 > nr1 * dr0
- Push all the smallest possible fraction by pushing all numerator with array’s largest value as denominator to the minHeap.
- Extract the smallest fraction from minHeap, and replace it with the next fraction by using same numerator, but just smaller denominator
- Continue this step 3, till we have extracted
k
elements, and kth’ element is our answer
- Continue this step 3, till we have extracted
Dry Run
- We create
minHeap
- We push all pairs as
{nrIdx, drIdx}
⇨ numerator and denominator index - We push all numerators from0
ton-2
and last indexn-1
as denominators - Now, we poll each pair one by one. We poll out numerator and denominator pair. If the we have reached
k
, we return current answer- and if we have not reached k, we push new
{nrIdex, drIdx - 1}
providednrIdx != (drIdx - 1)
.
- and if we have not reached k, we push new
Code
|
|
Complexity
- ⏰ Time complexity:
O(n log n)
as it takesO(n)
to build minHeap andO(k log n)
to pick firstk
elements from heap. - 🧺 Space complexity:
O(n)