Recover two numbers from their sum and bitwise XOR

Quick Overview

Given the sum and the bitwise XOR of two unknown non-negative integers, recover the pair with the smallest first value or report that no pair exists. It tests bit manipulation, how carries connect addition to XOR, and spotting inputs that no pair can produce.

Recover two numbers from their sum and bitwise XOR

Company: Deloitte

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given two non-negative integers `s` and `x`. They are the sum and the bitwise XOR of two unknown non-negative integers `A` and `B`, so `A + B == s` and `A XOR B == x`. Return a pair `[A, B]` that satisfies both equations, or `[-1, -1]` if no such pair exists. ### Function Signature ```python def sum_and_xor(s: int, x: int) -> list[int]: ... ``` ### Rules - `A` and `B` must both be non-negative integers. - Several pairs can satisfy the equations. Return the one with the smallest `A`. That pair always has `A <= B`, so the expected output is unique. - If no pair exists, return `[-1, -1]`. ### Constraints - `0 <= s <= 2**31 - 1` - `0 <= x <= 2**31 - 1` ### Examples ```text Input: s = 9, x = 5 Output: [2, 7] ``` `2 + 7 = 9` and `0b010 XOR 0b111 = 0b101 = 5`. The other valid pairs are `[3, 6]`, `[6, 3]` and `[7, 2]`, and none of them starts with a value below 2. ```text Input: s = 5, x = 7 Output: [-1, -1] ``` The sum of two non-negative integers is never smaller than their XOR, and here `s < x`. ```text Input: s = 6, x = 2 Output: [-1, -1] ``` The ways to split 6 are `0 + 6`, `1 + 5`, `2 + 4` and `3 + 3` (plus their reverses), and their XORs are 6, 4, 6 and 0. None of them is 2.

Overview: Given the sum and the bitwise XOR of two unknown non-negative integers, recover the pair with the smallest first value or report that no pair exists. It tests bit manipulation, how carries connect addition to XOR, and spotting inputs that no pair can produce.

Deloitte logo
Deloitte
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given two non-negative integers s and x. They are the sum and the bitwise XOR of two unknown non-negative integers A and B, so A + B == s and A XOR B == x. Return a pair [A, B] that satisfies both equations, or [-1, -1] if no such pair exists.

Function Signature

def sum_and_xor(s: int, x: int) -> list[int]:
    ...

Rules

  • A and B must both be non-negative integers.
  • Several pairs can satisfy the equations. Return the one with the smallest A . That pair always has A <= B , so the expected output is unique.
  • If no pair exists, return [-1, -1] .

Constraints

  • 0 <= s <= 2**31 - 1
  • 0 <= x <= 2**31 - 1

Examples

Input: s = 9, x = 5
Output: [2, 7]

2 + 7 = 9 and 0b010 XOR 0b111 = 0b101 = 5. The other valid pairs are [3, 6], [6, 3] and [7, 2], and none of them starts with a value below 2.

Input: s = 5, x = 7
Output: [-1, -1]

The sum of two non-negative integers is never smaller than their XOR, and here s < x.

Input: s = 6, x = 2
Output: [-1, -1]

The ways to split 6 are 0 + 6, 1 + 5, 2 + 4 and 3 + 3 (plus their reverses), and their XORs are 6, 4, 6 and 0. None of them is 2.