2. Swap Two Numbers Without Using a Third
Variable
Let the numbers be a and b.
Use arithmetic operations to swap
them:
a = a + b
b = a - b
a = a - b
ALGORITH
M
3. Reverse a Given Number/String Using
Command Line Arguments
• Read the string from command line
arguments.
• Reverse the string using slicing or a loop.
• Print the reversed string.
ALGORITH
M
4. Arrange 1s and 0s Together in a Single Array
Scan
• Initialize two pointers, left and right, at the beginning and
end of the array.
• Scan the array with two pointers.
• Swap elements when a 0 is encountered at right and a 1 is
encountered at left.
• Move pointers inward.
ALGORITH
M
5. Function to Swap Two Numbers Without
Using a Temporary Variable
Use bitwise XOR to swap the values.
ALGORITH
M
def swap(a, b):
a = a ^ b
b = a ^ b
a = a ^ b
return a, b
6. Difference Between the Sum of Odd and Even
Position Digits
ALGORITH
M
• Initialize sum_odd and sum_even to 0.
• Iterate through each digit and add to the
corresponding sum based on the position.
7. Convert a Matrix into Lower Triangular Matrix
ALGORITH
M
Iterate over the matrix and set elements above the
main diagonal to 0.
8. Factorial of a Number Without Using
Arithmetic Operations
ALGORITH
M
Use recursive bit manipulation to compute the
factorial.
9. Number of Pairs Whose Average is Present in
the Array
ALGORITH
M
• Use a set to store unique elements.
• Iterate through each pair and check if their
average exists in the set.
10. Binary Search Algorithm
ALGORITH
M
• Initialize low to 0 and high to len(arr) - 1.
• While low is less than or equal to high:
• Calculate the mid index.
• If the target is equal to arr[mid], return the mid index.
• If the target is less than arr[mid], set high to mid - 1.
• If the target is greater than arr[mid], set low to mid +
1.
• If the target is not found, return -1.
11. Time & Space Complexity (Popular Sorting
and Searching Algorithms)
Sorting Algorithms:
• Bubble Sort: Time: O(n^2), Space: O(1)
• Selection Sort: Time: O(n^2), Space: O(1)
• Insertion Sort: Time: O(n^2), Space: O(1)
• Merge Sort: Time: O(n log n), Space: O(n)
• Quick Sort: Time: O(n log n) average, O(n^2) worst, Space: O(log
n)
• Heap Sort: Time: O(n log n), Space: O(1)
• Radix Sort: Time: O(nk), Space: O(n + k) (where k is the range of
12. Time & Space Complexity (Popular Sorting
and Searching Algorithms)
Searching Algorithms:
• Linear Search: Time: O(n), Space: O(1)
• Binary Search: Time: O(log n), Space: O(1)
13. Unique Paths in a Grid
ALGORITH
M
• Use dynamic programming.
• Create a 2D array dp where dp[i][j] represents the
number of unique paths to cell (i, j).
• Initialize the first row and column to 1.
• Fill the rest of the dp table using the relation dp[i][j] =
dp[i-1][j] + dp[i][j-1].
14. Minimum and Maximum Element in Every
Contiguous Subarray of Size k
ALGORITH
M
• Use two deques to maintain the indices of minimum
and maximum elements.
• Slide the window of size k over the array and update
the deques.
• At each step, record the current minimum and
maximum.
15. Rotate the Array by K Elements
ALGORITH
M
• Reverse the entire array.
• Reverse the first k elements.
• Reverse the remaining elements.