SERIES 1-10 Practice Questions C++
SERIES 1-10 Practice Questions C++
QUES1. One of the important aspect of object oriented programming is readability of the code. To
enhance the readability of code, developers write function and variable names in Camel Case. You are given
a string, S, written in Camel Case. FindAllTheWordsContainedInIt.
Sample Input
IAmACompetitiveProgrammer
Sample Output
I
Am
A
Competitive
Programmer
QUES 2. Take as input S, a string. Write a function that does basic string compression. Print the value
returned. E.g. for input “aaabbccds” print out a3b2c2d1s1.
Sample Input
aaabbccds
Sample Output
a3b2c2d1s1
Explanation
In the given sample test case 'a' is repeated 3 times consecutively, 'b' is repeated twice, 'c' is repeated twice
and 'd and 's' occurred only once.
QUES 3. Take as input N, the size of array. Take N more inputs and store that in an array. Take as input
“target”, a number. Write a function which prints all pairs of numbers which sum to target.
Input Format
The first line contains input N. Next N lines contains the elements of array and (N+1)th line contains target
number.
Sample Input
5
1
3
4
2
5
5
Sample Output
1 and 4
2 and 3
Explanation
Find any pair of elements in the array which has sum equal to target element and print them.
QUES 4. Take as input S, a string. Write a function that does basic string compression. Print the value
returned. E.g. for input “aaabbccds” print out a3b2c2ds.
Sample Input
aaabbccds
Sample Output
a3b2c2ds
Explanation
In the given sample test case 'a' is repeated 3 times consecutively, 'b' is repeated twice, 'c' is repeated twice.
But, 'd' and 's' occurred only once that's why we do not write their occurrence.
QUES 5. Take as input S, a string. Write a program that inserts between each pair of characters the
difference between their ascii codes and print the ans.
Sample Input
acb
Sample Output
a2c-1b
Explanation
For the sample case, the Ascii code of a=97 and c=99 ,the difference between c and a is 2.Similarly ,the Ascii
code of b=98 and c=99 and their difference is -1. So the ans is a2c-1b.
Each number is separated from other by a tab. For given input n, You need to print n(n+1)/2 fibonacci numbers.
Kth row contains , next k fibonacci numbers.
QUES 7. An array of numbers. You need to arrange them in a way that yields the largest value.
Input Format
Sample Input
1
4
54 546 548 60
Sample Output
6054854654
Explanation
Upon rearranging the elements of the array , 6054854654 is the largest possible number that can be
generated.
QUES 8. Take as input S, a string. Write a function that removes all consecutive duplicates. Print the
value returned.
Sample Input
aabccba
Sample Output
abcba
Explanation
After removing all of the consecutive occurences, the Final ans will be : - "abcba".
QUES 9. Take as input N, the size of array. Take N more inputs and store that in an array. Take as input
“target”, a number. Write a function which prints all triplets of numbers which sum to target.
Input Format
Output Format
Print all the triplet present in the array in a new line each. The triplets must be printed as A, B and C where
A,B and C are the elements of the triplet ( A<=B<=C) and all triplets must be printed in sorted order. Print
only unique triplets.
Sample Input
9
5 7 9 1 2 4 6 8 3
10
Sample Output
1, 2 and 7
1, 3 and 6
1, 4 and 5
2, 3 and 5
Explanation
Array = {5, 7, 9, 1, 2, 4, 6 ,8 ,3}. Target number = 10. Find any three number in the given array which sum to
target number.
QUES 10. Take as input S, a string. Write a function that replaces every even character with the
character having just higher ASCII code and every odd character with the character having just lower ASCII
code. Print the value returned.
Output Format
String
Sample Input
abcg
Sample Output
badf
SERIES 2
QUES1. You are given an input array whose each element represents the height of a line towers. The width of
every tower is 1. It starts raining. Water is filled between the gap of towers if possible. You need to find how much
water filled between these given towers.
Example : 9k
Input Format
The first line consists of number of test cases T. Each test case consists an integer N as number of towers and next line
contains the N space separated integers.
Sample Input
2
6
3 0 0 2 0 4
12
0 1 0 2 1 0 1 3 2 1 2 1
Sample Output
10
6
Each number is separated from other by a tab. For given input n, You need to print n(n+1)/2 fibonacci numbers. Kth
row contains , next k fibonacci numbers.
QUES 3. Take as input S, a string. Write a function that returns true if the string is a palindrome and false
otherwise. Print the value returned.
Sample Input
abba
Sample Output
true
Explanation
A string is said to be palindrome if reverse of the string is same as string. For example, “abba” is palindrome as it's
reverse is "abba", but “abbc” is not palindrome as it's reverse is "cbba".
QUES 4. Take as input S, a string. Write a function that returns the character with maximum frequency. Print the
value returned.
Sample Input
aaabacb
Sample Output
a
Explanation
For the given input string, a appear 4 times. Hence, it is the most frequent character.
QUES 5. Deepak has a limited amount of money that he can spend on his girlfriend. So he decides to buy two
roses for her. Since roses are of varying sizes, their prices are different. Deepak wishes to completely spend that fixed
amount of money on buying roses for her.
As he wishes to spend all the money, he should choose a pair of roses whose prices when summed up are equal to the
money that he has.
Help Deepak choose such a pair of roses for his girlfriend.
NOTE: If there are multiple solutions print the solution that minimizes the difference between the prices i and j. After
each test case, you must print a blank line.
Input Format
Output Format
For each test case, you must print the message: ‘Deepak should buy roses whose prices are i and j.’, where i and j are
the prices of the roses whose sum is equal do M and i ≤ j. You can consider that it is always possible to find a solution.
If there are multiple solutions print the solution that minimizes the difference between the prices i and j.
Sample Input
2
2
40 40
80
5
10 2 6 8 4
10
Sample Output
Deepak should buy roses whose prices are 40 and 40.
Deepak should buy roses whose prices are 4 and 6.
QUES 7. Given an array arr of n integers where n > 1, return an array output such that output[i] is equal to the
product of all the elements of arr except arr[i].
Sample Input
4
1 2 3 4
Sample Output
24 12 8 6
QUES 8. Take as input N, a number. Take N more inputs and store that in an array. Write a recursive function
which inverses the array. Print the values of inverted array
Sample Input
5
0 2 4 1 3
Sample Output
0 3 1 4 2
Explanation: Swap element with index. for eg : element 4 at index 2 becomes element 2 at index 4
QUES 9. Take as input N, the size of array. Take N more inputs and store that in an array. Take as input a
number M. Write a function which returns the index on which M is found in the array, in case M is not found -1 is
returned. Print the value returned. You can assume that the array is sorted, but you’ve to optimize the finding process.
For an array of size 1024, you can make 10 comparisons at maximum.
Sample Input
5
3
5
6
9
78
6
Sample Output
2
Explanation
Array = {3, 5, 6, 9, 78}, target number = 6 . Index of number 6 in the given array = 2. Write Binary search to find the
number in given array as discuss in the class.
QUES 10. Take as input N, the size of an array. Take N more inputs and store that in an array. Take another
number’s input as M. Write a function which returns the index on which M is found in an array, in case M is not found
-1 is returned. Print the value returned.
1. It reads a number N.
2.Take Another N numbers as an input and store them in an Array.
2. Take another number M as an input.
3. If M is found in the Array the index of M is returned else -1 is returned and print the value returned.
Sample Input
5
2
4
6
9
17
17
Sample Output
4
Explanation
Sample Input
4
2
8
6
4
Sample Output
8
Explanation
QUES 2. You are provided two sorted arrays. You need to find the maximum length of bitonic
subsequence. You need to find the sum of the maximum sum path to reach from beginning of any array to
end of any of the two arrays. You can switch from one array to another array only at common elements.
Input Format
First line contains integer t which is number of test case. For each test case, it contains two integers n and m
which is the size of arrays and next two lines contains n and m space separated integers respectively.
Sample Input
1
8 8
2 3 7 10 12 15 30 34
1 5 7 8 10 15 16 19
Sample Output
122
Explanation
QUES 3. It is Alex’s birthday and she wants to go shopping. She only has ‘A’ units of money and she
wants to spend all of her money. However, she can only purchase one kind of item. She goes to a shop
which has ‘n’ types items with prices A0,A1,A2,…,An-1. The shopkeeper claims that he has atleast ‘k’ items
she can choose from. Help her find out if the shopkeeper is correct or not.
Input Format
The first line contains an integer ‘n’ denoting the number of items in the shop. The second line contains ‘n’
space-separated integers describing the respective price of each item. The third line contains an integer ‘q’
denoting the number of queries. Each of the subsequent lines contains two space-separated integers ‘A’ and
‘k’
Sample Input
4
100 200 400 100
5
100 2
200 3
500 4
600 4
800 4
Sample Output
Yes
Yes
No
No
Yes
Explanation
In query 1, Alex has 100 units of money. The shopkeeper claims that she can choose to buy from 2 kinds of
items i.e. item 1 and item 4 each priced at 100.
In query 2, The shopkeeper claims that she can choose to buy from 3 kinds of items ie item 1 and item 4 each
priced at 100(she can buy 1 from either of the two), or item 2 priced at 200(she can buy one)
In query 3, she has 500 units of money. She can either buy item 1 or item 4 ( 5 of each kind respectively). Thus,
she has only 2 kinds of items to choose from.
In query 5, she has 800 units of money. She can either buy item 1 or item 4 ( 8 of each kind respectively) or
item 2(she can buy 4 of these) or item 3(2 of these). Thus, she has 4 kinds of items to choose from.
QUES 4. Take as input N, the size of array. Take N more inputs and store that in an array. Write a
function that reverses the array. Print the values in reversed array.
Sample Input
5
0
4
6
8
9
Sample Output
9
8
6
4
0
Explanation
QUES 5. Print "lowercase" if user enters a character between 'a-z' , Print "UPPERCASE" is character lies
between 'A-Z' and print "Invalid" for all other characters like $,.^# etc.
Sample Input
$
Sample Output
Invalid
Sample Input
8
4
Sample Output
5
11
14
17
23
26
29
35
QUES 8. Take N as input, Calculate it's reverse also Print the reverse.
Sample Input 123456789
Sample Output 987654321
A positive integer of n digits is called an Armstrong number of order n (order is number of digits) if.
Input Format
First line contains three integer coefficients a,b,c for the equation ax^2 + bx + c = 0.
Output Format
Output contains one/two lines. First line contains nature of the roots .The next line contains roots(in non-
decreasing order) separated by a space if they exist. If roots are imaginary do not print the roots. Output the
integer values for the roots.
The input corresponds to equation ax^2 + bx + c = 0. Roots = (-b + sqrt(b^2 - 4ac))/2a , (-b - sqrt(b^2 -
4ac))/2a
QUES 11. Input three numbers, print the largest of these numbers
Sample Input 5 7 4
Sample Output 7
QUES 12. Read as input 5 Numbers and print the largest out of them
Sample Input 2 4 7 -2 3
Sample Output 7
QUES 13. Take as input a number N, print "Prime" if it is prime if not Print "Not Prime".
Sample Input 3
Sample Output Prime
QUES 14. Take the following as input. A number (N1) A number (N2)
Write a function which returns the LCM of N1 and N2. Print the value returned.
Sample Input
4
6
Sample Output
12
Explanation
The smallest number that is divisible by both N1 and N2 is called the LCM of N1 and N2.
QUES 15. Take the following as input. A number (N1) A number (N2)
Write a function which returns the GCD of N1 and N2. Print the value returned.
Sample Input
16
24
Sample Output
8
Explanation
The largest number that divides both N1 and N2 is called the GCD of N1 and N2.
SERIES 4
QUES 1. Help Manmohan to print pattern of a given number. See the output pattern for given input n = 5.
Sample Output
1
11
202
3003
40004
Explanation
If row number is n (>1), total character is n. First and last character is n-1 and rest are 0.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
QUES 4. Take as input N, a number. Print the pattern as given in the input and output section.
Sample Input
7
Sample Output
1******
12*****
123****
1234***
12345**
123456*
1234567
Explanation
There is no space between any two numbers. Catch the pattern for corresponding input and print them
accordingly
QUES 5. Take N (number of rows), print the following pattern (for N = 4) [FLOYD’S TRIANGLE]
Sample Output
1
2 3
4 5 6
7 8 9 10
Explanation Each number is separated from other by a tab.
QUES 6. Given number of rows N, you have to print a Hollow Rhombus. See the output for
corresponding given input.
Sample Output
*****
* *
* *
* *
*****
Explanation
For any input N. First line contains 4 space and then 5 {*} and then the second line contains according to the
output format.
QUES 7. Given N, help Manmohan to print pattern upto N lines. For eg For N=6 , following pattern will be printed.
Sample Output
1
11
111
1001
11111
100001
Explanation
For every odd number row print 1, odd number of times and for every even number row , print first and last
character as 1 and rest of middle characters as 0.
QUES 9. Given below is plus pattern. It has the shape of the mathematical plus sign(+).
*
*
*****
*
*
* *
* * *
* * * *
* * * * *
QUES 11. Write a Program to print the Full Inverted Pyramid Alphabet Pattern.
EEEEEEEEE
DDDDDDD
CCCCC
BBB
A
A QUES 12. Write a Program to Print Half Diamond Star Pattern.
*
**
***
****
*****
****
***
**
*
*
* *
* *
* *
* *
QUES 15. Write a Program to Print the Hollow Half Pyramid Star Pattern.
*
**
* *
* *
*****
QUES 16. DWrite a program to print the Alphabet Inverted Half Pyramid Pattern.
ABCDE
ABCD
ABC
AB
A
QUES 17. Write a Program to print the Solid Inverted Half Diamond Alphabet Pattern.
F
EF
DEF
CDEF
BCDEF
ABCDEF
BCDEF
CDEF
DEF
EF
F
QUES 18.
QUES 2. Take as input N, the size of array. Take N more inputs and store that in an array. Write a function
to implement the selection sort algorithm and print the sorted array through function.
QUES 5. Take as input str, a string. str represents keys pressed on a nokia phone keypad. We are
concerned with all possible words that can be written with the pressed keys.
Assume the following alphabets on the keys: 1 -> abc , 2 -> def , 3 -> ghi , 4 -> jkl , 5 -> mno , 6 -> pqrs , 7
-> tuv , 8 -> wx , 9 -> yz
E.g. “12” can produce following words “ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”
a. Write a recursive function which returns the count of words for a given keypad string. Print the value
returned.
b.Write a recursive function which prints all possible words for a given keypad string (void is the return type
for function).
Sample Input
12
Sample Output
ad ae af bd be bf cd ce cf
9
Explanation
Sample Input
3
xpix
xabpixx3.15x
xpipippixx
Sample Output
x3.14x
xab3.14xx3.15x
x3.143.14p3.14xx
Explanation
QUES 10. Given an integer N, now you have to convert all zeros of N to 5.
Sample Input
103
Sample Output
153
Explanation
QUES 12. Take as input N, a number. Print the following pattern (for N =4)
*
**
***
****
*****
Input Format
QUES 13. You are given an N*N sudoku grid (N is a multiple of 3). Solve the sudoku and print the
solution.
To learn more about sudoku, go to this link Sudoku-Wikipedia.
Input Format
First line contains a single integer N. Next N lines contains N integers each, where jth integer int ith line
denotes the value at ith row and jth column in sudoku grid. This value is 0, if the cell is empty.
Sample Input
9
5 3 0 0 7 0 0 0 0
6 0 0 1 9 5 0 0 0
0 9 8 0 0 0 0 6 0
8 0 0 0 6 0 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9
Sample Output
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9
QUES 14. Take as input str, a string. We are concerned with all the possible ascii-subsequences of str.
E.g. “ab” has following ascii-subsequences “”, “b”, “98”, “a”, “ab”, “a98”, “97”, “97b”, “9798”
a. Write a recursive function which returns the count of ascii-subsequences for a given string. Print the value
returned.
b. Write a recursive function which prints all possible ascii-subsequences for a given string (void is the
return type for function).
Output Format Display the number of ASCII-subsequences and display all the ASCII- subsequences
Sample Input ab
Sample Output
b 98 a ab a98 97 97b 9798
9
QUES 15. Take as input N, a number. Write a recursive function to find Nth triangle where 1st triangle
is 1, 2nd triangle is 1 + 2 = 3, 3rd triangle is 1 + 2 + 3 = 6, so on and so forth. Print the value returned.
Sample Input 4
Sample Output 10
QUES 16. Take as input str, a string. Write a recursive function which returns a new string in which all
duplicate consecutive characters are separated by a ‘ * ’. E.g. for “hello” return “hel*lo”. Print the value
returned
Explanation
After the 1st operation (X = 1), the array would be [1+5, 2+1, 3+2, 4+3, 5+4] =[6, 3, 5, 7, 9]
After 2nd operation (X = 0), the array would be [6+6, 3+3, 5+5, 7+7, 9+9] =[12, 6, 10, 14, 18]
Thus the correct answer would equal to = (12+6+10+14+18) % (10^9+7) = 60
QUES2. A Boston number is a composite number, the sum of whose digits is the sum of the digits of its prime
factors obtained as a result of prime factorization (excluding 1 ). The first few such numbers are 4,22 ,27 ,58 ,85
,94 and 121 . For example, 378 = 2 × 3 × 3 × 3 × 7 is a Boston number since 3 + 7 + 8 = 2 + 3 + 3 + 3 + 7.
Output Format
1 if the number is a Boston number. 0 if the number is a not Boston number.
Sample Input
378
Sample Output
1
QUES3. You are provided n numbers (both +ve and -ve). Numbers are arranged in a circular form.
First line contains integer t which is number of test case.
For each test case, it contains an integer n which is the size of array and next line contains n space separated
integers denoting the elements of the array.
Sample Input
1
7
8 -8 9 -9 10 -11 12
Sample Output
22
Explanation
Maximum Circular Sum = 22 (12 + 8 - 8 + 9 - 9 + 10)
QUES4. * * * * *
* * * *
* *
* * * *
* * * * *
Sample Input
5
QUES5. Take N as input. For a value of N=7, we wish to draw the following pattern :
Constraints
N is odd number.
Sample Input
7
Sample Output
1
2 1 1 2
3 2 1 1 2 3
4 3 2 1 1 2 3 4
3 2 1 1 2 3
2 1 1 2
1
QUES6. Ramu often uses public transport. The transport in the city is of two types: cabs and rickshaws. The city
has n rickshaws and m cabs, the rickshaws are numbered by integers from 1 to n, the cabs are numbered by
integers from 1 to m.
Public transport is not free. There are 4 types of tickets:
A ticket for one ride on some rickshaw or cab. It costs c1 ruppees;
A ticket for an unlimited number of rides on some rickshaw or on some cab. It costs c2 ruppees;
A ticket for an unlimited number of rides on all rickshaws or all cabs. It costs c3 ruppees;
A ticket for an unlimited number of rides on all rickshaws and cabs. It costs c4 ruppees.
Ramu knows for sure the number of rides he is going to make and the transport he is going to use. He asked you
for help to find the minimum sum of ruppees he will have to spend on the tickets.
Input Format
Each Test case has 4 lines which are as follows:
The first line contains four integers c1, c2, c3, c4 (1 ≤ c1, c2, c3, c4 ≤ 1000) — the costs of the tickets.
The second line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of rickshaws and cabs Ramu is
going to use.
The third line contains n integers ai (0 ≤ ai ≤ 1000) — the number of times Ramu is going to use the rickshaw
number i.
The fourth line contains m integers bi (0 ≤ bi ≤ 1000) — the number of times Ramu is going to use the cab
number i.
Output Format
For each testcase , print a single number - the minimum sum of rupees Ramu will have to spend on the tickets.
Sample Input
2
1 3 7 19
2 3
2 5
4 4 4
4 3 2 1
1 3
798
1 2 3
Sample Output
12
1
Explanation
The total cost of rickshaws = min( min(2 * 1, 3) + min(5 * 1, 3), 7) = min(5, 7) = 5
The total cost of cabs = min( min(4 * 1, 3) + min(4 * 1, 3) + min(4 * 1, 3) , 7) = min ( 9, 7) = 7
Total final cost = min( totalCabCost + totalRickshawCost , c4) = min( 5 + 7, 19) = min ( 12, 19) = 12
We print 12.
QUES7. Write a function that toggles the case of all characters in the string. Print the value returned.
Sample Input
abC
Sample Output
ABc
Explanation
Toggle Case means to change UpperCase character to LowerCase character and vice-versa.
QUES8. A Good String is a string which contains only vowels (a,e,i,o,u) . Given a string S, print a single
positive integer N where N is the length of the longest substring of S that is also a Good String.
Output Format
A single positive integer N, where N is the length of the longest sub-string of S that is also a Good String.
Sample Input
cbaeicde
Sample Output
3
Explanation
Longest good substring is "aei"
QUES9. Deepak and Gautam are having a discussion on a new type of number that they call Coding Blocks
Number or CB Number. They use following criteria to define a CB Number.
1. 0 and 1 are not a CB number.
2. 2,3,5,7,11,13,17,19,23,29 are CB numbers.
3. Any number not divisible by the numbers in point 2( Given above) are also CB numbers.
Deepak said he loved CB numbers.Hearing it, Gautam throws a challenge to him.
Gautam will give Deepak a string of digits. Deepak's task is to find the number of CB numbers in the string.
• CB number once detected should not be sub-string or super-string of any other CB number.
Ex- In 4991, both 499 and 991 are CB numbers but you can choose either 499 or 991, not both.
• Further, the CB number formed can only be a sub-string of the string.
Ex - In 481, you can not take 41 as CB number because 41 is not a sub-string of 481.
As there can be multiple solutions, Gautam asks Deepak to find the maximum number of CB numbers that can be
formed from the given string.
Deepak has to take class of Launchpad students. Help him by solving Gautam's challenge.
Input Format
First line contain size of the string.
Next line is A string of digits.
Output Format
Maximum number of CB numbers that can be formed.
Sample Input
5
81615
Sample Output
2
Explanation
61 and 5 are two CB numbers.
QUES10. Take N as input. Print the sum of its odd placed digits and sum of its even placed digits.
Sample Input
2635
Sample Output
11
5
Explanation
5 is present at 1st position, 3 is present at 2nd position, 6 is present at 3rd position and 2 is present
at 4th position. Sum of odd placed digits on first line. 5 and 6 are placed at odd position. Hence odd
place sum is 5+6=11 Sum of even placed digits on second line. 3 and 2 are placed at even position.
Hence even place sum is 3+2=5
Input Format
Integer (A number) Integer (A digit)
Output Format
Sample Input
5433231
3
Sample Output
3
Explanation
The digit can be from 0 to 9. Assume decimal numbers.In the given case digit 3 is occurring 3 times in
the given number.
QUES 12. Given an array A of size N , write a function that implements insertion sort on the array. Print the
elements of sorted array.
Input Format
First line contains a single integer N denoting the size of the array. Next line contains N space seperated integers
where ith integer is the ith element of the array.
Output Format
Output N space seperated integers of the sorted array in a single line.
Sample Input
4
3 4 2 1
Sample Output
1 2 3 4
QUES 13. Given an array nums of length n. We define a running sum of an array as runningSum[i] =
sum(nums[0]…nums[i]).
Input Format
First line contains an integer n representing number of elements. Next line contains n integers denoting array
elements.
Sample Input
4
1 2 3 4
Sample Output
1 3 6 10
Explanation
Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
QUES 14. Given an integer array of N integers , sort the array using the merge sort algorithm.
Input Format
First line of input contains integer n denoting the size of the array. The next line of input contains n space
separated integers denoting the elements of the array.
Sample Input
5
4 5 3 1 2
Sample Output
1 2 3 4 5
QUES 15. Given an array, print the Next Greater Element (NGE) for every element. The Next Greater Element
for an element x is the first greater element on the right side of x in array. Elements for which no greater element
exist, consider next greater element as -1.
Input Format
First line of the input contains a single integer T denoting the number of testcases. First line of each testcase
contains an integer N denoting the size of array. Second line of each testcase contains N space seperated integers
denoting the array.
Sample Input
2
4
11 13 21 3
5
11 9 13 21 3
Sample Output
11,13
13,21
21,-1
3,-1
11,13
9,13
13,21
21,-1
3,-1
Explanation
For the first testcase , the next greater element for 11 is 13 , for 13 its 21 and 21 being the largest
element of the array does not have a next greater element. Hence we print -1 for 21. 3 is the last
element of the array and does not have any greater element on its right. Hence we print -1 for it as
well.
QUES 16. Given a circular array (the next element of the last element is the first element of the array), print the
Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to
its traversing-order next in the array, which means you could search circularly to find its next greater number. If
it doesn't exist, output -1 for this number.
Input Format
First line contains N - size of array.
Second line contains N space separated integers denoting array elements.
Sample Input
3
1 2 1
Sample Output
2 -1 2
Explanation
The first 1's next greater number is 2; The number 2 can't find next greater number; The second 1's
next greater number needs to search circularly, which is also 2.
QUES 17. Given an array, the task is to calculate the sum of lengths of contiguous subarrays having all elements
distinct
Input Format
An integer n denoting size of array followed by n integers
Sample Input
3
1 2 3
Sample Output
10
Explanation
{1, 2, 3} is a subarray of length 3 with distinct elements. Total length of length three = 3. {1, 2}, {2, 3}
are 2 subarray of length 2 with distinct elements. Total length of lengths two = 2 + 2 = 4 {1}, {2}, {3}
are 3 subarrays of length 1 with distinct element. Total lengths of length one = 1 + 1 + 1 = 3 Sum of
lengths = 3 + 4 + 3 = 10
QUES 18. We are given a circular array, print the next greater number for every element. If it is not found print -
1 for that number. To find the next greater number for element Ai , start from index i + 1 and go uptil the last
index after which we start looking for the greater number from the starting index of the array since array is
circular.
Input Format
First line contains the length of the array n. Second line contains the n space separated integers.
Sample Input
3
1 2 3
Sample Output
2 3 -1
Explanation
Next greater element for 1 is 2,
for 2 is 3 but not present for 3 therefore -1
SERIES 9
QUES 1. Take as input str, a string. Assume that value of a=1, b=2, c=3, d=4, …. z=26. Write a recursive
function (return type Arraylist) to print all possible codes for the string. E.g. for “1123” possible codes are
aabc, kbc, alc, aaw, kw.
Sample Input
1125
Sample Output
[aabe, aay, ale, kbe, ky]
QUES 2. Take as input str, a string. The string is a mathematical expression e.g. “[a + {b + (c + d) + e} + f]”.
Write a recursive function which tests if the brackets in expression are balanced or not and returns a Boolean
value. Print the value returned.
Sample Input
[a+{b+(c+d)+e}+f]
Sample Output
true
QUES 3. Take as input N, a number. Write a recursive function which prints counting from 0 to N in
lexicographical order. In lexicographical (dictionary) order 10, 100 and 109 will be printed before 11.
Sample Input
10
Sample Output
0 1 10 2 3 4 5 6 7 8 9
QUES 4. Take as input str, a string. Write a recursive function that checks if the string was generated
using the following rules and returns a Boolean value: 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' Print the value returned.
QUES 5. Mike is a very passionate about sets. Lately, he is busy solving one of the problems on sets. He
has to find whether if the sum of any of the non-empty subsets of the set A is zero.
Input Format
The first line contains an integer T, which is the total number of test cases.
T test cases follow.
Each test case consists of two lines.
The first line consists of a single integer N, which is the number of elements present in the set A.
The second line contains N space separated integers denoting the elements of the set A.
Sample Input
1
4
1 3 2 -3
Sample Output
Yes
Explanation
The sum of the subset {3,-3} is zero.
The first line contains an integer N , the size of the array. The next line conatins N integers. The next line
contains an integer K.
Output Format
Output all the subsets that sum to K. The output should be printed as follows :
Sample Input
5
1 4 6 5 3
10
Sample Output
6 4
3 6 1
5 4 1
Explanation
4 + 6 = 10
1 + 4 + 5 = 10
1 + 3 + 6 = 10
QUES 7. Take as input N, the size of a chess board. We are asked to place N number of Knights in it, so
that no knight can kill other.
a. Write a recursive function which returns the count of different distinct ways the knights can be placed
across the board. Print the value returned.
b.Write a recursive function which prints all valid configurations (void is the return type for function).
Input Format
Output Format
Display the number of ways a knight can be placed and print all the possible arrangements in a space
separated manner
Sample Input
2
Sample Output
{0-0} {0-1} {0-0} {1-0} {0-0} {1-1} {0-1} {1-0} {0-1} {1-1} {1-0} {1-1}
6
Sample Output
a
ab
b
Explanation
QUES 9. Take as input str, a string. Write a recursive function which returns all the words possible by
rearranging the characters of this string which are in dictionary order smaller than the given string. The
output strings must be lexicographically sorted.
Output Format
Display all the words which are in dictionary order smaller than the string entered in a new line each. The
output strings must be sorted.
Sample Input
cab
Sample Output
abc
acb
bac
bca
Explanation
The possible permutations of string "cab" are "abc" , "acb" ,"bac" , "bca" , "cab" and "cba" . Only four of them are
lexicographically less than "cab". We print them in lexicographical order.
QUES 10. Take as input str, a string. Write a recursive function which prints all the words possible by
rearranging the characters of this string which are in dictionary order larger than the given string.
The output strings must be lexicographically sorted.
Output Format
Display all the words which are in dictionary order larger than the string entered in a new line each. The
output strings must be sorted.
Sample Input
cab
Sample Output
cba
Explanation
The possible permutations of string "cab" are "abc" , "acb" ,"bac" , "bca" , "cab" and "cba" . Only one of them is
lexicographically greater than "cab". We only print "cba".
SERIES 10
QUES 1. Create a 2D array using DMA and delete the array as well.
QUES 2. Create a class STUDENT {
Private: int marks;
Int roll;
Char branch;
1. Constructor
2. Parametrized constructor
3. Copy constructor
4. Copy assignment operator
5. Destructor
6. Operator overloading: i.) +=add
ii.) > marks
7. Static member: total students
};
2. DMA in LL:
i. Insertion at FRONT
ii. Insertion at END
iii. Insertion at MID
iv. Count length of linked list
v. Deletion at front
vi. Deletion at end
vii. Deletion at mid
i. Iteratively
ii. Recursively