Array of Doubled Pairs

MediumGreedyhashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an integer array arr of **even** length. Decide whether its elements can be split into arr.length / 2 pairs where one element of each pair is exactly **twice** the other. Equivalently, return true if arr can be reordered so that arr[2i + 1] == 2 * arr[2i] for every 0 <= i < arr.length / 2, and false otherwise. Values may be negative or zero. Aim for O(n log n) time.

Examples

Input: arr = [-6,-3,8,4]
Output: true
Pair -3 with -6 and 4 with 8.
Input: arr = [1,2,2,5]
Output: false
1 pairs with one 2, but the other 2 and 5 cannot pair.
Input: arr = [2,4,0,0]
Output: true
0 is twice 0, and 4 is twice 2.

Constraints

  • 2 <= arr.length <= 3 * 10^4
  • arr.length is even
  • -10^5 <= arr[i] <= 10^5

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems