Swap two numbers In Java without using third variable.
Swap two numbers In Java without using third variable. Write a program to swap/exchange 2 numbers without using temporary or third variable.
There are many approaches to solve this problem, we will see all of them one by one.
Algorithm
Approach 1: Using Addition and Subtraction Operator
In this approach, we will use addition '+' and subtraction '-' operator to swap 2 numbers without using any temporary variable.
Disadvantage of this approach: It may lead to Arithmetic Overflow Issue because in case if x and y are too large then their sum may go out of integer range.
package javabypatel; public class Swap2NumberWithoutTempVariable { public static void main(String[] args) { int a = 10; int b = -1; usingAdditionSubtractionWay(a, b); } private static void usingAdditionSubtractionWay(int a, int b){ System.out.println("Before swap a="+a + " , " + "b="+b); a=a-b; b=a+b; a=b-a; System.out.println("After Swap a="+a + " , " + "b="+b); } }
Approach 2: Using Multiplication and Divison Operator.
In this approach, we will use multiplication '*' and division '/' operator to swap 2 numbers without using any temporary variable.
Disadvantage of this approach:
It may lead to Arithmetic Overflow Issue because in case if x and y are too large then their sum may go out of integer range.
The multiplication and division based approach will not work if one of the numbers is 0 as the product becomes 0 irrespective of the other number.
package javabypatel; public class Swap2NumberWithoutTempVariable { public static void main(String[] args) { int a = 10; int b = -1; usingMultiplicationAndDivideToSwapNumbers(a, b); } private static void usingMultiplicationAndDivideToSwapNumbers(int a, int b){ System.out.println("Before swap a="+a + " , " + "b="+b); a=a*b; b=a/b; a=a/b; System.out.println("After Swap a="+a + " , " + "b="+b); } }Approach 3: Using Bit Manipulation.
In this approach, we will use XOR '^' operator, which uses Bit Manipulation to swap numbers.
Let's see how this approach works, if we change the bits of "a" to match exactly as "b" then "a" will become "b".
XOR will help us get bit difference between both numbers "a" and "b".
(XOR will give 0 for same bits and 1 for non matching.)
package javabypatel; public class Swap2NumberWithoutTempVariable { public static void main(String[] args) { int a = 10; int b = -1; usingBitManipulationToSwapNumbers(a, b); } private static void usingBitManipulationToSwapNumbers(int a, int b){ System.out.println("Before swap a="+a + " , " + "b="+b); a=a^b; b=a^b; a=b^a; System.out.println("After Swap a="+a + " , " + "b="+b); } }
You may also like to see
Knapsack Problem using Dynamic Programming in Java.
Why Selection sort is faster than Bubble sort.
Tower Of Hanoi Program in Java.
Java Multithreading and Concurrency Interview Questions and Answers with Example.
Find middle element of a linked list.
Transpose of Matrix Inplace.
Print Fibonacci series using Recursive approach in Java.
Enjoy !!!!
If you find any issue in post or face any error while implementing, Please comment.
Post a Comment