0% found this document useful (0 votes)
82 views7 pages

Problems - 7 Recursion Codeforces

The document outlines various programming problems that require recursive solutions, including tasks such as printing messages, calculating factorials, and determining palindromes. Each problem specifies input and output requirements, along with constraints on the values. The problems range from simple tasks like printing numbers to more complex ones like solving the knapsack problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views7 pages

Problems - 7 Recursion Codeforces

The document outlines various programming problems that require recursive solutions, including tasks such as printing messages, calculating factorials, and determining palindromes. Each problem specifies input and output requirements, along with constraints on the values. The problems range from simple tasks like printing numbers to more complex ones like solving the knapsack problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

9/25/25, 11:18 PM Problems - Codeforces

Sheet #7 (Recursion)

A. Print Recursion Make sure don't print any leading or trailing spaces.

1 second, 256 megabytes


D. Print Digits using Recursion
Given a number N . Print "I love Recursion" N times.
1 second, 256 megabytes
Note: Solve this problem using recursion.
Given a number N. Print the digits of N separated by a space.
Input
Only one line containing a number N (1 ≤ N ≤ 100) . Note: Solve this problem using recursion.

Output Input
Print "I love Recursion" N times. First line contains a number T (1 ≤ T ≤ 10) number of test cases.

Next T lines will contain a number N (0 ≤ N ≤ 109).


input
3 Output
For each test case print a single line contains the digits of the number
output separated by space.
I love Recursion
I love Recursion input
I love Recursion
3
121
39
B. Print from 1 to N 123456
1 second, 64 megabytes output
Given a number N. Print numbers from 1 to N in separate lines. 1 2 1
3 9
Note: Solve this problem using recursion. 1 2 3 4 5 6

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 Note: Solve this problem using recursion.


5
Input
output First line contains a number T (1 ≤ T ≤ 104) number of test cases.
1 Next T lines will contain a number N (1 ≤ N ≤ 109).
2
3
Output
4
For each test case print a single line contains the answer according to the
5
required above.

C. Print from N to 1 input


1 second, 64 megabytes 2
10
3
Given a number N. Print all numbers from N to 1 separated by a single
space. output
Note: Solve this problem using recursion. 1010
11
Input
Only one line containing a number N (1 ≤ N ≤ 103). To convert decimal number to binary :

A decimal integer can be converted to binary by dividing it by 2.


Output
Print from N to 1 separated by a single space. Take the quotient, and keep dividing it by 2, until you reach zero.

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

1 second, 256 megabytes output


***
Given a number N . Print a pyramid of height N . *

Note: Solve this problem using recursion.


input
Input
3
Only one line containing a number N
3
(1 ≤ N ≤ 10 ) .
output
Output
*****
Print the pyramid in N lines. ***
*
input
1 input
output 4

* 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

1 second, 256 megabytes

Given a string S. Print number of vowels in the string.


L. Summation
1 second, 256 megabytes
Note:
Given a number N and an array A of N numbers. Print the summation of
Vowel letters: ['a', 'e', 'i', 'o', 'u'].
the array elements.
Vowel letters could be capital or small.
Solve this problem using recursion. Note: Solve this problem using recursion.

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
.

Second line contains N numbers (−109 ≤ A i ≤ 10 )


9
.
K. Max Number
Output
1 second, 64 megabytes Print the sum of the last M numbers of the given array.

Given a number N and an array A of N numbers. Print the maximum


input
value in this array.
5 3
Note: Solve this problem using recursion. 1 8 2 10 3

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.

output The sequence is constructed as follows:


2 5 8
11 14 17
If the number n is odd, the next number will be 3n + 1.
If the number n is even, the next number will be n/2 .

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

Note: Solve this problem using recursion. output


1
Input
Only one line containing a number N (1 ≤ N ≤ 30).
input
Output 2
Print the value of the Nth Fibonacci number.
output
input 2

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

Note: solve this problem using recursion. knapsack.

Input Next N lines will contain two numbers wi and vi


First line contains a number N (1 ≤ N ≤ 100) number of elements. (1 ≤ wi ≤ 50, 1 ≤ vi ≤ 1000)

Second line contains N numbers (−10


9 9
≤ A i ≤ 10 ) . Output
Print maximum possible sum of values of items that you can take home.
Output
Print the calculated average, with 6 digits after the decimal point. input
3 8
input 3 30
5 4 50
1 2 3 4 5 5 60

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.

Note: Solve this problem using recursion.


input
3 3 Input
First line contains two numbers N and X
(1 ≤ N ≤ 20, −10
9
≤ X ≤ 10 )
9
.

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.

5 5 Next N lines each of them will contain M numbers


1 2 3 4 5 (−10 ≤ A i,j ≤ 10 ) .
5 5

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

W. Reach Value Y. Number of Ways


1 second, 256 megabytes 1 second, 256 megabytes

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.

Determine if your value can reach N or not.


Input
Only one line contains two numbers S and E (1 ≤ S ≤ E ≤ 15) .
Note: Solve this problem using recursion.
Output
Input Print the answer required above.
First line contains a number T (1 ≤ T ≤ 100) number of test cases.
12
input
Next T lines will contain a number N (1 ≤ N ≤ 10 ) .
2 5
Output output
For each test case print "YES" if your value can reach exactly N
otherwise, print "NO". 4

In the first example:


input
5 There are 4 ways to reach from point 2 to point 5 as follows: [2, 3, 4, 5], [2,
1 3, 5], [2, 4, 5] and [2, 5].
2
10
25 Z. Left Max
200
1 second, 256 megabytes
output
YES Given a number N and an array A of N numbers, print the maximum in
NO the range from 1 to i for each i ≤ N .
YES
NO Note: Solve this problem using recursion.
YES
Input
First line contains a number N
5
(1 ≤ N ≤ 10 ) number of elements.
X. The maximum path-sum
Second line contains N numbers (−10
9 9
≤ A i ≤ 10 ) .
1 second, 256 megabytes
Output
Given a matrix A of size N *M . Print the maximum sum of numbers that Print N numbers, the maximum from index 1 to index i .
can be obtained when you take a path from A 1,1 to A N ,M .

If you stay in A i,j you can only go to :

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

Codeforces (c) Copyright 2010-2025 Mike Mirzayanov


The only programming contests Web 2.0 platform

https://codeforces.com/group/MWSDmqGsZm/contest/223339/problems 7/7

You might also like