Skip to content

Swap Two Numbers

In this coding question, we input two numbers and swap them without using the swapping logic.

Gopi Gorantala
Gopi Gorantala
1 min read

Table of Contents

In this lesson, we use the XOR operator to swap the values of two inputs.

Introduction

In this question, input two numbers and swap them without using the swapping logic.

Problem statement

We need to write a program to swap two numbers.

Input: a = 10, b = 121 

Output: a = 121, b = 10

Solution

We can make use of XOR to swap two values. You will find the solution below:

Java

class SwapTwoNumbers {
    public static void main(String[] args) {

        int a = 10, b = 121;
        a = a ^ b;
        b = b ^ a;
        a = a ^ b;

        System.out.println("Finally, after swapping; a = " + a + " , b = " + b);
    }
}

Python

def swap_nums(a,b):
    a=a^b
    b=b^a
    a=a^b
    print("Finally, after swapping a =", a, ",b =", b)

a=10
b=121
swap_nums(a,b)

JavaScript

const SwapTwoNumbers = (a, b) => {
    a = a ^ b;
    b = b ^ a;
    a = a ^ b;

    console.log (`Finally, after swapping; a = ${a} , b = ${b}`)
}

const a = 10;
const b = 121;

SwapTwoNumbers (a, b);

C++

#include <iostream>

using namespace std;

int main() {
    int a = 10, b = 121;
    a = a ^ b;
    b = b ^ a;
    a = a ^ b;
    cout << "Finally, after swapping; a= " << a << " , b = " << b;
    return 0;
}

TypeScript

export const SwapTwoNumbers = (a: number, b: number): void => {
    a = a ^ b;
    b = b ^ a;
    a = a ^ b;

    console.log(`Finally, after swapping; a = ${a} , b = ${b}`)
}

const a: number = 10;
const b: number = 121;

SwapTwoNumbers(a, b);

Complexity analysis

Time Complexity: We are not running a loop or scaling the inputs. The inputs never change. So the operation takes a single unit of time, which is O(1).

Space Complexity: We didn’t create an extra memory for this. So, the space complexity is O(1).

Coding Interview QuestionsData Structures and AlgorithmsBit Manipulation

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Leetcode 217: Contains Duplicate

This question marks the first problem when working on duplicate data, either integers or strings etc. Companies that have asked this in their coding interview are Amazon, Apple, Netflix, Google, Microsoft, Adobe, Facebook, and many more top tech companies. Problem statement Given an integer array nums, return true if any

Leetcode 217: Contains Duplicate
Members Public

Leetcode 121: Best Time To Buy and Sell Stock

The Best time to buy and sell stock problem is a classic problem that can be solved using the Greedy approach. This is one of the most popular questions asked in such interviews. Companies that have asked this in their coding interview are Facebook, Amazon, Apple, Netflix, Google, Microsoft, Adobe,

Leetcode 121: Best Time To Buy and Sell Stock
Members Public

Bit Manipulation Course Overview

Overview In this course, you will learn how to solve problems using bit manipulation, a powerful technique that can be used to optimize your algorithmic and problem-solving skills. This is one of the most important/critical topics when someone starts preparing for coding interviews for FAANG companies. To kick things