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

Easy Recursion Questions

Recursion involves breaking a problem down into smaller subproblems of the same type until reaching a base case that can be solved directly. There are two key steps: (1) the base case, which solves the simplest form of the problem, and (2) the recursive step, which reduces the problem into simpler subproblems and calls itself recursively until the base case is reached. Easy recursive problems include calculating factorials, powers, HCF, searching lists, string comparisons, and determining palindromes. Medium problems include permutations, the Tower of Hanoi puzzle, power sets, and subset sums. Hard recursive problems find the longest increasing subsequence, longest common subsequence, coin change combinations, and longest palindromic subsequence.

Uploaded by

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

Easy Recursion Questions

Recursion involves breaking a problem down into smaller subproblems of the same type until reaching a base case that can be solved directly. There are two key steps: (1) the base case, which solves the simplest form of the problem, and (2) the recursive step, which reduces the problem into simpler subproblems and calls itself recursively until the base case is reached. Easy recursive problems include calculating factorials, powers, HCF, searching lists, string comparisons, and determining palindromes. Medium problems include permutations, the Tower of Hanoi puzzle, power sets, and subset sums. Hard recursive problems find the longest increasing subsequence, longest common subsequence, coin change combinations, and longest palindromic subsequence.

Uploaded by

Vikram Saurabh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

RECURSION

Principle behind designing recursive algorithm:


Step 1. If the problem is in simplest state, solve it straight way [Basis Step]
Step 2. Otherwise break the problem into similar but simpler problem with the target that at some point of time it will be
simple enough to be solved straight way. [Recursion Step]
Example:
1. Factorial of an integer n
If n = 1 then the result is 1 [Step 1]
Else the result is (n X Factorial of (n-1)) [Step 2].

EASY RECURSION QUESTIONS:


1.
2.
3.
4.
5.

To find out the power of x. (Value of x to the power n(x^n))


To find the HCF of two integers n1 and n2.
Find largest amongst n integers
To search a number n in a given list L (Return true if number is there else false).
String comparison: Compare strings s1 and s2 - print 0 if both are same, print -1 if alphabetically s1 comes before
s2 and print 1 if alphabetically s1 comes after s2.
6. Find out whether the given string S of length n is a palindrome or not (returns true if S is a palindrome, returns
false otherwise). [A palindrome is a string if it reads same from both the ends. Eg. ABCDCBA]
7. Sorting an array A of n integers in ascending order.
8. Find the nth term of Fibonacci Series.

MEDIUM RECURSION QUESTIONS:


1. Find all permutations of n objects. (Eg. For ABC permutations are ABC, ACB, BAC, BCA, CAB, CBA).
2. Tower of Hanoi Problem:
There are three towers
The first (source) tower holds some number of rings
You want to move all rings to the last (destination) tower
You can only move one ring at a time
You can never put a larger ring on top of a smaller ring
You have one auxiliary tower you can use
Show every movement of rings while transferring all the rings from
source to destination.
3. Find the power set of any set S (Power set is all the subset of a set S).
Example:
For set []={1,2,3}
Power set={},{1},{2},{3},{1,2},{2,3},{1,3},{1,2,3}.

4. Subset sum problem:


Given a set of non-negative integers, and a value sum, determine if there is a subset of the given set with sum equal to
given sum.
Example:

set [] = {3, 34, 4, 12, 5, 2}, sum = 9


Output: True //There is a subset (4, 5) with sum 9.

HARD RECURSION QUESTIONS:


1. The longest Increasing Subsequence (LIS) problem:
To find the length of the longest subsequence of a given sequence such that all elements of the subsequence are
sorted in increasing order.
Example:
Length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 and LIS is {10, 22, 33, 50, 60, 80}.

2. Longest Common Subsequence (LCS) problem:


Given two sequences, find the length of longest subsequence present in both of them. A subsequence is a sequence
that appears in the same relative order, but not necessarily contiguous.
Examples:
LCS for input Sequences ABCDGH and AEDFHR is ADH of length 3.
LCS for input Sequences AGGTAB and GXTXAYB is GTAB of length 4.

3. Coin Change Problem:


Given a value N, if we want to make change for N cents, and we have infinite supply of each of S = {S1, S2,..., Sm}
valued coins, how many ways can we make the change? The order of coins doesnt matter.
Examples:
For N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}. So output should be 4.
For N = 10 and S = {2, 5, 3, 6}, there are five solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. So the output
should be 5.

4. Given a sequence, find the length of the longest palindromic subsequence in it.
Example:
If the given sequence is BBABCBCAB, then the output should be 7 as BABCBAB is the longest palindromic
subsequence in it. BBBBB and BBCBB are also palindromic subsequences of the given sequence, but not the
longest ones.

CodeIIEST
Programming Club of IIEST Shibpur

You might also like