K-th Smallest Prime Fraction
Problem Statement
You get a sorted array arr of distinct positive integers. The first value is 1, and every other value is a prime number.
Make a fraction from every pair of positions i < j: the top is arr[i] and the bottom is arr[j]. Return the k-th smallest of these fractions, as the two-element list [numerator, denominator]. Only pairs with i < j count, so every fraction is less than 1.
A sorted integer array arr, and an integer k.
A list [numerator, denominator] for the k-th smallest fraction.
The fractions in order are 1/5, 1/3, 2/5, 1/2, 3/5, 2/3. The third one is 2/5.
There is only one fraction, 1/7.
- • 2 ≤ arr.length ≤ 1000
- • 1 ≤ arr[i] ≤ 3 · 10^4
- • arr[0] == 1
- • arr[i] is a prime number for i > 0
- • All the numbers of arr are unique and sorted in strictly increasing order
- • 1 ≤ k ≤ arr.length · (arr.length − 1) / 2
- • Follow-up: Can you solve the problem with better than O(n^2) complexity?
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
List every arr[i]/arr[j] with i < j, sort them by value, and read the k-th
Min-heap with one row per numerator, starting at the largest denominator
Make every fraction with i < j, sort them by value, and return the one at position k − 1. Sorting all of them just to read one is wasteful.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like K-way Merge translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.