Tcs Coding
Tcs Coding
Sample Input:
eat
ate
Output:
Anagram
Case 1
Input (stdin)
eat
ate
Output (stdout)
Anagram
Case 2
Input (stdin)
program
prograd
Output (stdout)
Not anagrams
Output:
Output consists of three lines, First line prints "in-order", Second line prints"pre-
order" and third line print the output of "post-order" .
Sample Input:
5
58946
Ouptut:
45689
54869
46985
Case 1
Input (stdin)
5
5 8 9 4 6
Output (stdout)
4 5 6 8 9
5 4 8 6 9
4 6 9 8 5
Case 2
Input (stdin)
6
6 5 4 8 3 9
Output (stdout)
3 4 5 6 8 9
6 5 4 3 8 9
3 4 5 9 8 6
Input :
First line contains an integer 'N', i.e the no. of students in the class.
Next 'N' lines contains the names of the students.
Output:
Each line consists of the name of student space and separated its frequency.
Constraints:
1<=N<=1000
string length<=100
string consists of lowercase letters
Sample Input:
sumit
ambuj
himanshu
ambuj
ambuj
Sample Output:
ambuj 3
himanshu 1
sumit 1
Case 1
Input (stdin)
5
sumit
ambuj
himanshu
ambuj
ambuj
Output (stdout)
ambuj 3
himanshu 1
sumit 1
Case 2
Input (stdin)
6
raj
raj
dinesh
dinesh
tamil
tamil
Output (stdout)
dinesh 2
raj 2
tamil 2
Given a maximum of four digit to the base 17(10 -> A, 11 -> B, 12 -> C, 16 -> G) as
input, output its decimal value.
Input - 1
1A
Expected Output
27
Input - 2
23GF
Expected Output
10980
Case 1
Input (stdin)
1A
Output (stdout)
27
Case 2
Input (stdin)
23GF
Output (stdout)
10980
break, case, continue, default, defer, else, for, func, goto, if, map, range,
return, struct, type, var
Example-1
Input
defer
Expected Output
defer is a keyword
Example-2
Input
While
Expected Output
Case 1
Input (stdin)
defer
Output (stdout)
defer is a keyword
Case 2
Input (stdin)
while
Output (stdout)
while is not a keyword
It was one of the places, where people need to get their provisions only through fair
price (“ration”) shops. As the elder had domestic and official work to attend to, their
wards were asked to buy the items from these shops. Needless to say, there was a
long queue of boys and girls. To minimize the tedium of standing in the serpentine
queue, the kids were given mints. I went to the last boy in the queue and asked him
how many mints he has. He said that the number of mints he has is one less than
the sum of all the mints of kids standing before him in the queue. So I went to the
penultimate kid to know how many mints she has.
She said that if I add all the mints of kids before her and subtract one from it, the
result equals the mints she has. It seemed to be the uniform response from
everyone. So, I went to the boy in the head of queue consoling myself that he would
not give the same response as others. He said, “I have four mints”.
Given the number of first kid’s mints (n) and the length (len) of queue as input, write
a program to display the total number of mints with all the kids.
constraints:
2<n<10
1<len<20
Example-1
Input
42
Expected output:
Example-2
Input
14 4
Expected output
105
Case 1
Input (stdin)
4 2
Output (stdout)
7
Case 2
Input (stdin)
14 4
Output (stdout)
105
Given a pair of positive integers m and n (m < n; 0 < m < 999; 1 < n < = 999),
write a program to smarty affix zeroes, while printing the numbers from m to n.
Example-1
Input
5 10
Expected output
05 06 07 08 09 10
Example-2
Input
9 100
Expected output
009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027
028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046
047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065
067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085
086 087 088 089 090 091 092 093 094 095 096 097 098 099 100
Example-3
Input
19
Expected output
123456789
Case 1
Input (stdin)
1 9
Output (stdout)
1 2 3 4 5 6 7 8 9
Case 2
Input (stdin)
3 7
Output (stdout)
3 4 5 6 7
Our hoary culture had several great persons since time immemorial and king
vikramaditya’s nava ratnas (nine gems) belongs to this [Link] are named in the
following shloka:
Among these, Varahamihira was an astrologer of eminence and his book Brihat
Jataak is recokened as the ultimate authority in astrology.
He was once talking with Amarasimha,another gem among the nava ratnas and the
author of Sanskrit thesaurus, Amarakosha.
Amarasimha wanted to know the final position of a person, who starts from the origin
0 0 and travels per following scheme.
constraints:
2<=n<=1000
Input-1
Expected output:
-20 20
Input -2
Expected output:
-20 -20
Case 1
Input (stdin)
3
Output (stdout)
-20 20
Case 2
Input (stdin)
4
Output (stdout)
-20 -20
Input 1:
4567
Expected output:
Explanation
Sum of odd position digits 4 and 6 is 10. Sum of even position digits 5 and 7 is 12.
The difference is 12-10=2.
Input #2:
9834698765123
Expected output:
Input 3:
5476
Expected output:
Case 1
Input (stdin)
4567
Output (stdout)
2
Case 2
Input (stdin)
9834698765123
Output (stdout)
1
Program 10: segregate all the 0’s in left side and 1’s in
right side
Write a program to segregate all the 0’s in left side and 1’s in right side in the same
array with O(n) complexity.
Sample Input:
01010
Output:
00011
Case 1
Input (stdin)
5
0 1 0 1 0
Output (stdout)
00011
Case 2
Input (stdin)
7
0 0 0 1 1 0 0
Output (stdout)
0000011
Hint:
6. Exit
Sample Input:
20
Sample Output:
10 20
Case 1
Input (stdin)
1
10
2
20
3
4
5
6
Output (stdout)
Enter value of element
Enter value of element
10 20
10 deleted from beginning successfully.
20 deleted from end successfully.
Case 2
Input (stdin)
1
10
3
6
Output (stdout)
Enter value of element
10
Program 12: Find the Nth element
Write a program to create a Singly Linked List and find the Nth element from the end
of the list.
Output:
output consists of single line, print the element in the given position in a list, else
print"No node found"
Sample Input:
10 8 6 4 0
Output:
Case 1
Input (stdin)
5
10 8 6 4 0
2
Output (stdout)
8
Case 2
Input (stdin)
10
100 200 300 400 500 600 700 800 900 101
11
Output (stdout)
No node found
Note: Use long long for storing the numbers from the string.
Input:
The first line consists of T test cases and next T lines contain a string.
Output:
For each string output the number stored in that string if various numbers are there
print the largest one. If a string has no numbers print -1.
Constraints:
1<=T<=100
1<=|S|<=10000
Example:
Input:
Output:
5057
Case 1
Input (stdin)
1
This is alpha 5057 and 97
Output (stdout)
5057
Case 2
Input (stdin)
1
dream job 100 and 101
Output (stdout)
101
Program 14: Largest power of prime
Given a positive integer N and a prime p, the task is to print the largest power of
prime p that divides N!. Here N! means the factorial of N = 1 x 2 x 3 . . (N-1) x N.
Input:
The first line of input contains a single integer T denoting the number of test cases.
Then T test cases follow. Each test case consists of a single line containing a positive
integer N and a prime p.
Output:
Corresponding to each test case, in a new line, print the largest power of
prime p that divides N.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 100000
2 ≤ p ≤ 5000
Example:
Input:
62 7
76 2
35
Output:
73
Case 1
Input (stdin)
3
62 7
76 2
3 5
Output (stdout)
9
73
0
Case 2
Input (stdin)
1
1000 100
Output (stdout)
10
You have to find out the member who wins the game.
Input : The input consists of 2 lines. The first line consists of N, the member of family
in the class. The next line consists of a string denoting the lyrics of the song the
teacher plays.
Output :
Print a single integer denoting the roll number of the student who wins the game.
Constraints :
2≤N≤100000
1≤|S|≤10000, where |S| denotes the length of the input string. It is guaranteed that
at least 1 lyric in the song will be a 'y'
Note: Copy and paste is disabled for all the questions. Try not to use other ides.
Sample input:
xyx
Output:
2nd turn lyrics : 'y' therefore 2nd member gets out of game and passes to 3rd
Case 1
Input (stdin)
3
xyx
Output (stdout)
1
Case 2
Input (stdin)
500
xyxyyxxxxyyyyxxyyxyxyyxyxxyxxxxyxxxxyxyxyxxxyxyyyyyyyxxxxxxxyyyyyyxyxyxxxyyyyxxxxyx
yxxxxxxxxxyyyxyyxxyxxyxxyyxxyxyxyxyyxyyxxyyyxyyxyyxxxyxyxxxxxyyyyyyyyxxxyxxyxxxxyyx
xxyxxyyyxxyxyyyyxyxyxxxxxxxxyxxyxyxyxxxxyyxyyyyxxxxyyyyxxxyxxyxxyxxyyyxyyyyyxyxyyyx
xxxyxxyxxxyyyyxyyxyyxyyxyyxxxxxxxxyxyxxyxxxxxxxxyyxyyyyyxyxxxyyxxxyyyyyxyyyyyxxxxyy
xyxxyyyyyyyyyxxyyxxyyxyxxxxxyxyxyyyyxxyxxyyxyxxyxxxyxyyxyyxxyyyyyxxyxyyxxyxyyyxyxyy
xxxxyyxxxyyyyyyxyyxxyyxyyxxxxyyxyxyxyyxyyxxyyxyyyyyyxyxyxxyxyxxxyyyxxxyyxyyxyxxyyyx
xx
Output (stdout)
83
Output:
Output consists of single line, print the count of unique digits in given range. Else
Print"No Unique Number"
Sample Input:
10
15
Ouput:
Case 1
Input (stdin)
10
15
Output (stdout)
5
Case 2
Input (stdin)
10
20
Output (stdout)
10
Output:
Output consists of single line, print the count prime pairs in given range. Else
print"No Prime Pairs".
constraints:
2<=n<=1000
n<=m<=2000
Sample Input:
30
Output:
Explanation:
(5, 11) (7, 13) (11, 17) (13, 19) (17, 23) (23, 29) . we have 6 prime pairs.
Case 1
Input (stdin)
4
30
Output (stdout)
6
ase 2
Input (stdin)
10
100
Output (stdout)
13
sample Input:
55
Output:
70
Case 1
Input (stdin)
5
5
Output (stdout)
70
Case 2
Input (stdin)
10
10
Output (stdout)
48620
Input
The first line of input is an integer N. 2 <= N <= 10000 The second line is N integers
indicates the number of apples of the ith boy. Each integer is positive and no more
than 10^9.
Output
A single line contains the minimal number of steps to make each boy have the same
number of apples.
Sample Input:
1397
Output:
1. (2,3,9,6)
2. (3,3,9,5)
3. (3,4,8,5)
4. (3,5,7,5)
5. (3,6,6,5)
6. (3,7,5,5)
7. (4,6,5,5)
8. (5,5,5,5)
Case 1
Input (stdin)
4
1 3 9 7
Output (stdout)
8
Case 2
Input (stdin)
5
3 2 5 4 6
Output (stdout)
4
Other elements of the array are two less than the mean of the number preceding
and succeeding [Link] it is obvious, Riya felt that this idea was stupid and hence she
wanted to punish [Link] decided to ask Madhav the nth number of the array. If
he tells the wrong answer, she would slap [Link] Madhav to escape from this
embarrassing situation.
Input:
Output:
For each test case, output an integer which is the Nth number of the array. As the
answer can be very large, output it modulo 109+7
Constraints:
1 ≤ T ≤ 105
1 ≤ N ≤ 1018
Sample Input:
2
Output:
15
Explanation:
Case 1
Input (stdin)
2
1
3
Output (stdout)
1
15
Case 2
Input (stdin)
3
101
35
251
Output (stdout)
20301
2415
125751
1<=N<10^6
1<=T<=2*10^6
INPUT:
First line of input contains an Integer T denoting the number of test cases.
OUTPUT:
Sample Input:
51
12
65
Output:
53
11
67
Case 1
Input (stdin)
3
51
12
65
Output (stdout)
53
11
67
Case 2
Input (stdin)
2
452
526
Output (stdout)
449
523
Program 22: Palindromic Array
You are given an array A of size N. Your task is to find the minimum number of
operations needed to convert the given array to 'Palindromic Array'.
Palindromic Array:
The only allowed operation is that you can merge two adjacent elements in the array
and replace them with their sum.
Input:
The first line of input contains an integer T denoting the number of test [Link]
first line of each test case is N, where N is the size of [Link] second line of each
test case contains N space separated integers which is the input for the array.
Output:
Output the minimum number of operations required to make the given array a
palindromic array.
Constraints:
Example:
Input:
32335
5334
Output:
Explanation:
For Test Case 1: [3 2 3 3 5] after merging the 1st two elements 3 and 2, we get the
array as [5 3 3 5] which is a palindrome, hence only 1 operation is needed.
Case 1
Input (stdin)
2
5
3 2 3 3 5
4
5 3 3 4
Output (stdout)
1
3
Case 2
Input (stdin)
1
6
6 3 6 3 2 1
Output (stdout)
3
Constraints:
1<=T<=100
1<=N<=10^12
Input:
The first line of the input contains a single integer T denoting the number of
test cases.
The description of T test cases [Link] first and only line of each test case
contains a single integer N.
Output :
For each test case, print a single line containing the string "Yes" if you can make
exactly N rupees or "No" otherwise.
Sample Input:
10
25
200
Output:
Yes
No
Yes
No
Yes
Explanation:
In the last case hacker can get Rs. 200 by first using 10x hack and then using 20x
hack once.
Case 1
Input (stdin)
5
1
2
10
25
200
Output (stdout)
Yes
No
Yes
No
Yes
Case 2
Input (stdin)
3
101
52
124
Output (stdout)
No
No
No
Input:
The first line of input contains an integer T denoting the number of test
cases. T testcases follow. For each testcase there is one line of input that contains
the number N.
Output:
Constraints:
1 ≤ T ≤ 104
1 ≤ N ≤ 104
Example:
Input:
10
Output:
12
Case 1
Input (stdin)
2
10
4
Output (stdout)
12
4
Case 2
Input (stdin)
3
520
432
125
Output (stdout)
1166400
455625
3125
Input Format:
Second line contains the number of positions you have to shift the elements in the
string.
Output Format:
Print the rotated string
Constrain:
1<=len(A)<1000
Sample Input:
faceprep
Sample Output:
eprepfac
Case 1
Input (stdin)
faceprep
3
R
Output (stdout)
repfacep
Case 2
Input (stdin)
faceprep
3
R
Output (stdout)
Repfacep
Output:
Constraints:
Sample Input:
05:39
Sample Output:
11
Explanation:
It takes 11 minutes for minute value to become 50, 05:50 is a palindromic time.
Case 1
Case 2
Input (stdin)
05:39
Output (stdout)
11
Case 2
Input (stdin)
13:31
Output (stdout)
0
Input Format
Output Format
Constraints
1<=weight<=1016
Sample Input:
20
Sample Output:
AABBC
Case 1
Input (stdin)
20
Output (stdout)
AABBC
Input Format
Output Format
Sample Input:
{()}[]
Sample Output:
Balanced
Case 1
Case 2
Input (stdin)
{()}[]
Output (stdout)
Balanced
Case 2
Input (stdin)
{{}(})
Output (stdout)
Not Balanced
Sample Input:
-1
36
Sample Output:
6
Case 1
Input (stdin)
5 3 7 2 4 6 8 -1
2 4
Output (stdout)
3
Case 2
Input (stdin)
6 3 1 4 2 -1
3 6
Output (stdout)
6
Input Format
The input consists of a list of integers, negative value indicates the end of the linked
list.
Output Format
Sample Input
10
20
20
10
-30
Sample Output
20
10
10
20
Case 1
Input (stdin)
10
20
20
10
-30
Output (stdout)
20
10
10
20
Input Format
Output Format
Sample Input:
abc
Sample Output:
abc
acb
bac
bca
cab
cba
Case 1
Input (stdin)
abc
Output (stdout)
abc
acb
bac
bca
cab
cba
Input Format
One line containing two integers R and C representing the dimensions of the matrix
M as rows R, and columns C, respectively.
Output Format
Single line containing integers without space representing the desired traversal.
Constraints
0<R,C<500
Sample Input:
123
456
789
Sample Output:
123698745
Case 1
Input (stdin)
3
1 2 3
4 5 6
7 8 9
Output (stdout)
1 2 3 6 9 8 7 4 5
Case 2
Input (stdin)
10
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
Output (stdout)
1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 9 8 7 6 5 4 3 2 1 1 1 1 1 1 1 1 1 2 3 4 5 6 7
8 9 9 9 9 9 9 9 9 8 7 6 5 4 3 2 2 2 2 2 2 2 3 4 5 6 7 8 8 8 8 8 8 7 6 5 4 3 3 3 3 3
4 5 6 7 7 7 7 6 5 4 4 4 5 6 6 5
Input Format
Output Format
((a+b))
Sample Output:
Yes
Case 1
Input (stdin)
((a+b))
Output (stdout)
Yes
Case 2
Input (stdin)
(a+b*(c-d))
Output (stdout)
No
Of course anyone can memorise the letters (repeated many times) better than the
letters repeated few times, so Omar will concatenate all the words in the context he
has, and try to know the difficulty of each letter according to the number of
repetitions of each letter.
So Omar has now the whole context and wants to arrange the letters from the most
difficult letter (repeated few times) to the less difficult letter (repeated many times).
If there are 2 letters with the same level of difficulty, the letter with higher value of
ASCII code will be more difficult.
Input Format:
Output the English lower case characters from the most difficult letter to the less
difficult letter. (leave a space between 2 successive letters) (Output each test case in
a separate line).
Sample Input:
oomar
Sample Output:
zyxwvutsqpnlkjihgfedcbrmao
Case 1
Input (stdin)
1
oomar
Output (stdout)
z y x w v u t s q p n l k j i h g f e d c b r m a o
Case 2
Input (stdin)
1
thirdworldwar
Output (stdout)
z y x v u s q p n m k j g f e c b t o l i h a w d r
connected if they share a side i.e. if they are adjacent to each other.
Your task is, given the information about the field, print the size of the largest forest.
Size of a forest is the number of trees in it. See the sample case for clarity
INPUT:
First line contains the size of the matrix N.
OUTPUT:
CONSTRAINTS:
1<=N<=1000
Sample Input:
TTTWW
TWWTT
TWWTT
TWTTT
WWTTT
Sample Output:
10
Explanation:
The forest on the top left has 6 trees but the forest on the bottom right is bigger with
10 trees.
Case 1
Case 2
Input (stdin)
5
TTTWW
TWWTT
TWWTT
TWTTT
WWTTT
Output (stdout)
10
Case 2
Input (stdin)
6
TTTWWT
TWWTTT
TWWTTT
TWTTTW
WWTTTW
TWWTWW
Output (stdout)
14
Given an integer array 'A', find the length of its Longest Increasing Subsequence of a
sub-array of the given integer array where the elements are sorted in a monotonic
increasing order.
You need to fill in a function that takes two inputs - integer 'n' and an integer array
containing 'n' integers and returns the length of its LIS.
Input Specification:
Output Specification;
Example 1:
Input 1: 3
Input 2: {1, 3, 2}
Output: 2
Case 1
Input (stdin)
3
1 3 2
Output (stdout)
2
Case 2
Input (stdin)
9
10 22 9 33 21 50 41 60 80
Output (stdout)
6
The equal elements must appear in pairs in the array but there cannot be more than
two consecutive occurrences of an element.
For example :
232
1122233
22311
Sample Input:
22311
Sample Output:
3
Case 1
Input (stdin)
5
2 2 3 1 1
Output (stdout)
3
Case 2
Input (stdin)
11
2 2 3 3 2 2 4 4 3 1 1
Output (stdout)
3
Sample Input:
Sample Output:
Constraints:
1 <= n <= 40
Examples:
Input: n = 1
Output: 1
Output: 2
Input: n = 4
Output: 5
Case 1
Input (stdin)
5
Output (stdout)
8
Input (stdin)
39
Output (stdout)
102334155
Input:
The first line of input contains an integer T denoting the number of test cases. Then
T test cases follow. Each test case contains an integer n denoting the size of the
array. The next line contains n space separated integers forming the array. The last
line contains the sum.
Output:
Count all the subsets of given array with sum equal to given sum.
NOTE: Since result can be very large, print the value modulo 109+7.
Constraints:
1<=T<=100
1<=n<=103
1<=a[i]<=103
1<=sum<=103
Example:
Input:
2 3 5 6 8 10
10
12345
10
Output:
Explanation:
Case 1
Input (stdin)
2
6
2 3 5 6 8 10
10
5
1 2 3 4 5
10
Output (stdout)
3
3
Case 2
Input (stdin)
1
7
56 57 8 9 6 3 4
60
Output (stdout)
2
Input Format:
The first line contains the number of test cases T, T lines follow.
Each line then contains an integer N, the total number of people attended that
meeting.
Output Format:
Constraints:
Output:
Explanation:
Case 1
Input (stdin)
2
1
2
Output (stdout)
0
1
Case 2
Input (stdin)
3
100
101
102
Output (stdout)
4950
5050
5151
If they have 4 books and students, the possible exchanges are 9. Bi is the book of i-
th student and after the exchange, he should get a different book, other than Bi.
B2 B1 B4 B3
B2 B3 B4 B1
B2 B4 B1 B3
B3 B1 B4 B2
B3 B4 B1 B2
B3 B4 B2 B1
B4 B1 B2 B3
B4 B3 B1 B2
B4 B3 B2 B1
Find the number of possible exchanges, if the books are exchanged so that every
student will receive a different book.
Constraints
Input Format
Input contains one line with N, indicates the number of books and number of
students.
Output Format
Sample Input:
Sample Output:
9
Case 1
Input (stdin)
4
Output (stdout)
9
Case 2
Input (stdin)
10
Output (stdout)
1334961
Since he is pushing the pendulum to his right always. He wanted to store that
distance in the right extreme of the arrangement. And the pendulum will move
towards the extreme left at that time he want that value to be stored in the left most
extreme in the arrangement. And this continues till the pendulum stops. He is also
sure that the distance reached at that oscillation will always be lesser than the
previous oscillation towards that particular [Link] a program to arrange the
distance as instructed.
Sample Input:
13254
Sample Output:
42135
Explanation:
The maximum distance in the given data is 5 hence that is placed in the right most
end
The next maximum element is 4 which is placed in the left most end.
Again the pendulum oscillates towards right to cover a distance of 3 and this
continues.
Case 1
Input (stdin)
5
1 3 2 5 4
Output (stdout)
4 2 1 3 5
Case 2
Input (stdin)
10
100 231 1 487 232 91 80 50 30 10
Output (stdout)
232 100 80 30 1 10 50 91 231 487
Note:
1. Palindrome: A string is called a palindrome if you reverse the string yet the
order of letters remains the same. For example, MADAM.
2. Fake Palindrome: A string is called as a fake palindrome if any of its
permutations is a palindrome. For example, AAC is fake palindrome, but ACD
is not.
3. Sub-string: A sub-string is a contiguous sequence (non-empty) of characters
within a string.
4. Two sub-strings are considered same if their starting indices and ending
indices are equal.
Input Format:
Output Format:
Constraints:
ABAB
Sample Output 1:
Explanation:
The fake palindrome for the string ABAB are A, B, A, B, ABA, BAB, ABAB.
Sample Input 2:
AAA
Sample output 2:
Explanation:
The fake palindrome for the string AAA are A, A, A, AA, AA, AAA
Case 1
Case 2
Input (stdin)
ABAB
Output (stdout)
7
Case 2
Input (stdin)
AAA
Output (stdout)
6
You have been provided with a strict instruction that you should not use atoi()
function in this problem. Usage of this function will refrain you from any upcoming
scholarships.
Input Format:
Output Format:
Should be an Integer.
Sample Input:
12
Sample output:
28
Explanation:
2+3+5+7+11 = 28
Case 1
Input (stdin)
12
Output (stdout)
28
Case 2
Input (stdin)
123456K
Output (stdout)
0
After this, James calls his son and tells him all about the trip, and the list of cities. He
gives him a task and asks him, the number of ways the city/cities can be chosen
from the list, if the total number of cities written on the list is n. The cities are
numbered from 1 to n. At least 1 city has to be selected.
He now asks your help to find the answer for the question that was asked to him.
INPUT:
OUTPUT:
Output contains t lines, each line contains the answer for that test case. As the
answer can be large, print the answer modulo 10^9+7
CONSTRAINTS:
1<=t<=100000
1<=n<=1012
SAMPLE INPUT:
Output:
Explanation:
For test case 1: The only ways to select the cities is [1], [2], [1, 2]. Therefore the
answer is 3.
For test case 2: The only way to select a city is [1] Therefore the answer is 1.
Case 1
Input (stdin)
2
2
1
Output (stdout)
3
1
Case 2
Input (stdin)
5
5
4
6
3
2
Output (stdout)
31
15
63
7
3
Initially you are given the number 0. After each day the number doubles itself. At
any day, you can add the number 1 any number of times during the day.
You are given a number N and you need to tell the minimum number of times you
have to add 1 to get N at any point of time.
Input Format:
The first line contains a single integer T denoting the number of test cases.
Constraints:
1≤T≤100
1 ≤N ≤109
Sample Input:
Output:
Explanation:
For 8, you need to add 1 only once at the starting. It will double to 2, then to 4, then
to 8.
For 5, you need to add 1 at the beginning, it will double to 2, then to 4, then you will
have to add 1 again to make it 5. So, answer is 2.
Case 1
Input (stdin)
4
4
8
7
5
Output (stdout)
1
1
3
2
Case 2
Input (stdin)
2
9
10
Output (stdout)
2
2
Given a string x (1<= len(x)<=1000), find the length of its longest palindromic
subsequence.
Write a program that takes in input as String x and returns the length of the longest
palindromic subsequence of x.
Input Specification:
Output Specification:
Example1:
input1: ababa
Output: 5
Explanation:
Case 1
Input (stdin)
ababa
Output (stdout)
5
Case 2
Input (stdin)
Drivingracecar
Output (stdout)
7
Input:
The first line of input contains an integer T denoting the number of test cases. Then
T test cases follow. Each test case contains an integer n denoting the size of the
array. The next line contains n space separated integers forming the array. The last
line contains the sum.
Output:
Count all the subsets of given array with sum equal to given sum.
NOTE: Since result can be very large, print the value modulo 109+7.
Constraints:
1<=T<=100
1<=n<=103
1<=a[i]<=103
1<=sum<=103
Example:
Input:
2
2 3 5 6 8 10
10
12345
10
Output:
Explanation:
Case 1
Case 2
Input (stdin)
2
6
2 3 5 6 8 10
10
5
1 2 3 4 5
10
Output (stdout)
3
3
Case 2
Input (stdin)
1
7
56 57 8 9 6 3 4
60
Output (stdout)
2
Input Format
The first line contains an integer N denoting the number of elements in costs.
Output Format
Constraints
1<=N<105
1<costs[i]<=109
1<=energies[i]<=109
1<=C<=109
Sample Input:
3
1
Sample Output:
17
Explanation:
N=3. Costs= [5,3,1]. Energies= [7,8,9]. Available money = C = 4. We can eat last
two sweets fully (1kg each) to gain 17 energy.
Case 1
Input (stdin)
3
5
3
1
7
8
9
4
Output (stdout)
17
Format:
Input:
The first line of input contains an integer T denoting the number of test cases.
Then T test cases follow. Each test case contains an integer N denoting the size of
the array. The next line contains N space-separated integers forming the array.
Output:
Print the modified array which contains all sorted even placed numbers followed by
reverse sorted odd placed numbers.
Constraints:
Example:
Input:
01234567
3 1 2 4 5 9 13 14 12
Output:
02467531
2 3 5 12 13 14 9 4 1
Case 1
Input (stdin)
2
8
0 1 2 3 4 5 6 7
9
3 1 2 4 5 9 13 14 12
Output (stdout)
0 2 4 6 7 5 3 1
2 3 5 12 13 14 9 4 1
Case 2
Input (stdin)
4
8
421 152 141 175 263 340 215 147
6
52 47 852 41 21 58
5
789 456 123 258 147
2
524 963
Output (stdout)
141 215 263 421 340 175 152 147
21 52 852 58 47 41
123 147 789 456 258
524 963
In the maze matrix, 0 means the block is the dead end and non-zero number means
the block can be used in the path from source to destination. The non-zero value of
mat[i][j] indicates number of maximum jumps rat can make from cell mat[i][j].
Input:
The first line of input contains an integer T denoting the number of test cases. For
each test case, the first line contains an integer n denoting the size of the square
matrix followed by N*N space-separated values of the matrix m where 0's represents
blocked paths and any number represents valid paths.
Output:
For each test case, the output is a matrix containing 1 for the path taken and 0 for
not chosen path. If no path exists print -1.
Constraints:
1 <= T <= 50
2 <= n <= 10
Input
2100
3001
0101
0001
2100
2001
0101
0001
Output:
1000
1001
0001
0001
-1
Explanation:
Testcase 1: Rat started with m[0][0] and can jump up to 2 steps right/down. First
check m[0][1] as it is 1, next check m[0][2], this won't lead to the solution. Then
check m[1][0], as this is 3(non-zero), so we can make 3 jumps to reach m[1][3].
From m[1][3] we can move downwards taking 1 jump each time to reach destination
at m[3][3].
Case 1
Case 2
Input (stdin)
2
4
2 1 0 0
3 0 0 1
0 1 0 1
0 0 0 1
4
2 1 0 0
2 0 0 1
0 1 0 1
0 0 0 1
Output (stdout)
1 0 0 0
1 0 0 1
0 0 0 1
0 0 0 1
-1
Case 2
Input (stdin)
1
4
2 1 0 0
3 0 0 1
0 1 0 1
3 0 0 1
Output (stdout)
1 0 0 0
1 0 0 0
0 0 0 0
1 0 0 1
Sample Input:
-1
Sample Output:
Case 1
Input (stdin)
1
2
3
4
5
-1
Output (stdout)
Diameter of the given binary tree is 4
Case 2
Input (stdin)
1
2
3
-1
Output (stdout)
Diameter of the given binary tree is 3
Input Format
Given input is in the form of string containing the content of the document.
Output Format
Sample Input:
Sample Output:
2
Explanation:
Case 1
Input (stdin)
UN was established on 24-10-1945. India got freedom on 15-08-1947.
Output (stdout)
2
Input (stdin)
The company was founded on 2-04-2017 and By this year 2-04-2020 it would be 2
years. I have Joined the company on 07-05-2017.
Output (stdout)
2
Input Format
Output Format
Constraints
1<=weight<=1016
20
Sample Output:
AABBC
Case 1
Input (stdin)
20
Output (stdout)
AABBC
Input Format
Output Format
Sample Input:
10
5146784379
Sample Output:
13
Case 1
Input (stdin)
10
5 1 4 6 7 8 4 3 7 9
Output (stdout)
13
Input Format
The input consists of a list of integers, negative value indicates the end of the linked
list.
Output Format
Sample Input
11
22
33
22
33
44
-77
Sample Output
11
22
33
44
Case 1
Case 2
Input (stdin)
11
22
33
22
33
44
-77
Output (stdout)
11
22
33
44
Case 2
Input (stdin)
-5
Output (stdout)
List is empty
Input:
The first line of input contains the number of test cases T. For each test case, the
first line contains the length of the linked list and the next line contains the elements
of the linked list.
Output:
For each test case, print the reversed linked list in a new line.
User Task:
The task is to complete the function reverseList() with head reference as the only
argument and should return a new head after reversing the list.
Constraints:
Example:
Input:
123456
2 7 8 9 10
Output:
654321
10 9 8 7 2
Explanation:
Case 1
Input (stdin)
2
6
1 2 3 4 5 6
5
2 7 8 9 10
Output (stdout)
6 5 4 3 2 1
10 9 8 7 2
Case 2
Input (stdin)
3
3
1 12 3
4
17 18 19 10
6
25 45 23 60 40 55
Output (stdout)
3 12 1
10 19 18 17
55 40 60 23 45 25