DSA: Recursion
For Interview
DSA (Data Structures and Algorithms) Math is a branch of mathematics that focuses on mathematical concepts and techniques used in the analysis and design of algorithms and data structures. DSA Math plays a crucial role in understanding and solving complex algorithmic problems efficiently. It helps in analyzing the time and space complexity of algorithms, designing efficient data structures, optimizing algorithms, and evaluating their performance. Proficiency in DSA Math is essential for developing strong problem-solving skills in the field of computer science and programming.
- Day 1
Concept:
Resources:
Assignments:
You are given a number n. You need to find the digital root of n.
DigitalRoot of a number is the recursive sum of its digits until we get a single digit number.
Constraints:
1 <= n <= 10^7
Example 1:
Input:
n = 1
Output: 1
Explanation: Digital root of 1 is 1
Example 2:
Input:
n = 99999
Output: 9
Explanation: Sum of digits of 99999 is 45, which is not a single digit number, hence
sum of digit of 45 is 9 which is a single digit number.
- Day 2
Concept:
Resources:
Assignments:
Implement pow(x, n), which calculates x raised to the power n (i.e., x^n).
Example 1:
Input: x = 2.00000, n = 10
Output: 1024.00000
Example 2:
Input: x = 2.10000, n = 3
Output: 9.26100
Example 3:
Input: x = 2.00000, n = -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25
- Day 3
Concept:
Resources:
Assignments:
Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.
Constraints:
The number of nodes in the list is in the range [0, 10^4].
1 <= Node.val <= 50
0 <= val <= 50
Example 1:
Input: head = [1,2,6,3,4,5,6], val = 6
Output: [1,2,3,4,5]
Example 2:
Input: head = [], val = 1
Output: []
Example 3:
Input: head = [7,7,7,7], val = 7
Output: []
- Day 4
Concept:
Resources:
Assignments:
You are given a positive integer p. Consider an array nums (1-indexed) that consists of the integers in the inclusive range [1, 2p - 1] in their binary representations. You are allowed to do the following operation any number of times:
Choose two elements x and y from nums.
Choose a bit in x and swap it with its corresponding bit in y. Corresponding bit refers to the bit that is in the same position in the other integer.
For example, if x = 1101 and y = 0011, after swapping the 2nd bit from the right, we have x = 1111 and y = 0001.
Find the minimum non-zero product of nums after performing the above operation any number of times. Return this product modulo 10^9 + 7.
Note: The answer should be the minimum product before the modulo operation is done.
Constraints:
1 <= p <= 60
Examples
Example 1:
Input: p = 1
Output: 1
Explanation: nums = [1].
There is only one element, so the product equals that element.
Example 2:
Input: p = 2
Output: 6
Explanation: nums = [01, 10, 11].
Any swap would either make the product 0 or stay the same.
Thus, the array product of 1 * 2 * 3 = 6 is already minimized.
Example 3:
Input: p = 3
Output: 1512
Explanation: nums = [001, 010, 011, 100, 101, 110, 111]
- In the first operation we can swap the leftmost bit of the second and fifth elements.
- The resulting array is [001, 110, 011, 100, 001, 110, 111].
- In the second operation we can swap the middle bit of the third and fourth elements.
- The resulting array is [001, 110, 001, 110, 001, 110, 111].
The array product is 1 * 6 * 1 * 6 * 1 * 6 * 7 = 1512, which is the minimum possible product.
- Day 5
Concept:
Resources:
Assignments:
Write the code for bubble sort using recursion.
- Day 6
Concept:
Resources:
Assignments:
You are given a set of three rods, denoted as `source`, `destination`, and `helper`, and a collection of disks with distinct sizes, initially stacked in ascending order on the `source` rod. Your task is to implement a function `tower_of_hanoi(disk_count, source, destination, helper)` that simulates the movement of disks from the source rod to the destination rod following the rules of the Tower of Hanoi.
Write a recursive algorithm to solve the Tower of Hanoi problem, adhering to the following rules:
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the rods and placing it on top of another rod or on an empty rod.
3. No disk may be placed on top of a smaller disk.
Your function should take the following parameters:
- `disk_count`: The number of disks to be moved.
- `source`: The rod from which the disks should be
moved initially.
- `destination`: The rod to which the disks should be moved.
- `helper`: The remaining rod, which acts as an auxiliary to facilitate the movement.
Implement the function and demonstrate its functionality by providing examples of moving disks for different initial configurations.
Note: Your implementation should showcase the step-by-step movement of disks from the source rod to the destination rod, satisfying the Tower of Hanoi rules.
- Day 7
Concept:
Resources:
Assignments:
You have a list arr of all integers in the range [1, n] sorted in a strictly increasing order. Apply the following algorithm on arr:
1. Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.
2. Repeat the previous step again, but this time from right to left, remove the rightmost number and every other number from the remaining numbers.
3. Keep repeating the steps again, alternating left to right and right to left, until a single number remains.
Given the integer n, return the last number that remains in arr.
Example:
Input: n = 9
Output: 6
Explanation:
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr = [2, 4, 6, 8]
arr = [2, 6]
arr = [6]
- Day 8
Concept:
Resources:
Assignments:
There is a regular convex polygon with n vertices. The vertices are labeled from 0 to n - 1 in a clockwise direction, and each vertex has exactly one monkey. The figure shows a convex polygon of 6 vertices.
Each monkey moves simultaneously to a neighboring vertex. A neighboring vertex for a vertex i can be:
1. The vertex (i + 1) % n in the clockwise direction, or
2. The vertex (i - 1 + n) % n in the counter-clockwise direction.
A collision happens if at least two monkeys reside on the same vertex after the movement or intersect on an edge.
Return the number of ways the monkeys can move so that at least one collision happens. Since the answer may be very large, return it modulo 10^9 + 7.
Note that each monkey can only move once.
Example 1:
Input: n = 3
Output: 6
Explanation: There are 8 total possible movements.
Two ways such that they collide at some point are:
- Monkey 1 moves in a clockwise direction; monkey 2 moves in an anticlockwise direction; monkey 3 moves in a clockwise direction. Monkeys 1 and 2 collide.
- Monkey 1 moves in an anticlockwise direction; monkey 2 moves in an anticlockwise direction; monkey 3 moves in a clockwise direction. Monkeys 1 and 3 collide.
It can be shown 6 total movements result in a collision.
- Day 9
Concept:
Resources:
Assignments:
We build a table of n rows (1-indexed). We start by writing 0 in the 1st row. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.
For example, for n = 3, the 1st row is 0, the 2nd row is 01, and the 3rd row is 0110.
Given two integer n and k, return the kth (1-indexed) symbol in the nth row of a table of n rows.
Constraints:
1 <= n <= 30
1 <= k <= 2^(n - 1)
Example 1:
Input: n = 1, k = 1
Output: 0
Explanation: row: 1 0
Example 2:
Input: n = 2, k = 1
Output: 0
Explanation:
row 1: 0
row 2: 01
Example 3:
Input: n = 2, k = 2
Output: 1
Explanation:
row 1: 0
row 2: 01
- Day 10
Concept:
Resources:
Assignments:
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
Given n, calculate F(n).
Constraints:
0 <= n <= 30
Example 1:
Input: n = 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
Example 2:
Input: n = 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
Example 3:
Input: n = 4
Output: 3
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.