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 i and j such that 2 * nums[i] <= nums[j], then mark i and j.

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
Maximum Marked Indices: Find Optimal Pairs3524index 0index 1index 2index 3Condition: 2 * nums[i] ≤ nums[j]2 * 2 = 4 ≤ 4 ✓Valid pair: (2,3) → mark indices 2 and 3Check other pairs: 2*3=6>5, 2*3=6>4, 2*5=10>4Maximum Marked: 2 indices🎯 **Key Insight:** Sort first, then use greedy pairing for optimal result
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
Asked in
Google 15 Amazon 12 Microsoft 8
12.5K Views
Medium Frequency
~25 min Avg. Time
285 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen