Professional Documents
Culture Documents
Write a program to find the whether the given string is the anagram of the first string.
Sample Input:
eat
ate
Output:
Anagram
Sample Input 0
eat
ate
Sample Output 0
Anagram
Sample Input 1
program
prograd
Sample Output 1
Not anagrams
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
Sample Input 0
5
01010
Sample Output 0
00011
Sample Input 1
7
0001100
Sample Output 1
0000011
Write a program to create a Singly Linked List using dynamic memory allocation. If linked list
is empty, print "Linked list is empty".
Hint:
Exit
Sample Input:
10
2
20
3
4
5
6
Sample Output:
10 20
1
10
2
20
3
4
5
6
Sample Output 0
Sample Input 1
1
10
3
6
Sample Output 1
Write a program to create a Singly Linked List and find the Nth element from the end of the list.
Sample Input:
5
10 8 6 4 0
2
Output:
Output Format
Output:
output consists of single line, print the element in the given position in a list, else print"No node
found"
Sample Input 0
5
10 8 6 4 0
2
Sample Output 0
Sample Input 1
10
100 200 300 400 500 600 700 800 900 101
11
Sample Output 1
No node found
Bastin once had trouble finding the numbers in a string. The numbers are distributed in a string
across various test cases.
There are various numbers in each test case you need to find the number in each test case. Each
test case has various numbers in sequence. You need to find only those numbers which do not
contain 9. For eg, if the string contains "hello this is alpha 5051 and 9475". You will extract 5051
and not 9475. You need only those numbers which are consecutive and you need to help him
find the numbers. Print the largest number.
Note: Use long long for storing the numbers from the string.
Example:
Input:
1
This is alpha 5057 and 97
Output:
5057
Input Format
Input:
The first line consists of T test cases and next T lines contain a string.
Constraints:
1<=T<=100
1<=|S|<=10000
Output Format
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.
1
This is alpha 5057 and 97
Sample Output 0
5057
Sample Input 1
1
dream job 100 and 101
Sample Output 1
101
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.
Example:
Input:
3
62 7
76 2
35
Output:
9
73
0
Input Format
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.
Constraints
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 100000
2 ≤ p ≤ 5000
Output Format
Output:
Corresponding to each test case, in a new line, print the largest power of prime p that divides
N.
Sample Input 0
3
62 7
76 2
35
9
73
0
Sample Input 1
1
1000 100
Sample Output 1
10
In this lockdown a family of N members decided to play a game the rules of which are:
All N members are made to sit uniformly in a circle (ie. from 1 to N in clockwise direction). The
game start with the person sitting at first position. A song is played in the background. The
lyrics of the song are denoted by a string which consists of only letters 'x' and 'y'. Assume that
each lyric of the song is a single letter. If the lyric 'x' occurs in the song, the member who is
currently holding the Parcel passes it on to the next member. This passing takes place in
clockwise direction. If the lyric 'y' occurs in the song, the member who is currently holding the
Parcel loses his/her chances of winning the game. He/she hands over the parcel to the next
member (in clockwise direction) and moves out. The game continues until a single member
survives in the end. He/she will be the winner of the game. Note that the song repeats
continuously ie. while the game is going on, if at all the song ends, the stereo system will
automatically start playing the song from the start without any delay. You have to find out the
member who wins the game
Sample input:
3
xyx
Output:
Explanation: Starting from 1 lyrics : 'x' therefore he passes the ballto 2nd
2nd turn lyrics : 'y' therefore 2nd member gets out of game and passes to 3rd
Input Format
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.
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'
Output Format
Output :
Print a single integer denoting the roll number of the student who wins the game.
Sample Input 0
3
xyx
Sample Output 0
Sample Input 1
500
xyxyyxxxxyyyyxxyyxyxyyxyxxyxxxxyxxxxyxyxyxxxyxyyyyyyyxxxxxxxyyyyyyxyxyxxxyyyyxxxx
yxyxxxxxxxxxyyyxyyxxyxxyxxyyxxyxyxyxyyxyyxxyyyxyyxyyxxxyxyxxxxxyyyyyyyyxxxyxxyxxx
xyyxxxyxxyyyxxyxyyyyxyxyxxxxxxxxyxxyxyxyxxxxyyxyyyyxxxxyyyyxxxyxxyxxyxxyyyxyyyyyx
yxyyyxxxxyxxyxxxyyyyxyyxyyxyyxyyxxxxxxxxyxyxxyxxxxxxxxyyxyyyyyxyxxxyyxxxyyyyyxyyy
yyxxxxyyxyxxyyyyyyyyyxxyyxxyyxyxxxxxyxyxyyyyxxyxxyyxyxxyxxxyxyyxyyxxyyyyyxxyxyyxx
yxyyyxyxyyxxxxyyxxxyyyyyyxyyxxyyxyyxxxxyyxyxyxyyxyyxxyyxyyyyyyxyxyxxyxyxxxyyyxxxy
yxyyxyxxyyyxxx
Sample Output 1
83
Write a program to find the count of numbers which consists of unique digits.
Sample Input:
10
15
Ouput:
Input Format
Input:
Output Format
Output:
Output consists of single line, print the count of unique digits in given range. Else Print"No
Unique Number"
Sample Input 0
10
15
Sample Output 0
Sample Input 1
10
20
Sample Output 1
10
There is a range given n and m in which we have to find the count all the prime pairs whose
difference is 6. We have to find how many sets are there within a given range.
Sample Input:
4
30
Output:
Explanation:
(5, 11) (7, 13) (11, 17) (13, 19) (17, 23) (23, 29) . we have 6 prime pairs.
Constraints
Constraints:
2<=n<=1000
n<=m<=2000
Output Format
Output:
Output consists of single line, print the count prime pairs in given range. Else print"No Prime
Pairs".
Sample Input 0
4
30
Sample Output 0
Sample Input 1
10
100
Sample Output 1
13
A Robot wants to move through a cave grid of size M x N. (M- Rows N- Columns). It starts from
(0,0) and destination is (M-1,N-1). It can only move right or down. Calculate the total number
of ways robot can reach the destination.
Sample Input:
55
Output:
70
Sample Input 0
5
5
Sample Output 0
70
Sample Input 1
10
10
Sample Output 1
48620
N boys are sitting in a circle. Each of them have some apples in their hand. You find that the
total number of the apples can be divided by N. So you want to divide the apples equally among
all the boys. But they are so lazy that each one of them only wants to give one apple to one of
the neighbors at one step. Calculate the minimal number of steps to make each boy have the
same number of apples.
Sample Input:
4
1397
Output:
Input Format
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 Format
Output
A single line contains the minimal number of steps to make each boy have the same number of
apples.
Sample Input 0
4
1397
Sample Output 0
Sample Input 1
5
32546
Madhav went to Riya's Birthday Party. He was a geek so he had no idea regarding which gift
she'l like. So he took an array of integers with him. The array followed a particular order. First
element of array is 1. Second element of array is 6.
Other elements of the array are two less than the mean of the number preceding and succeeding
it. As it is obvious, Riya felt that this idea was stupid and hence she wanted to punish
Madhav.She decided to ask Madhav the nth number of the array. If he tells the wrong answer,
she would slap him. Help Madhav to escape from this embarrassing situation.
Sample Input:
2
1
3
Output:
1
15
Explanation:
First test case is trivial as a [1] = 1. In second test case, a[2]=(a[1]+a[3])/2-2. Substituting the
values of a [1] and a[2] , we get: 6=(1+a[2])/2-2. So, a[2]=8*2-1=15
Sample Input 0
2
1
3
Sample Output 0
1
15
Sample Input 1
3
101
35
251
Sample Output 1
20301
2415
125751
Write a program to create and implement a Tree structure. Use DFS to display the elements (in-
order, pre-order, post-order).
Sample Input:
5
58946
Ouptut:
45689
54869
46985
Output Format
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 0
5
58946
Sample Output 0
45689
54869
46985
Sample Input 1
6
654839
Sample Output 1
345689
654389
345986
Joy is a hacker at hackerclub and he got a new problem on prime numbers.The problem states
that given an integer N find the nearest prime number to N.If multiple answer is possible then
output the smallest one of them.There are T number of test cases.
1<=N<10^6
1<=T<=2*10^6
Sample Input:
3
51
12
65
Output:
53
11
67
Input Format
INPUT:
First line of input contains an Integer T denoting the number of test cases.
Output Format
OUTPUT:
Sample Input 0
3
51
12
65
Sample Output 0
53
11
67
2
452
526
Sample Output 1
449
523
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.
Example:
Input:
2
5
32335
4
5334
Output:
1
3
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.
Input Format
Input:
The first line of input contains an integer T denoting the number of test cases.The first line of
each test case is N, where N is the size of array.The second line of each test case contains N space
separated integers which is the input for the array.
Constraints
Constraints:
Output:
Output the minimum number of operations required to make the given array a palindromic
array.
Sample Input 0
2
5
32335
4
5334
Sample Output 0
1
3
Sample Input 1
1
6
636321
Sample Output 1
You are a bank account hacker.I nitially you have 1 rupee in your account, and you want exactly
N rupees in your account.You wrote two hacks, First hack can multiply the amount of money
you own by 10, while the second can multiply it by 20. These hacks can be used any number of
time . Can you achieve the desired amount N using these hacks.
Sample Input:
5
1
2
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.
Input Format
Input:
The first line of the input contains a single integer T denoting the number of test cases. The
description of T test cases follows. The first and only line of each test case contains a single
integer N.
Constraints
Constraints:
1<=T<=100
1<=N<=10^12
Output Format
Output :
For each test case, print a single line containing the string "Yes" if you can make exactly N rupees
or "No" otherwise.
5
1
2
10
25
200
Sample Output 0
Yes
No
Yes
No
Yes
Sample Input 1
3
101
52
124
Sample Output 1
No
No
No
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6,
8, 9, 10, 12, 15, … shows the first 11 ugly numbers. By convention, 1 is included. Write a program
to find Nth Ugly Number.
Example:
Input:
2
10
4
Output:
12
4
Input Format
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.
Constraints
Constraints:
1 ≤ T ≤ 10^4
1 ≤ N ≤ 10^4
Output Format
Output:
Sample Input 0
2
10
4
Sample Output 0
12
4
3
520
432
125
Sample Output 1
1166400
455625
3125
Write a program to Rotate the string in the specified direction and print the output.
Sample Input:
faceprep
3
L
Sample Output:
eprepfac
Input Format
Input Format:
Second line contains the number of positions you have to shift the elements in the string.
Constraints
Constraints:
1<=len(A)<1000
Output Format
Output Format:
Sample Input 0
faceprep
3
R
Sample Output 0
repfacep
welcome
5
L
Sample Output 1
Mewelco
Dinesh is fond of video games. Due to the pandemic, he designs a video game called the Corona
world. In this game, the player enters the game with a certain energy. The player should defeat
all the corona infected zombies to reach the next level. When time increases the zombies will
increase double the previous minute. Anyhow the player can manage to fight against all the
zombies. In this case, definitely the player can not achieve the promotion. Hence the player gets
a superpower to destroy all the zombies in the current level when the current game time is a
palindrome. Anyhow the player can manage only if he knows the time taken to get the
superpower. Help the player by providing the minimum minutes needed to get the superpower
by which he can destroy all the zombies. You will be provided with the starting time of the game.
Sample Input:
05:39
Sample Output:
11
Explanation:
It takes 11 minutes for minute value to become 50, 05:50 is a palindromic time.
Input Format
Input Format:
Constraints
Constraints:
Output Format
Output:
Sample Input 0
05:39
Sample Output 0
11
13:31
Sample Output 1
Every character has a weight. The weight of an English uppercase alphabet A-Z is given below.
A=1 B=2*A+A C=3*B+B D=4*C+C . . . Y=25*X+X Z=26*Y+Y The weight of any string made up of
these characters is the summation of weights of each character. Given a total string weight,
determine shortest string of that given weight. If there is more than one solution, return the
lexicographically smallest of them. For example, given weight = 25, and the weights of the first
few characters of the alphabet are A=1, B=3, C=12, D=60 it is certain that no letter larger than
C is required. Some of the strings with a total weight equal to the largest are ABBBBC, ACC, and
AAAAAAABBBBBB. The shortest of these is ACC. While any permutation of these characters will
have the same weight, this is the lexicographically smallest of them. Function Description
Complete the function smallestString in the editor below. The function must return the shortest
string of the target weight. If there are multiple answers, return the lexicographically smallest
of them. smallestString has the following parameter(s): weight: a long integer that denotes the
target weight.
Sample Input:
20
Sample Output:
AABBC
Input Format
Input Format
Constraints
Constraints
1<=weight<=10^16
Output Format
Output Format
Sample Input 0
20
Sample Output 0
AABBC
16
Sample Output 1
ABC
Sample Input:
{()}[]
Sample Output:
Balanced
Input Format
Input Format
Output Format
Output Format
Sample Input 0
{()}[]
Sample Output 0
Balanced
Sample Input 1
{{}(})
Sample Output 1
Not Balanced
Write a C++ program to find the common ancestor of given two numbers in a tree. Let T be a
rooted tree. The lowest common ancestor between two nodes n1 and n2 is defined as the lowest
node in T that has both n1 and n2 as descendants (where we allow a node to be descendant of
itself). The LCA of n1 and n2 in T is the shared ancestor of n1 and n2 that is located farthest
from the root. Computation of Lowest common ancestor is useful, for instance, as part of a
procedure for determining the distance between pairs of node in a tree: the distance from n1
to n2 can be computed as the distance from the root to n1, plus the distance from from the root
to n2, minus twice the distance from the root to their lowest common ancestor.
Sample Input:
6
3
1
4
2
-1
36
Sample Output:
Sample Input 0
5 3 7 2 4 6 8 -1
24
Sample Output 0
Sample Input 1
6 3 1 4 2 -1
36
Sample Output 1
Given a linked list, write a program to swap elements pairwise. If the list is empty, print "List is
empty".
Sample Input
10
20
20
10
-30
Sample Output
20
10
10
20
Input Format
Input Format
The input consists of a list of integers, negative value indicates the end of the linked list.
Output Format
Output Format
Sample Input 0
10
20
20
10
-30
Sample Output 0
20
10
10
20
-2
Sample Output 1
List is empty
"I'm not a fan of having kids memorize formulas, and I'm even less of a fan of pushing them to
learn those formulas," says Mr. John, 7th grade Math teacher. Maybe he has got a point. He
believes in teaching the logic rather than the formula. Anyway, we aren't here to weigh/debate
about his opinions. All we gotta do is help Mr. John and his students by writing an algorithm
that can calculate & print all the permutations of a given number in strictly sorted order.
Remember, Mr. John is gonna use your algorithm to demonstrate permutations in his next class.
Sample Input:
abc
Sample Output:
abc
acb
bac
bca
cab
cba
Input Format
Input Format
Output Format
Output Format
Sample Input 0
abcd
Sample Output 0
abcd
abdc
acbd
acdb
adbc
adcb
bacd
badc
bcad
bcda
Sample Input:
3
123
456
789
Sample Output:
123698745
Input Format
Input Format
One line containing two integers R and C representing the dimensions of the matrix M as rows
R, and columns C, respectively.
R lines, each containing C space separated number of integers which collectively form the
matrix data.
Constraints
Constraints
Output Format
Output Format
Single line containing integers without space representing the desired traversal.
Sample Input 0
3
123
456
789
Sample Output 0
123698745
10
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
Sample Output 1
1234567890000000000987654321111111112345678999999998
765432222222345678888887654333334567777654445665
You are the class leader. The teacher asked you to write down the names of students on the
board who are talking. Also, she asked to write the student's name as many times they talk in
the class (the same student shall be present on the board more than once). Finally, the teacher
decided to give imposition based on the number of times a student's name got repeated. For
example, If Suresh's name is repeated 4 times he needs to write and submit the assignment for
4 times). The list of students is given as your input. Your output should be arranged in
lexicographical order.
Sample Input:
5
sumit
ambuj
himanshu
ambuj
ambuj
Sample Output:
ambuj 3
himanshu 1
sumit 1
Input Format
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.
Constraints
Constraints:
Output Format
Output:
Each line consists of the name of student space and separated its frequency.
5
sumit
ambuj
himanshu
ambuj
ambuj
Sample Output 0
ambuj 3
himanshu 1
sumit 1
Sample Input 1
6
raj
raj
dinesh
dinesh
tamil
tamil
Sample Output 1
dinesh 2
raj 2
tamil 2
Given a string of balanced expression, find if it contains redundant parentheses or not. A set of
parentheses is redundant if the same sub-expression is surrounded by unnecessary or multiple
brackets. Print ‘Yes’ if redundant else ‘No’.
Sample Input:
((a+b))
Sample Output:
Yes
Input Format
Input Format
Output Format
Output Format
Sample Input 0
((a+b))
Sample Output 0
Yes
Sample Input 1
(a+b*(c-d))
Sample Output 1
No
Yesterday while Omar was trying to learn English, he saw that there are letters repeated many
times in words while some other letters repeated only few times or not repeated at all!
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.
Sample Input:
1
oomar
Sample Output:
zyxwvutsqpnlkjihgfedcbrmao
Input Format
Input Format:
Output Format
Output 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 0
1
oomar
Sample Output 0
zyxwvutsqpnlkjihgfedcbrmao
1
thirdworldwar
Sample Output 1
zyxvusqpnmkjgfecbtolihawdr
Imagine the field is a 2D plane. Each cell is either water 'W' or a tree 'T'.
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
Sample Input:
5
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.
Input Format
Input:
Constraints
Constraints:
1<=N<=1000
Output Format
Output:
5
TTTWW
TWWTT
TWWTT
TWTTT
WWTTT
Sample Output 0
10
Sample Input 1
6
TTTWWT
TWWTTT
TWWTTT
TWTTTW
WWTTTW
TWWTWW
Sample Output 1
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.
Example 1:
Input 1: 3
Input 2: {1, 3, 2}
Output: 2
Input Format
Input Specification:
Output Format
Output Specification;
Sample Input 0
3
132
Sample Output 0
Sample Input 1
9
10 22 9 33 21 50 41 60 80
Sample Output 1
Given an array of integers where every element appears even number of times except one
element which appears odd number of times, write a program to find that odd occurring
element in O(log n) time.
The equal elements must appear in pairs in the array but there cannot be more than two
consecutive occurrences of an element.
For example:
3
232
7
1122233
5
22311
Sample Input:
5
22311
Sample Output:
Sample Input 0
5
22311
Sample Output 0
11
22332244311
Sample Output 1
A naughty kid is climbing a staircase of 'n' steps. He can take either 1 step or 2 steps at a time.
Write a program to find the number of distinct ways to reach the nth step. Assume he is standing
on the 0th step.
Sample Input:
Sample Output:
Examples:
Input: n = 1
Output: 1
Input: n = 2
Output: 2
Input: n = 4
Output: 5
Explanation: (1, 1, 1, 1), (1, 1, 2), (2, 1, 1), (1, 2, 1), (2, 2)
Constraints
Constraints:
1 <= n <= 40
Sample Input 0
Sample Output 0
39
Sample Output 1
102334155
Given an array of integers and a sum, the task is to count all subsets of given array with sum
equal to given sum.
NOTE: Since result can be very large, print the value modulo 109+7.
Example:
Input:
2
6
2 3 5 6 8 10
10
5
12345
10
Output:
3
3
Explanation:
Input 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. The last line contains the sum.
NOTE: Since result can be very large, print the value modulo 109+7.
Constraints
Constraints:
1<=T<=100
1<=n<=103
1<=a[i]<=103
1<=sum<=103
Output:
Count all the subsets of given array with sum equal to given sum.
Sample Input 0
2
6
2 3 5 6 8 10
10
5
12345
10
Sample Output 0
3
3
Sample Input 1
1
7
56 57 8 9 6 3 4
60
Sample Output 1
Before the outbreak of corona virus to the world, a meeting happened in a room in Wuhan. A
person who attended that meeting had COVID-19 and no one in the room knew about it! So
everyone started shaking hands with everyone else in the room as a gesture of respect and after
meeting unfortunately every one got infected! Given the fact that any two persons shake hand
exactly once, Can you tell the total count of handshakes happened in that meeting?
Sample Input:
2
1
2
Output:
0
1
Explanation:
Input Format
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.
Constraints
Constraints:
Output Format
Output Format:
2
1
2
Sample Output 0
0
1
Sample Input 1
3
100
101
102
Sample Output 1
4950
5050
5151
For enhancing the book reading, school distributed story books to students as part of the
Children’s day celebrations. To increase the reading habit, the class teacher decided to exchange
the books every weeks so that everyone will have a different book to read. She wants to know
how many possible exchanges are possible.
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.
Sample Input:
Sample Output:
Input Format
Input Format
Input contains one line with N, indicates the number of books and number of students.
Constraints
Output Format
Output Format
Sample Input 0
Sample Output 0
Sample Input 1
10
Sample Output 1
1334961
Bob is doing a research in Pendulum. He is just pushing the pendulum aside and the pendulum
started moving in to-and-fro motion. Bob will push the pendulum always towards his right side
to start the oscillation. Bob wanted to calculate the distance between extreme position and the
centre position of pendulum for each oscillations. He somehow calculated all the possible
distance. Since he is busy in this research he is giving the task to his assistant who needs to
arrange the values as instructed.
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 end.Write a program
to arrange the distance as instructed.
Sample Input:
5
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.
Sample Input 0
5
13254
Sample Output 0
42135
Sample Input 1
10
100 231 1 487 232 91 80 50 30 10
Sample Output 1
You are given a string A and you have to find the number of different sub-strings of the string A
which are fake palindromes.
Note:
Palindrome: A string is called a palindrome if you reverse the string yet the order of letters
remains the same. For example, MADAM.
Two sub-strings are considered same if their starting indices and ending indices are equal.
Sample Input 1:
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
Input Format
Input Format:
Constraints:
Output Format
Output Format:
Sample Input 0
ABAB
Sample Output 0
Sample Input 1
AAA
Sample Output 1
John and Thor wanted to test their students learning. Hence they come up with a problem. The
problem consists of a string containing an integer. You should convert the string to its
equivalent integer and then find the sum of prime number. There can be negative number also
hence you need to multiply the negative value by 11. In case of invalid string print the output
as 0.
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.
Sample Input:
12
Sample output:
28
Explanation:
2+3+5+7+11 = 28
Input Format
Input Format:
Output Format
Output Format:
Should be an Integer.
Sample Input 0
12
Sample Output 0
28
Sample Input 1
123456K
Sample Output 1
James has decided to take a break from his work. He makes a plan to visit India for a few days
with his family. He knows a lot about India, and also about the various cities he could visit. He
decides to write down all the cities on a paper. Let the number of cities be n. Now, he shows the
list to his wife and asks her, the city/cities she wants to visit. He tells her that she can select any
city/cities and any number of cities. The wife says, she needs a day to decide.
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.
SAMPLE INPUT:
2
2
1
Output:
3
1
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.
Input Format
Input:
Constraints
Constraints:
1<=t<=100000
1<=n<=1012
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
Sample Input 0
2
2
1
Sample Output 0
3
1
Sample Input 1
5
5
4
6
3
2
Sample Output 1
31
15
63
7
3
After performing a lot of experiments at the SEGP and EEDC Lab, you need a break. You see your
roommate stuck at a math problem. He asks for your help and says he will give you party at the
NC if you solve this problem for him.
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.
Sample Input:
4
4
8
7
5
Output:
1
1
3
2
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.
Input Format
Input Format:
The first line contains a single integer T denoting the number of test cases.
Constraints
Constraints:
1≤T≤100
1 ≤N ≤10^9
Output Format:
Sample Input 0
4
4
8
7
5
Sample Output 0
1
1
3
2
Sample Input 1
2
9
10
Sample Output 1
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.
Example1:
input1:
ababa
Output:
Explanation:
Input Format
Input Specification:
input1:
string input
Output Format
Output Specification:
Sample Input 0
ababa
Sample Output 0
Sample Input 1
Drivingracecar
Sample Output 1
Given an array of integers and a sum, the task is to count all subsets of given array with sum
equal to given sum.
NOTE: Since result can be very large, print the value modulo 109+7.
Example:
Input:
2
6
2 3 5 6 8 10
10
5
12345
10
Output:
3
3
Explanation:
Input 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. The last line contains the sum.
Constraints
Constraints:
1<=T<=100
1<=n<=10^3
1<=a[i]<=10^3
1<=sum<=10^3
Output:
Count all the subsets of given array with sum equal to given sum.
Sample Input 0
2
6
2 3 5 6 8 10
10
5
12345
10
Sample Output 0
3
3
Sample Input 1
1
7
56 57 8 9 6 3 4
60
Sample Output 1
You are going to a sweet shop to have sweets as you are low on energy. The sweets are selling
out. There is only a kilogram of N types of sweets remaining, each having a specific cost and
energy. You have a total of C amount of money with you. Your task is to calculate the maximum
total energy that you can gain by purchasing the sweets optimally and savoring them. In
particular, note that you can buy only a portion weighing K of the provided sweet less than or
equal to kilogram that contains a provided energy parameter e. Eating this sweet can increase
your total energy by e x k. The maximum total energy can be denoted as a fraction x/y in lowest
terms, that is, GCD (x,y) =1. Print the answer as (x*(y-1))%109+7.
Sample Input:
3
5
3
1
7
8
9
4
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.
Input Format
Input Format
The first line contains an integer N denoting the number of elements in costs.
Constraints
Constraints
1<=N<10^5
1<=energies[i]<=10^9
1<=C<=10^9
Output Format
Output Format
Sample Input 0
3
5
3
1
7
8
9
4
Sample Output 0
17
Sample Input 1
3
6
5
3
9
6
3
8
Sample Output 1
Given an array of N distinct numbers, the task is to sort all even-placed numbers in increasing
and odd-placed numbers in decreasing order. The modified array should contain, all sorted
even-placed numbers followed by reverse sorted odd-placed numbers.
Example:
Input:
2
8
01234567
9
3 1 2 4 5 9 13 14 12
Output:
02467531
2 3 5 12 13 14 9 4 1
Input 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.
Constraints
Constraints:
Output Format
Output:
Print the modified array which contains all sorted even placed numbers followed by reverse
sorted odd placed numbers.
2
8
01234567
9
3 1 2 4 5 9 13 14 12
Sample Output 0
02467531
2 3 5 12 13 14 9 4 1
Sample Input 1
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
Sample Output 1
A Maze is given as N*N binary matrix of blocks where source block is the upper left most block
i.e., maze[0][0] and destination block is lower rightmost block i.e., maze[N-1][N-1]. A rat starts
from source and has to reach the destination. The rat can move in only two directions: first
forward if possible or down. If multiple solutions exist, the shortest earliest hop will be
accepted. For the same hop distance at any point, forward will be preferred over downward.
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].
Example:
Input
2
4
2100
3001
0101
0001
4
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].
Input Format
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.
Constraints
Constraints:
1 <= T <= 50
2 <= n <= 10
0 <= m[i][j] <= 5
Output Format
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.
Sample Input 0
2
4
2100
3001
0101
0001
4
2100
2001
0101
0001
Sample Output 0
1000
1001
0001
0001
-1
1
4
2100
3001
0101
3001
Sample Output 1
1000
1000
0000
1001
Sample Input:
1
2
3
4
5
-1
Sample Output:
Sample Input 0
1
2
3
4
5
-1
Sample Output 0
Sample Input 1
1
2
3
-1
Sample Output 1
The United Nations Organization released an official document regarding the most important
events from the beginning of time (date: 00-00-0000) with a brief description of the events. The
date of all the events is mentioned in the ‘DD-MM-YYYY’ format. Find the total number of
distinct years referenced in the document.
Sample Input:
Sample Output:
Explanation:
Input Format
Input Format
Given input is in the form of string containing the content of the document.
Output Format
Output Format
Sample Input 0
Sample Output 0
Sample Input 1
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.
Sample Output 1
Every character has a weight. The weight of an English uppercase alphabet A-Z is given below.
A=1 B=2*A+A C=3*B+B D=4*C+C . . . Y=25*X+X Z=26*Y+Y The weight of any string made up of
these characters is the summation of weights of each character. Given a total string weight,
determine shortest string of that given weight. If there is more than one solution, return the
lexicographically smallest of them. For example, given weight = 25, and the weights of the first
few characters of the alphabet are A=1, B=3, C=12, D=60 it is certain that no letter larger than
C is required. Some of the strings with a total weight equal to the largest are ABBBBC, ACC, and
AAAAAAABBBBBB. The shortest of these is ACC. While any permutation of these characters will
have the same weight, this is the lexicographically smallest of them. Function Description
Complete the function smallestString in the editor below. The function must return the shortest
string of the target weight. If there are multiple answers, return the lexicographically smallest
of them. smallestString has the following parameter(s): weight: a long integer that denotes the
target weight.
Sample Input:
20
Sample Output:
AABBC
Input Format
Input Format
Constraints
Constraints
1<=weight<=1016
Output Format
Output Format
Sample Input 0
20
Sample Output 0
AABBC
16
Sample Output 1
ABC
Maximum profit obtained by buying and selling stocks You have been given stock prices for
next N days. Find out the maximum profit that can be obtained by buying and selling the stocks.
Conditions: Stock must be sold any day after the buying date You can buy and sell multiple
times, but cannot hold more than 1 stock at a time. You can only perform a single transaction
on a day i.e., can only either buy or sell on a single day. For example: Share price in thousands
5 1 4 6 7 8 4 3 7 9 Max benefit:
Sample Input:
10
5146784379
Sample Output:
13
Input Format
Input Format
Output Format
Output Format
Sample Input 0
10
5146784379
Sample Output 0
13
John assigned a roll number to each student in ascending order. Every time he assigns a number
to a student he wants to check whether the number is already assigned to any other student to
avoid the duplicates in the roll number. Add the number to the list only if the number is not
present already. Implement this concept using a Linked List. If the list is empty, print "List is
empty".
Sample Input
11
22
33
22
33
44
-77
Sample Output
11
22
33
44
Input Format
Input Format
The input consists of a list of integers, negative value indicates the end of the linked list.
Output Format
Output Format
Sample Input 0
11
22
33
22
33
44
-77
11
22
33
44
Sample Input 1
-5
Sample Output 1
List is empty
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.
Example:
Input:
2
6
123456
5
2 7 8 9 10
Output:
654321
10 9 8 7 2
Explanation:
Input Format
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.
Constraints
Constraints:
Output:
For each test case, print the reversed linked list in a new line.
Sample Input 0
2
6
123456
5
2 7 8 9 10
Sample Output 0
654321
10 9 8 7 2
Sample Input 1
3
3
1 12 3
4
17 18 19 10
6
25 45 23 60 40 55
Sample Output 1
3 12 1
10 19 18 17
55 40 60 23 45 25