Problems - 7 Recursion Codeforces
Problems - 7 Recursion Codeforces
Sheet #7 (Recursion)
A. Print Recursion Make sure don't print any leading or trailing spaces.
Output Input
Print "I love Recursion" N times. First line contains a number T (1 ≤ T ≤ 10) number of test cases.
Input
Only one line containing a number N (1 ≤ N ≤ 103). E. Base Converssion
Output 1 second, 256 megabytes
Print N lines according to the required above.
Given a number N. Print the binary equivalent of N.
input Each time you perform this division, take note of the remainder. Now
reverse the remainders list, and you get the number in binary form
4
Example to convert 29 to binary
output
4 3 2 1
https://codeforces.com/group/MWSDmqGsZm/contest/223339/problems 1/7
9/25/25, 11:18 PM Problems - Codeforces
output
*
***
input
3
output
for more details visit this https://flaviocopes.com/converting-decimal-to-
binary/. *
***
*****
F. Print Even Indices
input
1 second, 256 megabytes
4
Given a number N and an array A of N numbers. Print the numbers in
output
even indices in a reversed order.
*
Note: ***
*****
Assume array A is 0-based indexing. *******
Solve this problem using recursion. Don't print any extra space after '*'.
Input
First line contains a number N (1 ≤ N ≤ 103) number of elements.
H. Inverted Pyramid
1 second, 256 megabytes
Second line contains N numbers ( - 109 ≤ Ai ≤ 109).
Given a number N . Print an inverted pyramid of height N .
Output
Print numbers in even indices in a reversed order separated by spaces. Note: Solve this problem using recursion.
input Input
Only one line containing a number N 3
(1 ≤ N ≤ 10 ) .
4
1 4 2 7
Output
output Print the pyramid in N lines.
2 1 Don't print any extra space after '*'.
input input
7 1
1 5 8 2 3 9 11
output
output
*
11 3 8 1
input
G. Pyramid 2
* output
*******
input *****
***
2 *
https://codeforces.com/group/MWSDmqGsZm/contest/223339/problems 2/7
9/25/25, 11:18 PM Problems - Codeforces
output
I. Count Vowels 5
Input Input
Only one line containing a string S (1 ≤ |S| ≤ 200) where |S| is the length First line contains a number N (1 ≤ N ≤ 103) number of elements.
9
of the string and it consists only of capital ,small letters and spaces. Second line contains N numbers ( - 10 ≤ Ai ≤ 109).
Output Output
Print number of vowels in string S. Print the summation of the N numbers.
input input
Data Structure Lab 4
1 4 2 7
output
6 output
14
J. Factorial input
1 second, 64 megabytes 4
5 5 5 5
Given a number N. Print factorial of N.
output
Note: Solve this problem using recursion.
20
Input
Only one line containing a number N (1 ≤ N ≤ 20).
M. Suffix Sum
Output 1 second, 256 megabytes
Print the factorial of the number N.
Given two numbers N and M , and an array A of N numbers. Calculate
input the sum of the last M numbers.
5 Note: solve this problem using recursion.
output
Input
120 First line contains two numbers N and M (1 ≤ M ≤ N ≤ 10 )
5
.
Input output
First line contains a number N (1 ≤ N ≤ 103) number of elements. 15
9 9
Second line contains N numbers ( - 10 ≤ Ai ≤ 10 ).
Output N. Sum of a Matrix
Print the maximum value in this array. 1 second, 256 megabytes
input Given two matrices A and B of size R * C. Print the summation of A and
5 B.
1 -3 5 4 -6
Note: Solve this problem using recursion.
Input
https://codeforces.com/group/MWSDmqGsZm/contest/223339/problems 3/7
9/25/25, 11:18 PM Problems - Codeforces
First line contains two numbers R and C (1 ≤ R, C ≤ 100). number of output
rows and number of columns respectively.
0
Next R lines will contain C numbers ( - 100 ≤ Ai, j ≤ 100) matrix A
numbers. input
Next R lines will contain C numbers ( - 100 ≤ Bi, j ≤ 100) matrix B 8
numbers. output
Output 3
Print the summation result.
input Q. 3n + 1 sequence
2 3 1 second, 256 megabytes
1 2 3
4 5 6
Given a number n, you should print the length of the 3n + 1 sequence
1 3 5
7 9 11 starting with n.
O. Fibonacci For example, the 3n + 1 sequence of 3 is {3, 10, 5, 16, 8, 4, 2, 1} and its
length is 8.
1 second, 256 megabytes
Note: Solve this problem using recursion.
Given a number N. Print the value of the Nth Fibonacci number.
Input
Only one line containing a number n (1
5
≤ n ≤ 10 ) .
Output
Print the length of 3n + 1 sequence of the given n.
input
1
5
input
output
3
3
output
For more information visit Fibonacci: 8
https://www.mathsisfun.com/numbers/fibonacci-sequence.html.
R. Palindrome Array
P. Log2
1 second, 256 megabytes
1 second, 256 megabytes
Given a number N and an array A of N numbers. Determine if it's
Given a number N . Print ⌊log2 (N )⌋ . palindrome or not.
Note: Solve this problem using recursion. Note:
Input An array is called palindrome if it reads the same backward and forward,
18
Only one line containing a number N (1 ≤ N ≤ 10 ) . for example, arrays { 1 } and { 1,2,3,2,1 } are palindromes, while arrays {
1,12 } and { 4,7,5,4 } are not.
Output
Print the answer required above. NOTE: Solve it using recursion.
input Input
5
First line contains a number N (1 ≤ N ≤ 10 ) number of elements.
1
9
https://codeforces.com/group/MWSDmqGsZm/contest/223339/problems 4/7
9/25/25, 11:18 PM Problems - Codeforces
9
Second line contains N numbers (1 ≤ Ai ≤ 10 ). output
Output 1
Print "YES" (without quotes) if A is a palindrome array, otherwise, print
"NO" (without quotes). For more information visit combination:
https://www.mathsisfun.com/combinatorics/combinations-
input permutations.html
5
1 3 2 3 1
U. Knapsack
output
2 seconds, 256 megabytes
YES
There are N items numbered from 1 to N . The ith item has a weight of
input wi and a value of vi .
4 You have to choose some items out of the N items and carry them home
1 2 3 4
in a knapsack. The capacity of the knapsack is W which donate the
output maximum weight that can be carried inside the knapsack. In other words,
W means the total summation of all weights of items that can be carried
NO
in the knapsack.
Print maximum possible sum of values of items that you can take home.
S. Array Average
Note: Solve this problem using recursion.
1 second, 256 megabytes
Input
Given a number N and an array A of N numbers. Calculate the average
First line contains two numbers N and W
of these numbers.
(1 ≤ N ≤ 20, 1 ≤ W ≤ 100) number of items and the capacity of the
output output
3.000000 90
input
T. Combination
6 15
2 seconds, 64 megabytes 6 5
5 6
Given two numbers N and R. Print the NCR value. 6 4
6 6
Note: Solve this problem using recursion. 3 5
7 2
Input output
Only one line contains two numbers N and R (0 ≤ N, R ≤ 30).
17
Output
Print the NCR value.
V. Creating Expression1
input 1 second, 256 megabytes
4 2
Given two numbers N ,X and an array A of N numbers. Determine if
output there is a way to put '+' or '-' signs between every two numbers in the
6 array A in order to make an expression that is equal to X.
https://codeforces.com/group/MWSDmqGsZm/contest/223339/problems 5/7
9/25/25, 11:18 PM Problems - Codeforces
Second line contains N distinct numbers A 1 , A 2 , . . . . A N A i+1,j if and only if i ≤ N
(1 ≤ A i ≤ 10 )
5
. A i,j+1 if and only if j ≤ M
Output
Print "YES" if you can put '+' or '-' signs between every two number to Note: Solve this problem using recursion.
create an expression that is equal to X otherwise, print "NO". Input
First line contains two numbers N and M (1 ≤ N , M ≤ 10) N
input donates number of rows and M donates number of columns.
output
Output
YES Print the maximum sum of numbers can be obtained.
input input
5 2 3 3
1 2 3 4 5 5 2 4
1 3 5
output 9 2 7
NO
output
In the first example: 1 - 2 - 3 + 4 + 5 = 5 24
Given a number N . Initially you have a value equal 1 and you can perform Given two numbers S and E where S denotes a start point and E
one of the following operation any number of times: denotes an end point. Determine how many possible ways to reach point
E if you can move either 1 step, 2 steps or 3 steps at a time.
1. Multiply your current value by 10.
2. Multiply your current value by 20. Note: Solve this problem using recursion.
https://codeforces.com/group/MWSDmqGsZm/contest/223339/problems 6/7
9/25/25, 11:18 PM Problems - Codeforces
input output
5 4 4 5 7 7
4 3 5 7 3
https://codeforces.com/group/MWSDmqGsZm/contest/223339/problems 7/7