Find the Maximum Number of Marked Indices - Problem
You are given a 0-indexed integer array nums.
Initially, all of the indices are unmarked. You are allowed to make this operation any number of times:
- Pick two different unmarked indices
iandjsuch that2 * nums[i] <= nums[j], then markiandj.
Return the maximum possible number of marked indices in nums using the above operation any number of times.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [3,5,2,4]
›
Output:
2
💡 Note:
We can mark indices 2 and 3 since 2 * nums[2] = 2 * 2 = 4 ≤ nums[3] = 4. No other valid pairs exist.
Example 2 — Multiple Pairs Possible
$
Input:
nums = [1,3,2,4]
›
Output:
2
💡 Note:
We can mark indices 0 and 1 since 2 * nums[0] = 2 * 1 = 2 ≤ nums[1] = 3. Alternatively, we could mark indices 2 and 3 since 2 * nums[2] = 2 * 2 = 4 ≤ nums[3] = 4. Both give 2 marked indices.
Example 3 — No Valid Pairs
$
Input:
nums = [7,6,8]
›
Output:
0
💡 Note:
No pair (i,j) exists where 2 * nums[i] ≤ nums[j], so no indices can be marked.
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
[3,5,2,4] - find pairs satisfying condition
2
Valid Pairs
Check all pairs (i,j) where 2*nums[i] ≤ nums[j]
3
Maximum Marked
Return maximum number of indices that can be marked
Key Takeaway
🎯 Key Insight: Sorting enables greedy two-pointer matching for optimal pairing
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code