0% found this document useful (0 votes)
29 views13 pages

Write A Recursive Code To Check Given Number Is Prime or Not

Uploaded by

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

Write A Recursive Code To Check Given Number Is Prime or Not

Uploaded by

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

1. Write a recursive code to check given number is prime or not.

Description:- Prime numbers are the natural numbers greater than 1 with exactly two factors,
i.e. 1 and the number itself.
Example:-
Input : 11
Output : 1
Input : 15
Output : 0

2. Write a recursive code to print binary representation of number.


Example:-
Input : 7
Output : 111
Input : 8
Output : 1000

Description:- Binary Numbers are represented by two digits, 0 (zero) and 1 (one). Binary
numbers are represented in the base-2 numeral system. Every digit in the binary system is
called a bit. Decimal numbers refer to the base-10 numbering system.
Generally, a decimal can be anything based on the number 10. In C++, we can convert a
decimal number to a binary number using the Iterative method or recursive method.

3. Write a recursive code to reverse the given array with out using any extra space.
Example:-

Input : 5

16794

Output : 4 9 7 6 1

Input : 7

4 5 6 8 7 12 5

Output : 5 12 7 8 6 5 4

Description :- Array reverse or reverse a array means changing the position of each number of
the given array to its opposite position from end, i.e. if a number is at position 1 then its new
position will be Array.length, similarly if a number is at position 2 then its new position will be
Array.length – 1, and so on.
4. Write a recursive code to find first occurrence of given element in the array. If
element not found return -1.
Example:-

Input : 5
16794
6
Output : 1
Input : 7
4 5 6 8 7 12 5
12
Output : 5
Input : 6
1 6 7 9 4 25
37
Output : -1
Description :- The idea to solve this problem is iterate on the elements of given array and
check given elements in an array and keep track of first and last occurrence of the found
element’s index.

5. Write a recursive code to find last occurrence of given element in the array. If element not
found return -1.

Example:-

Input : 5
16764
6
Output : 3
Input : 7
4 5 7 8 7 12 5
7
Output : 4

Description:- Given an array of length N and an integer x, you need to find and return the last index of
integer x present in the array. Return -1 if it is not present in the array. Last index means - if x is present
multiple times in the array, return the index at which x comes last in the array. You should start traversing
your array from 0, not from (N - 1). Do this recursively. Indexing in the array starts from 0.

6. Write recursive code check given array is sorted or not.


Examples:-
Input : 5
16764
Output : 0
Input : 7
4 5 6 8 9 12 25
Output : 1
Description:- Given an array of size n, write a program to check if it is sorted in ascending
order or not. Equal values are allowed in an array and two consecutive equal values are
considered sorted.

7.Given a “2 x n” board and tiles of size “2 x 1”, count the number of ways to tile the given
board using the 2 x 1 tiles. A tile can either be placed horizontally i.e., as a 1 x 2 tile or vertically
i.e., as 2 x 1 tile.

Examples:
Input: n = 4
Output: 5
Explanation:
For a 2 x 4 board, there are 5 ways

 All 4 vertical (1 way)


 All 4 horizontal (1 way)
 2 vertical and 2 horizontal (3 ways)
8.You are a professional robber planning to rob houses along a street. Each house has a certain
amount of money stashed, the only constraint stopping you from robbing each of them is that
adjacent houses have security systems connected and it will automatically contact the police if
two adjacent houses were broken into on the same night. Given an integer array
nums representing the amount of money of each house, return the maximum amount of money
you can rob tonight without alerting the police.

Example 1:
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.

9. Print all subsequence of the given array of n elements.


Example:-
Input:- 3
154
Output:-
154
15
14
1
54
5
4
Description:- A subarray is a contiguous part of the array. An array that is inside another array. For
example, consider the array [1, 2, 3, 4], There are 10 non-empty sub-arrays. The subarrays are (1),
(2), (3), (4), (1,2), (2,3), (3,4), (1,2,3), (2,3,4) and (1,2,3,4).
10. Generate all permutations of the given string.
Example:-

Input:- Pin
Output:-

Pin
Pni
iPn
inP
niP
nPi
Description:- A permutation also called an “arrangement number” or “order,” is a
rearrangement of the elements of an ordered list S into a one-to-one correspondence with S
itself. A string of length N has N! permutations.
11.Given a collection of candidate numbers (candidates) and a target number (target), find all
unique combinations in candidates where the candidate numbers sum to target.
Each number in candidates may only be used once in the combination.
Note: The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [10,1,2,7,6,1,5], target = 8
Output:
[[1,1,6], [1,2,5], [1,7], [2,6] ]
12.Given an array of distinct integers candidates and a target integer target, compute a list of
all unique combinations of candidates where the chosen numbers sum
to target. The same number may be chosen from candidates an unlimited number of times
Example 1:
Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.
13. Given an array nums of distinct integers, return all the possible permutations. You can return
the answer in any order.

Example 1:
Input: 3
189
Output:
189
198
819
891
981
918

You might also like