0% found this document useful (0 votes)
116 views

Swap Two Numbers 2nd Method

This document provides an example solution to swapping two integer variables without using a third variable. It includes two approaches, with the contributed approach showing code to swap using a temporary variable. The code sample demonstrates assigning the value of the first variable to the temporary variable, then assigning the first variable to the second variable and finally assigning the temporary variable value to the second variable. It notes the time and space complexity of this approach as O(1).

Uploaded by

Sa Dik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

Swap Two Numbers 2nd Method

This document provides an example solution to swapping two integer variables without using a third variable. It includes two approaches, with the contributed approach showing code to swap using a temporary variable. The code sample demonstrates assigning the value of the first variable to the temporary variable, then assigning the first variable to the second variable and finally assigning the temporary variable value to the second variable. It notes the time and space complexity of this approach as O(1).

Uploaded by

Sa Dik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Practice Guided Paths Interview Prep Challenges Knowledge Centre

Problem Submissions Solution Python (3.5)

Contribute a new Your Code Solution Report


Approach 1
approach
Approach 2 1 '''
2   Time complexity: O(1)
Contributed By 3   Space complexity: O(1).
Shrey Pansuria | Level 1
4 '''
5 ​
Swapping using a temporary 6 def swap(a, b):
variable 7    temp = 0
Take input in two integer datatype variables, a and 8    # Store the value of a in temp.
b. Create a temporary variable temp and initialize it 9    temp = a
equal to a. Now make a equal to b, and finally 10    # Make a equal to b.
make b equal to temp. Print a and b.
11    a = b
12    # Make b equal to temp.
Time Complexity 13    b = temp
O(1),  14    
15    return a, b
The Time Complexity is O(1).

Space Complexity
O(1), 

The Space Complexity is O(1).

Suggest Edit

Console

Rate this Show Saving


problem Previous Next Hint Code... Run Code S

You might also like