Swap Two Numbers Without Using a Third Variable
Exchange the values of two integers without introducing a temporary variable.
You can swap two numbers with only arithmetic: a = a + b, then b = a - b (which is the old a), then a = a - b (which is the old b). No third variable is needed. Starting from a = 5, b = 10, the steps produce a = 10 and b = 5.
Problem Statement
Given two integers a and b, exchange their values so that a ends up
holding the original b and b ends up holding the original a. You must not
use a third (temporary) variable.
For example, if a = 5 and b = 10, after the swap a should be 10 and b
should be 5.
Input: Two integers a and b.
Output: The two integers with their values exchanged.
Examples
Input: a = 5, b = 10
Output: a = 10, b = 5The two values are exchanged with add-and-subtract arithmetic.
Input: a = 3, b = 7
Output: a = 7, b = 3a becomes the old b (7) and b becomes the old a (3).
Constraints
a and b fit in a 32-bit int (mind that a + b could overflow for extreme values)No third/temporary variable may be used
Think Before You Code
Reveal the questions to ask yourself first
- How can
a + btemporarily store information about both numbers? - Once the sum is in
a, how do you recover each original value? - What risk does the arithmetic method carry for very large inputs?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Use the sum to carry both values, then peel them apart.
a = a + b— nowaholds the combined total.b = a - b— subtracting the currentbleaves the originalainb.a = a - b— subtracting the newb(originala) leaves the originalbina.
The values are now swapped. The XOR variant (a ^= b; b ^= a; a ^= b;) does the
same thing without any overflow concern.
Dry Run
Walk through the example step by step
Swapping a = 5, b = 10:
step | a | b
------------+--------------+------------
start | 5 | 10
a = a + b | 5 + 10 = 15 | 10
b = a - b | 15 | 15 - 10 = 5
a = a - b | 15 - 5 = 10 | 5
result | 10 | 5
Solution
Reveal the full Java solution
public class SwapNumbers {
public static int[] swap(int a, int b) {
a = a + b;
b = a - b; // original a
a = a - b; // original b
return new int[] { a, b };
}
public static void main(String[] args) {
int[] first = swap(5, 10);
System.out.println("a = " + first[0] + ", b = " + first[1]); // a = 10, b = 5
int[] second = swap(3, 7);
System.out.println("a = " + second[0] + ", b = " + second[1]); // a = 7, b = 3
}
}
The sum a + b momentarily encodes both numbers in a single slot; subtracting
one original value recovers the other. Since Java passes primitives by value, the
method returns the swapped pair in an array so the caller can observe the result.
For production code, XOR swapping avoids the theoretical overflow of a + b.
O(1)Space: O(1)Common Mistakes
- Expecting the swap to affect the caller's own variables — Java passes primitives by value.
- Ignoring that `a + b` can overflow int for values near Integer.MAX_VALUE.
Edge Cases to Test
- Swapping equal values, e.g. a = b = 4, correctly leaves both unchanged.
- One value being 0, e.g. a = 0, b = 9, swaps to a = 9, b = 0.
Interview Follow-Ups
- How would the XOR-based swap look, and why is it overflow-safe?
- Why can XOR swapping fail if both references point to the same memory location?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →