Write A Recursive Code To Check Given Number Is Prime or Not
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
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.
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
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.
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