Array of Doubled Pairs - Problem

Given an integer array arr of even length, return true if it is possible to reorder arr such that arr[2 * i + 1] = 2 * arr[2 * i] for every 0 <= i < len(arr) / 2, or false otherwise.

In other words, we need to check if we can pair up all elements such that each pair contains a number and its double.

Input & Output

Example 1 — Basic Valid Case
$ Input: arr = [1,2,4,16,8,4]
Output: true
💡 Note: We can reorder as [1,2,4,8,4,16] where arr[1]=2×arr[0], arr[3]=2×arr[2], arr[5]=2×arr[4]
Example 2 — Impossible Case
$ Input: arr = [2,1,2,6]
Output: false
💡 Note: We have two 2s but only one 1 and one 6. We can pair (1,2) but the remaining 2 cannot pair with 6 since 6≠2×2
Example 3 — Zeros
$ Input: arr = [4,-2,2,-4]
Output: true
💡 Note: We can pair (-2,-4) since -4 = 2×(-2) and (2,4) since 4 = 2×2

Constraints

  • 1 ≤ arr.length ≤ 1000
  • arr.length is even
  • -1000 ≤ arr[i] ≤ 1000

Visualization

Tap to expand
Array of Doubled Pairs: Pairing Challenge1241684Goal: Pair each element with its double(1, 2)(4, 8)(4, 16)2 = 2 × 1 ✓8 = 2 × 4 ✓16 = 2 × 4 ✓Result: true - All pairs valid!
Understanding the Visualization
1
Input
Array [1,2,4,16,8,4] needs to be paired
2
Find Pairs
Match each number with its double: (1,2), (4,8), (4,16)
3
Check Valid
All elements can be paired successfully
Key Takeaway
🎯 Key Insight: Count frequencies and greedily match each number with its double, handling zeros as special case
Asked in
Google 42 Facebook 28 Amazon 35
32.0K Views
Medium Frequency
~25 min Avg. Time
890 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