Skip to main content
Back to problems
#954
Medium Algorithms

Array of doubled pairs

Array Hash Table Greedy Sorting
39.7% acceptance
Feb 25, 2026
1571
185
Given an integer array of even length arr, 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.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn can_reorder_doubled(arr: Vec<i32>) -> bool {
    let mut count = std::collections::HashMap::<i32, i32>::new();
    for &x in &arr { *count.entry(x).or_insert(0) += 1; }
    let mut keys: Vec<i32> = count.keys().copied().collect();
    keys.sort_by_key(|&x| x.abs());
    for k in keys {
      let cnt = *count.get(&k).unwrap_or(&0);
      if cnt == 0 { continue; }
      let double = k * 2;
      let cnt2 = count.entry(double).or_insert(0);
      if *cnt2 < cnt { return false; }
      *cnt2 -= cnt;
      *count.get_mut(&k).unwrap() = 0;
    }
    true
  }
}