2Brute2Best
🔥0

Find the Kth Largest Integer in the Array

Medium·LeetCode #1985 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given an array nums of strings. Each string is a whole number written in digits. A number can have up to 100 digits, so it can be too big for any C++ integer type. No number has leading zeros.

Return the kth largest number, as a string. Equal numbers count as separate positions, as they do in Kth Largest Element in an Array.

Input

nums, an array of digit strings, and k.

Output

The kth largest number, as a string.

Example 1
Input: nums = ["3", "6", "7", "10"], k = 4
Output: "3"

Sorted from biggest: 10, 7, 6, 3. The 4th is 3. The number 10 is bigger than 7, even though it starts with 1.

Example 2
Input: nums = ["2", "21", "12", "1"], k = 3
Output: "2"

Sorted from biggest: 21, 12, 2, 1. The 3rd is 2.

Example 3
Input: nums = ["0", "0"], k = 2
Output: "0"

Both numbers are 0. Equal numbers still take separate positions, so the 2nd largest is 0.

Constraints
  • • 1 ≤ k ≤ nums.length ≤ 10^4
  • • 1 ≤ nums[i].length ≤ 100
  • • nums[i] consists of only digits
  • • nums[i] will not have any leading zeros
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“as strings, up to 100 digits”
→ A 100-digit number does not fit in int or long long. Compare the text, not a converted value.
“kth largest”
→ The same min-heap of size k as Kth Largest Element. Only the comparison changes.
“no leading zeros”
→ Length alone decides the order between numbers of different lengths.
03

Approach Complexity Ladder

Brute Force — sort with a number comparatorO(N log N · L)O(1)

Sort biggest first, comparing length before text, then return index k − 1

Optimal — min-heap of size k with the same comparatorO(N log K · L)O(K · L)

Keep the k largest numbers in a min-heap; its top is the kth largest

Brute Force Intuition

Sort nums from biggest to smallest. The comparator checks length first, then the text. After that, nums[k − 1] is the answer.

Recognition rule for this sub-pattern:

For K largest keep a min-heap of size K; the top is the Kth largest.
Remember this · Heap — Top K
Trigger
"K largest / smallest / most frequent / closest", kth element, streaming top-K.
Think
Do I really need everything sorted, or only the best K?
Key question
Min-heap or max-heap of size K — which one lets me kick out the worst candidate?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Heap — Top K translate into production architectures and low-level memory mechanics.