Largest Subarray Length K - Problem
An array A is larger than some array B if for the first index i where A[i] != B[i], we have A[i] > B[i].
For example, consider 0-indexing: [1,3,2,4] > [1,2,2,4], since at index 1, 3 > 2. Similarly, [1,4,4,4] < [2,1,1,1], since at index 0, 1 < 2.
A subarray is a contiguous subsequence of the array. Given an integer array nums of distinct integers, return the largest subarray of nums of length k.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,4,3,2,5], k = 3
›
Output:
[4,3,2]
💡 Note:
Possible subarrays: [1,4,3], [4,3,2], [3,2,5]. Compare lexicographically: [4,3,2] > [1,4,3] (4 > 1) and [4,3,2] > [3,2,5] (4 > 3)
Example 2 — Larger Array
$
Input:
nums = [1,2,3,4], k = 2
›
Output:
[3,4]
💡 Note:
Possible subarrays: [1,2], [2,3], [3,4]. Compare: [3,4] > [2,3] (3 > 2) and [3,4] > [1,2] (3 > 1)
Example 3 — Single Element
$
Input:
nums = [1,4,3,2,5], k = 1
›
Output:
[5]
💡 Note:
All subarrays of length 1: [1], [4], [3], [2], [5]. The largest is [5] since 5 is the maximum element
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ k ≤ nums.length
- 1 ≤ nums[i] ≤ 109
- All integers in nums are distinct
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array [1,4,3,2,5] and k=3
2
Process
Find all subarrays of length 3 and compare lexicographically
3
Output
Return the largest: [4,3,2]
Key Takeaway
🎯 Key Insight: Lexicographic comparison means we compare element by element until we find a difference
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code