Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Algo

The document presents a series of tricky exercises designed for brain workouts, including problems such as finding a missing number in an array, identifying the majority element, reversing a string using recursion, and generating all subsets of a set. Additional exercises involve solving the Tower of Hanoi problem, finding the longest palindromic substring, calculating the sum of digits recursively, generating permutations of a string, and identifying the first non-repeating character. Lastly, it covers finding all prime numbers less than a given number using the Sieve of Eratosthenes algorithm.

Uploaded by

Nihel Ghennani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Algo

The document presents a series of tricky exercises designed for brain workouts, including problems such as finding a missing number in an array, identifying the majority element, reversing a string using recursion, and generating all subsets of a set. Additional exercises involve solving the Tower of Hanoi problem, finding the longest palindromic substring, calculating the sum of digits recursively, generating permutations of a string, and identifying the first non-repeating character. Lastly, it covers finding all prime numbers less than a given number using the Sieve of Eratosthenes algorithm.

Uploaded by

Nihel Ghennani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tricky Exercises for Brain Workout

1. Find the Missing Number


Given an array of size n − 1 that contains all numbers from 1 to n except one
number, find the missing number.
Example:
arr = {3, 7, 1, 2, 8, 4, 5}, n = 8
Output: 6

2. Find the Majority Element


Given an array of size n, find the element that appears more than n/2 times in
the array. If no such element exists, return −1.
Example:
arr = {3, 3, 4, 2, 4, 4, 2, 4, 4}
Output: 4

3. Reverse a String Using Recursion


Write a recursive function to reverse a string without using any extra data
structures (like arrays or stacks).
Example:
str = ”hello”
Output: ”olleh”

4. Find All Subsets of a Set


Write a program that prints all possible subsets (power set) of a given set. The
input set can be of any size, and your solution should handle it recursively.
Example:
arr = {1, 2, 3}
Output:
{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}

1
5. Tower of Hanoi
Write a recursive function to solve the Tower of Hanoi problem. You have n
disks and three rods. The goal is to move all the disks from the first rod to the
third rod, following these rules:
• Only one disk can be moved at a time.
• A disk can only be placed on top of a larger disk or an empty rod.
Example:
n=3 (Number of disks)
Output:
Move disk 1 from rod A to rod C
Move disk 2 from rod A to rod B
Move disk 1 from rod C to rod B
Move disk 3 from rod A to rod C
Move disk 1 from rod B to rod A
Move disk 2 from rod B to rod C
Move disk 1 from rod A to rod C

6. Find the Longest Palindromic Substring


Given a string, write a program to find the longest palindromic substring.
Example:
str = ”babad”
Output: ”bab” or ”aba”

7. Sum of Digits (Recursion)


Write a recursive function that takes an integer n and returns the sum of its
digits.
Example:
n = 12345
Output: 15 (1 + 2 + 3 + 4 + 5)

8. Permutations of a String
Write a program that generates all the permutations of a string without using
extra data structures (like a list). You should do it recursively.
Example:
str = ”ABC”
Output:
”ABC”, ”ACB”, ”BAC”, ”BCA”, ”CAB”, ”CBA”

2
9. Find the First Non-Repeating Character
Given a string, find the first non-repeating character and return its index. If no
such character exists, return −1.
Example:
str = ”geeksforgeeks”
Output: 2 (The first non-repeating character is ’f’)

10. Find All Prime Numbers Less than N (Sieve


of Eratosthenes)
Write a program to find all prime numbers less than n using the Sieve of Er-
atosthenes algorithm. This is an efficient way to find primes.
Example:
n = 30
Output: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29

You might also like