Find the Maximum Number of Marked Indices - Problem

Imagine you're organizing pairs of numbers where one number is at least double the other! You have an array of integers, and your goal is to create as many valid pairs as possible.

The Challenge: Given a 0-indexed integer array nums, you need to find the maximum number of indices you can mark by pairing them strategically.

Pairing Rule: You can pick two different unmarked indices i and j where 2 * nums[i] ≀ nums[j], then mark both indices. This means nums[j] must be at least twice as large as nums[i].

Goal: Return the maximum possible number of marked indices. Since each successful pairing marks 2 indices, you want to maximize the number of valid pairs you can form.

Example: In array [3, 5, 2, 4], you can pair index 2 (value 2) with index 1 (value 5) since 2 * 2 ≀ 5, marking 2 indices total.

Input & Output

example_1.py β€” Basic Pairing
$ Input: [3,5,2,4]
β€Ί Output: 2
πŸ’‘ Note: We can pair index 2 (value 2) with index 1 (value 5) since 2 * 2 = 4 ≀ 5. This marks 2 indices. We cannot form any more pairs with the remaining elements [3,4] since 2 * 3 = 6 > 4.
example_2.py β€” Multiple Pairs
$ Input: [1,3,2,4]
β€Ί Output: 4
πŸ’‘ Note: After sorting: [1,2,3,4]. We can pair 1 with 2 (since 2*1=2 ≀ 2), and pair 3 with 4 (since 2*3=6 > 4, but we can't pair them). Actually, we pair 1 with 3 and 2 with 4, marking all 4 indices.
example_3.py β€” No Valid Pairs
$ Input: [1,1]
β€Ί Output: 2
πŸ’‘ Note: We can pair the two 1's together since 2 * 1 = 2 β‰₯ 1. Both indices get marked, so the answer is 2.

Constraints

  • 1 ≀ nums.length ≀ 105
  • 1 ≀ nums[i] ≀ 109
  • nums.length is always even (important for optimal pairing)

Visualization

Tap to expand
πŸ•ΊπŸ’ƒ Dance Partner MatchingStep 1: Line up dancers by heightH:2H:3H:5H:8Step 2: Apply the pairing rule (tall β‰₯ 2 Γ— short)Partner?5 β‰₯ 2Γ—2 βœ“PAIR 1Partner?8 β‰₯ 2Γ—3? 8β‰₯6 βœ“PAIR 2Result: All 4 dancers paired! πŸŽ‰πŸ’‘ Greedy Strategy Works:β€’ Always pair shortest available with shortest valid tall partnerβ€’ This leaves larger dancers available for future pairingsβ€’ Maximizes total number of successful pairsTime: O(n log n) | Space: O(1)
Understanding the Visualization
1
Line Up Dancers
Sort all dancers by height from shortest to tallest
2
Greedy Pairing
Always pair the shortest available dancer with the shortest compatible tall dancer
3
Maximize Pairs
This greedy strategy ensures we get the maximum number of dance pairs possible
Key Takeaway
🎯 Key Insight: The greedy approach of pairing the smallest unused number with the smallest valid partner (that's at least twice as large) guarantees the maximum number of pairs. This works because using a larger partner wastes future pairing opportunities.
Asked in
Google 42 Meta 38 Amazon 31 Microsoft 29
43.2K Views
Medium Frequency
~18 min Avg. Time
1.8K 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