1273. Swap Two Numbers Without a Temp Variable

EasyBit ManipulationBit ManipulationXOR SwapArithmetic

Given two integers a and b, swap their values without using a temporary variable, and return the swapped pair as an array [new_a, new_b] which equals [b, a].

Input: A JSON object {"a": <integer>, "b": <integer>}.

Output: Return an array of two integers [b, a] in that order.

Examples

Example 1
Input: {"a":3,"b":9}
Output: [9,3]
Explanation: After the XOR swap a becomes 9 and b becomes 3 -> [9,3].
Example 2
Input: {"a":-2,"b":5}
Output: [5,-2]
Explanation: Swapping gives [5,-2].

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →