0% found this document useful (0 votes)
10 views5 pages

Programs

The document outlines a series of programming tasks that involve recursive functions and combinatorial algorithms. Tasks include checking string patterns, counting ways to climb stairs, performing binary search, generating subsets, and creating permutations and combinations based on numeric strings. Each task provides input and output formats, along with sample inputs and expected outputs.

Uploaded by

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

Programs

The document outlines a series of programming tasks that involve recursive functions and combinatorial algorithms. Tasks include checking string patterns, counting ways to climb stairs, performing binary search, generating subsets, and creating permutations and combinations based on numeric strings. Each task provides input and output formats, along with sample inputs and expected outputs.

Uploaded by

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

1)Check AB

Send Feedback
Suppose you have a string, S, made up of only 'a's and 'b's. Write a recursive
function that checks if the string was generated using the following rules:
a. The string begins with an 'a'
b. Each 'a' is followed by nothing or an 'a' or "bb"
c. Each "bb" is followed by nothing or an 'a'
If all the rules are followed by the given string, return true otherwise return
false.
Input format :
String S
Output format :
'true' or 'false'
2)
Staircase
Send Feedback
A child is running up a staircase with N steps, and can hop either 1 step, 2 steps
or 3 steps at a time. Implement a method to count how many possible ways the child
can run up to the stairs. You need to return number of possible ways W.
Input format :
Integer N
Output Format :
Integer W
Constraints :
1 <= N <= 30
Sample Input 1 :
4
Sample Output 1 :
7
Sample Input 2 :
5
Sample Output 2 :
13
3.)
Binary Search (Recursive)
Send Feedback
Given an integer sorted array (sorted in increasing order) and an element x, find
the x in given array using binary search. Return the index of x.
Return -1 if x is not present in the given array.
Note : If given array size is even, take first mid.
Input format :

Line 1 : Array size

Line 2 : Array elements (separated by space)

Line 3 : x (element to be searched)

Sample Input :
6
2 3 4 5 6 8
5
Sample Output:
3
4.)
Return subset of an array
Send Feedback
Given an integer array (of length n), find and return all the subsets of input
array.
Subsets are of length varying from 0 to n, that contain elements of the array. But
the order of elements should remain same as in the input array.
Note : The order of subsets are not important.
Input format :

Line 1 : Size of array

Line 2 : Array elements (separated by space)

Sample Input:
3
15 20 12
Sample Output:
[] (this just represents an empty array, don't worry about the square brackets)
12
20
20 12
15
15 12
15 20
15 20 12
5.)
Return subsets sum to K
Send Feedback
Given an array A of size n and an integer K, return all subsets of A which sum to
K.
Subsets are of length varying from 0 to n, that contain elements of the array. But
the order of elements should remain same as in the input array.
Note : The order of subsets are not important.
Input format :
Line 1 : Integer n, Size of input array
Line 2 : Array elements separated by space
Line 3 : K
Constraints :
1 <= n <= 20
Sample Input :
9
5 12 3 17 1 18 15 3 17
6
Sample Output :
3 3
5 1
6.)
Print Subset Sum to K
Send Feedback
Given an array A and an integer K, print all subsets of A which sum to K.
Subsets are of length varying from 0 to n, that contain elements of the array. But
the order of elements should remain same as in the input array.
Note : The order of subsets are not important. Just print them in different lines.
Input format :
Line 1 : Size of input array
Line 2 : Array elements separated by space
Line 3 : K
Sample Input:
9
5 12 3 17 1 18 15 3 17
6
Sample Output:
3 3
7.)
Return all codes - String
Send Feedback
Assume that the value of a = 1, b = 2, c = 3, ... , z = 26. You are given a numeric
string S. Write a program to return the list of all possible codes that can be
generated from the given string.
Note : The order of codes are not important. And input string does not contain 0s.
Input format :
A numeric string
Constraints :
1 <= Length of String S <= 10
Sample Input:
1123
Sample Output:
aabc
kbc
alc
aaw
kw
8.)
Print all Codes - String
Send Feedback
Assume that the value of a = 1, b = 2, c = 3, ... , z = 26. You are given a numeric
string S. Write a program to print the list of all possible codes that can be
generated from the given string.
Note : The order of codes are not important. Just print them in different lines.
Input format :
A numeric string S
Output Format :
All possible codes in different lines
Constraints :
1 <= Length of String S <= 10
Sample Input:
1123
Sample Output:
aabc
kbc
alc
aaw
kw
9.)
Return Permutations - String
Send Feedback
Given a string S, find and return all the possible permutations of the input
string.
Note 1 : The order of permutations is not important.
Note 2 : If original string contains duplicate characters, permutations will also
be duplicates.
Input Format :
String S
Output Format :
All permutations (in different lines)
Sample Input :
abc
Sample Output :
abc
acb
bac
bca
cab
cba
10.)
Print Permutations - String
Send Feedback
Given a string, find and print all the possible permutations of the input string.
Note : The order of permutations are not important. Just print them in different
lines.
Sample Input :
abc
Sample Output :
abc
acb
bac
bca
cab
cba
*******************
Return Keypad Code
Send Feedback
Given an integer n, using phone keypad find out all the possible strings that can
be made using digits of input n.
Return empty string for numbers 0 and 1.
Note : 1. The order of strings are not important.
2. Input and output has already been managed for you. You just have to populate the
output array and return the count of elements populated in the output array.
Input Format :
Integer n
Output Format :
All possible strings in different lines
Constraints :
1 <= n <= 10^6
Sample Input:
23
Sample Output:
ad
ae
af
bd
be
bf
cd
ce
cf
**************************
Print Keypad Combinations Code
Send Feedback
Given an integer n, using phone keypad find out and print all the possible strings
that can be made using digits of input n.
Note : The order of strings are not important. Just print different strings in new
lines.
Input Format :
Integer n
Output Format :
All possible strings in different lines
Constraints :
1 <= n <= 10^6
Sample Input:
23
Sample Output:
ad
ae
af
bd
be
bf
cd
ce
cf

You might also like