Swap two numbers | Practice | GeeksforGeeks

Swap given two numbers and print them. (Try to do it without a temporary variable.) and return it.
Example 1:
Input: a = 13, b = 9
Output: 9 13
Explanation: after swapping it
becomes 9 and 13.

â€‹Example 2:
Input: a = 15, b = 8
Output: 8 15
Explanation: after swapping it
becomes 8 and 15.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function get() which takes a, b as inputs and returns the list of integers contains the new value of a and b after swap.

Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ a, b ≤ 106
0
mayank180919995 days ago
  pair<int, int> get(int a, int b){
        //complete the function here
        int temp=a;
        a=b;
        b=temp;
        return{a,b};
    }
0
rsbly7300956 days ago
class Solution {
   get(a,b){
       a = a+b;
       b = a - b;
       a = a - b
       return [a,b];
   }
}
0
shazidur1128181 week ago
#java
static List<Integer> get(int a,int b)   {       ArrayList<Integer> arr = new ArrayList<>(); arr.add(a); arr.add(b); Collections.swap(arr,0,1); return arr;   }
0
sangrambachu1 week ago
Using Bitwise operator
 
class Solution{
    static List<Integer> get(int a,int b)
    {
        // code here
        List<Integer> li = new ArrayList<Integer>();
        a = a^b;
        b = a^b;
        a = a^b;
        li.add(a);
        li.add(b);
        
        return li;
    }
}
0
nikeshbhalerao19983 weeks ago
import java.io.*;
import java.util.*;
public class {

public static void main(String[] args){

Scanner sc = new Scanner(System.in);
 int a = sc.nextInt();
 int b = sc.nextInt();
 
 a = a+b;  
 b= a-b;   
 a= a-b; 
 System.out.println(a);
 System.out.println(b);  
 
  }
  } 
0
sayanmazumder9991 month ago
JAVA
        ArrayList A = new ArrayList();        A.add(b); A.add(a);        return A;
+1
0niharika22 months ago
        //xor operator       //A * A = 0.       //A * 0 = A.       int temp = 0;       temp = a ^ b;       b = b ^ temp; //b = b ^ b ^ a = 0 ^ a = a.       a = a ^ temp;        pair<int, int> p(a,b);       return p;
0
siddharthweb32 months ago
The classic old method......
 
class Solution{   public:   pair<int, int> get(int a, int b){       //complete the function here       int temp;       temp = a;       a =b;       b = temp;       return {a,b};          }};
0
abhishektripathi85822 months ago
Noobs class Solution{   public:   pair<int, int> get(int a, int b){       //complete the function here              return{b,a};    }};
0
thejaskbenny
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.