Hack With Infy - Handout
Hack With Infy - Handout
1. Ramu has N dishes of different types arranged in a row: A1,A2,…,AN where Ai denotes the type of the ith
dish. He wants to choose as many dishes as possible from the given list but while satisfying two conditions:
a. He can choose only one type of dish.
b. No two chosen dishes should be adjacent to each other.
Ramu wants to know which type of dish he should choose from, so that he can pick the maximum number of
dishes.
Example :
Given N= 9 and A= [1,2,2,1,2,1,1,1,1]
For type 1, Ramu can choose at most four dishes. One of the ways to choose four dishes of type 1 is A1,A4, A7
and A9.
For type 2, Ramu can choose at most two dishes. One way is to choose A3 and A5.
So in this case, Ramu should go for type 1, in which he can pick more dishes.
Input Format:
The first line contains T, the number of test cases. Then the test cases follow.
For each test case, the first line contains a single integer N.
The second line contains N integers A1,A2,…,AN.
Output Format:
For each test case, print a single line containing one integer ― the type of the dish that Ramu should choose
from. If there are multiple answers, print the smallest one.
Constraints:
1 <= T <= 10^3
1 <= N <= 10^3
1 <= Ai <= 10^3
Sample Input :
3
5
1 2 2 1 2
6
111111
8
12223421
1
Sample Output :
1
1
2
Explanation:
2. There are three piles of stones. The first pile contains stones, the second pile contains b stones and the third
pile contains c stones. You must choose one of the piles and split the stones from it to the other two piles;
specifically, if the chosen pile initially contained s stones, you should choose an integer k (0≤k≤s), move k
stones from the chosen pile onto one of the remaining two piles and s−k stones onto the other remaining pile.
Determine if it is possible for the two remaining piles (in any order) to contain x stones and y stones
respectively after performing this action.
2
Input Format:
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 five space-separated integers a,b,c, x and y.
Output Format :
For each test case, print a single line containing the string “YES” if it is possible to obtain piles of the given
sizes or “NO” if it is impossible.
Constraints :
1 <= T <= 100
1 <= a,b,c,x,y <= 10^9
Sample Input :
4
12324
32565
24262
6 5 2 12 1
Sample Output :
YES
NO
YES
NO
Test case 1: You can take the two stones on the second pile, put one of them on the first pile and the other one
on the third pile.
Test case 2: You do not have enough stones.
Test case 3: You can choose the first pile and put all stones from it on the second pile.
3
Explanation:
3. Altaf has recently learned about number bases and is becoming fascinated.
Altaf learned that for bases greater than ten, new digit symbols need to be introduced, and that the convention is
to use the first few letters of the English alphabet. For example, in base 16, the digits are 0123456789ABCDEF.
Altaf thought that this was unsustainable; the English alphabet only has 26 letters, so this scheme can only work
up to base 36. But this is no problem for Altaf, because Altaf is very creative and can just invent new digit
symbols when she needs them. (Altaf is very creative.)
Altaf also noticed that in base two, all positive integers start with the digit 1! However, this is the only base
where this is true. So naturally, Altaf wonders: Given some integer N, how many bases b are there such that the
base-b representation of N starts with a 1?
Input Format:
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases
follows.
Each test case consists of one line containing a single integer N (in base ten).
Output Format:
For each test case, output a single line containing the number of bases b, or INFINITY if there are an infinite
number of them.
4
Constraints :
● 1 <= T <= 10^5
● 0 <= N < 10^12
Sample Input:
4
6
9
11
24
Sample Output :
4
7
8
14
Explanation:
5
4. You have been given a board where there are '2' rows and 'N' columns. You have an infinite supply of 2x1
tiles, and you can place a tile in the following ways:
6
Input format :
The first and only line of each test case contains an Integer 'N' which denotes the size of the board, i.e. '2' rows
and 'N' columns.
Output format :
For each test case, print the number of ways to tile the board modulo 10^9 + 7.
Note:
You are not required to print the output explicitly, it has already been taken care of. Just implement the function.
Constraints :
1 <= N <= 10^18
Where 'N' is the number of columns in the board.
Time limit: 1 sec
Sample Input 1 :
3
Sample Output 1 :
3
Explanation to Sample Input 1 :
For a 2*3 board, there are three ways:
1. Place all 3 tiles vertically.
2. Place the first tile vertically and the remaining 2 tiles horizontally.
3. Place the first 2 tiles horizontally and the remaining tiles vertically.
Sample Input 2 :
4
Sample Output 2 :
5
Explanation to Sample Input 2 :
7
For a 2*4 board, there are five ways:
1. All 4 vertical
2. All 4 horizontal
3. First 2 vertical, remaining 2 horizontal
4. First 2 horizontal, remaining 2 vertical
5. Corner 2 vertical, middle 2 horizontal
5. You are given an integer N which denotes the number of courses numbered from 1 to N and a matrix
‘prerequisites’, in which each row contains exactly two integers ‘A’ and ‘B’ which represents the course ‘A’
has to be studied in some semester before studying course ‘B’.
You are supposed to find the minimum number of semesters required to study all the courses. If it is
impossible to study all the courses, then return -1.
Note : There is no limitation on taking the number of courses in a particular semester as long as all the
prerequisites for taking the course are satisfied.
Input Format :
● The first line of input contains an integer ‘T’, denoting the number of test cases. The test cases follow.
● The first line of each test case contains two integers ‘N’ and ‘M,’ which denotes the number of courses
and the number of rows of the matrix ‘prerequisites.’
● The next M lines contain two integers, prerequisites[i][0] and prerequisites[i][1], denoting that
prerequisites[i][0] has to be studied before prerequisites[i][1].
Output Format :
For each test case, print the minimum number of semesters required to study all the courses.
Constraints :
1<= T <= 50
1 <= N <= 20000
0 <= M <= 20000
1 <= prerequisites[i][0], prerequisites[i][1] <= N
prerequisites[i][0] != prerequisites[i][1], for any valid i
Sample Input 1 :
2
76
16
26
36
46
56
67
55
8
12
23
34
45
51
Sample Output 1 :
3
-1
In the second test case, there are five courses and five prerequisites. 1 should be finished before 2, 2 should be
finished before 3, 3 should be finished before 4, 4 should be finished before 5, and 5 should be finished before
1. All the courses are dependent on other courses. There is no course to start in the first semester. So, none of
the courses can be finished. So, the answer is -1.
Sample Input 2 :
2
77
16
26
36
46
56
67
71
54
12
23
34
45
Sample Output 2 :
-1
5
6. A string 'S' is defined as S[s,N] such that 'N' repetitions of 's' make 'S'. For example if S[“ab” 4] then 'S' =
”abababab”. You are given two string S1[s1, N1] and S2[s2, N2]. Your task is to find the maximum value of
'M' such that [S2, M] can be obtained from S1.
It is defined that 'S1' can be obtained from 'S2' if we can remove some character from 'S2' to get 'S1'. For
example, string 's' = ”ab” can be obtained from “adeb” but not from “adef”.
9
For example,
S1[“abc” 4] , S2 = [“ab” 2]
Note:
You do not need to print anything or take input. This already has been taken care of. Just implement the
function.
Constraints:-
1 <= T <= 5
1 <= length of s1, length of s2 <= 100
1 <= n1, n2 <= 10^3
Test case 2:
Here S1 = "abcdeabcdeabcdeabcdeabcde" and S2 = "aaa".
We can obtain "aaaaa" from S1. Here we can find S2 only one time, so M = 1.
10
Sample input 2:
2
abcd 4
aba 2
abde 2
abde 4
Sample output 2:
1
0
7. You are given 2 numbers ‘N’ and ‘M’ and an array of size ‘N’. A triplet is defined if it satisfies ANY ONE of
the following conditions:
1) All numbers in the triplet are the same (Eg. {1, 1, 1})
2) The numbers are consecutive (Eg. {1, 2, 3})
Given the array, find the maximum number of triplets that can be formed. All elements in the array are <= ‘M’.
Output: 1
There are two ways of getting one triplet: either we group all the 1’s together ({1,1,1}) which form the triplet of
type 1, or we can pick {1,2,3} and form the triplets of type 2.
Input Format :
● The first line will contain the integer 'T', the number of test cases.
● Each test case consists of two lines.
● The first line of input contains two integers, 'N' and 'M', separated by spaces.
● Followed by a line containing space-separated ‘N’ positive integers not exceeding ‘M’, denoting the
array gifted by Alice.
Output format :
For each test case, print the maximum number of triplets that can be formed from the given array.
Note : You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= 'T' <= 10
1 <= 'N' <= 10^5
1 <= 'M' <= 10^4
1 <= ‘ARR[i]’ <= ‘M’
Time Limit: 3 sec
Sample Input 1 :
11
2
10 6
2333444556
12 6
153334353233
Sample Output 1 :
3
3
Sample Input 2 :
2
13 5
1151233242345
6 13
12 12 12 11 10 12
Sample Output 2 :
4
2
8. You are given an array ‘ARR’ of length ‘N’. you have to make 3 cuts in ‘ARR’ such that four subarrays are
formed ‘A’, ‘B’, ‘C’, ‘D’. Let ‘P’, ‘Q’, ‘R’, ‘S’ be the sum of the respective subarrays. Find the minimum
possible absolute difference of maximum and minimum among ‘P’, ‘Q’, ‘R’, ‘S’.
EXAMPLE:
Input:
'N' = 5
‘ARR’ = [3, 2, 4, 1, 2]
Output: 2
If we divide ‘ARR’ as ‘A’, ‘B’, ‘C’, ‘D’ = [3], [2], [4], [1, 2], then ‘P’ = 3, ‘Q’ = 2, ‘R’ = 4, ‘S’ = 3. Here the
maximum and minimum among ‘P’, ‘Q’, ‘R’, ‘S’ are 4 and 2, with an absolute difference of 2. we cannot make
the absolute difference of maximum and minimum less than 2, so the answer is 2.
Input Format :
The first line will contain the integer 'T', the number of test cases. For each test case
● In the first line of each test case, an integer ‘N’ denotes the length of the array ‘ARR’.
12
● The second line of each test case contains ‘N’ integers denoting the elements of array ‘ARR’.
Output format :
For each test case, print the minimum possible absolute difference of maximum and minimum among ‘P’, ‘Q’,
‘R’, and ‘S’.
Note : You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= 'T' <= 10
4 <= 'N' <= 10^5
1 <= ‘ARR[i]’ <= 10^5
Sample Output 1 :
36
999999994
In the second test case, the optimal division of ‘ARR’ is [1, 2, 3] [1000000000] [4, 5] [6]. Then ‘P’ = 6, ‘Q’ =
1000000000, ‘R’ = 9, ‘S’ = 6. Here the maximum and minimum among ‘P’, ‘Q’, ‘R’, ‘S’ are 1000000000 and
4, with an absolute difference of 999999994. we cannot make the absolute difference between maximum and
minimum less than 999999994
Hence, the answer is 999999994.
Sample Input 2 :
2
4
4376
4
3751
Sample Output 2 :
4
13
6
9. A theatre is open on ‘K’ days in the Ninja-Land. There are ‘N’ people who visit this theatre every day.
Ninja is given ‘K’ arrays which are all permutations of numbers from 1 to N. The i-th array represents the order
of ‘N’ people coming to a theatre on some i-th day (1 <= i <= K). Ninja has to find the maximum size of the
group of people that come to the theatre in the same sequence each of the K days. Ninja called you for help as
you are his only friend. Help him to solve the problem.
There can be multiple sequences with the same maximum answer. We have to print only a single integer that is
the maximum number of people that came in the same order all day.
In the problem 'MATRIX' stores all the permutations in the form of a 2D array.
Example:
Input:
'N' = 3
‘K’ = 2
MATRIX[1] = [ 1, 2, 3 ]
MATRIX[2] = [ 1, 3, 2 ]
Output: 2
As the [1, 2] is the maximum number of people containing sequence that come in the same order all day. Hence
answer is 2.
There can be other sequences, like [1, 3] but note that the maximum length of the sequence cannot be greater
than 2 here, therefore we will print 2.
Input Format :
The first line will contain the integer 'T', denoting the number of test cases.
In each test case, the first line of input contains an integer N — the number of people coming to the theatre
every day.
The second line contains a single integer ‘K’ —the number of days theatre is open.
In the next ‘K’ lines, each line contains an array of size ‘N’ denoting the order in which all the N-people arrive
on the i-th day.
Output format :
For each test case, output one integer — Find the maximum size of the group of people that come to the theatre
in the same sequence each of the K days.
Note : You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 100
1 <= K <= 10
1 <= MATRIX[i][j] <= N
[Link] = K and
14
MATRIX[i].length = N
Time Limit: 1 sec
Sample Input 1 :
2
4
2
1234
1324
2
1
12
Sample Output 1 :
3
2
Sample Input 2 :
2
4
3
1324
1324
1432
3
1
213
Sample Output 2 :
3
3
10.Ninja wants to be a faster typist and is taking a typing test to find out which key takes the longest time to
press.
Given the results of the test, determine which key takes the longest to press.
For example, given keyTimes =[[0, 2], [1, 5], [0, 9], [2, 15]]. Interpret each keyTimes[i][0] as an encoded
character in the range ascii[a-z] where a = 0, b = 1,...z = 25.
In the problem keyTimes is represented by ‘ARR’ and ‘N’ represents size of ‘ARR’.
The second element represents the time the key is pressed since the start of the test.
15
In the example, keys pressed in order are abac at times 2, 5, 9, 15. From the start time, it took 2 - 0 = 2 to press
the first key, 5 - 2 = 3 to press the second, and so on. The longest time it took to press a key was key 2, or 'c', at
15 - 9 = 6. Output the single character, the slowest key that Ninja presses. If more than one key is pressed after
the same time then output the smallest character. Ninja called you for help as you are his only friend. Help him
to solve the problem.
Example:
Input: ‘N’ = ‘4’ , 'ARR' = [[0, 2], [1, 5], [0, 9], [2, 15] ]
Output: ‘c’
As ‘c’ took 15-9=6 to press.
Input Format :
The first line will contain the integer 'T', denoting the number of test cases.
For each test case, the first line contains a single integer ‘N’ the size of the input array.
Next N-lines contain 2 space-separated integers- The first element represents the pressed key and the second
element represents the time at which the key is pressed.
Output format :
For each test case, print a single character representing the key which takes the longest time to press.
Note : You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 10^4
0 <= ARR[i][0] < 26
1<= ARR[i][1] <= 10^8
ARR[i-1][1] <= ARR[i][1] (for all 2 <= i <= N )
Time Limit: 1 sec
Sample Input 1 :
2
4
14
35
46
57
2
11
23
Sample Output 1 :
b
c
16
Sample Input 2 :
2
4
11
55
25 6
57
2
17
20 100
Sample Output 2 :
f
u
11.Ninja is participating in In a Rock Paper Scissor tournament. The contestants stand in a straight line. Each pair
of consecutive players compete in a round. If there is an odd number of players, the last one in the line
qualifies for the next round without playing.
For each game, each player indicates either a rock, paper or scissors denoted 'R', 'P' or 'S' respectively.
Outcomes are as follows:
After each round, the winners remain and the losers are out of the competition. In case of a tie, both players
lose. Ninja would like to win the competition and has one advantage: the knowledge that each other player uses
only one type of hand formation in all the rounds of the tournament. Determine how many times Ninja will have
to change the hand formation in order to win the competition.
Example:-
The number of players, N = 3, and Ninja is standing at the position given by NINJAPOSITION = 2 (0-based
index). The hand formations used by other players are given by formations = "PS" with length n - 1 = 2. In the
first round, scissors (S) beats paper (P). The ninja must then beat the winner of round one who always chooses
scissors (S). Ninja will choose rock (R) and win the tournament after having chosen only one hand formation,
hence the answer is 0.
Input Format :
The first line contains the single integer ‘T’ representing the number of test cases.
For each test case, the first line contains an integer ‘N’ representing the number of players in the tournament.
Second-line contains an integer ‘ninjaPosition’ representing the position of Ninja in the line, 0-indexed.
Third-line contains a string ‘playerMoves’ of length ‘N-1’ representing the hand formations used by other
players.
Output format :
17
For each test case, Print an integer, the number of times the Ninja needs to change hand formation in order to
win the tournament.
Note : You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 10^5
0 <= NINJAPOSITION <= N-1
PLAYERMOVES[i] belongs to {‘R’, ‘P’, ‘S’} for all 0 <= i < N
[Link] = N-1
Sample Input 1 :
2
4
1
PRS
3
2
PS
Sample Output 1 :
1
0
Sample Output 2 :
0
1
12.Your friend loves strings and he recently came across a string problem that he was not able to solve so he
asked you to help him with that problem.
18
You are given an integer ‘N’ representing the number of strings then you have got an array ‘ARR’ of strings on
which you have to tell the number of substrings for each string in the array which satisfies the following
conditions:-
For each string the substring is the string that is derived from removing zero or more characters from the front
and zero or more characters from the back.
Example:-
For string ‘ABC’ the valid substrings are:-
‘ABC’
‘AB’
‘A’
‘BC’
‘B’
‘C’
Output :- 2
For string ab:/a\a all the valid substrings are:-
ab:/a\a
b:/a\a
Input Format :
The first line contains the single integer ‘T’ representing the number of test cases.
For each test case, the first line contains an integer ‘N’ representing the number of strings in ‘ARR’.
Next ‘N’ lines contain strings representing the strings of ‘ARR’.
Output format :
For each test case, Print N elements represent the number of valid substrings of each string of ARR.
Note :You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 5
0 <= |ARR[i]| <= 10^5
Each character of ARR[i] belongs to [a-z, 0-9, /, \, :]
Time Limit: 1 sec
19
Sample Input 1 :
2
1
ab:/a\a
2
w\\//a/b
w\\//a\b
Sample Output 1 :
2
00
Sample Input 2 :
2
4
w\\/a\b
w:://a\b
w::/a\b
w:/a\bc::/12\xyz
1
w\\/a\bw:/a\bc::/12\xyz
Sample Output 2 :
0018
10
Note : Distance is the difference in the index at which the value occurs.
Example:
N=8
A = [ 5, 10, 5, 7, 3, 2, 4, 5 ]
20
The peaks are at index 2, 4, and 8. Troughs are at index 1, 3, and 6.
Five present at index 8 is a peak because it has no next element, and the element before it is smaller. Rest peaks
have smaller elements on either side.
The five present at index 1 is a trough because it has no previous element and the element next to it is greater.
Rest troughs have greater elements on either side.
Input Format
The first-line contains 'T,' denoting the number of Test cases.
For each Test case: The first line contains an integer, 'N', where 'N' denotes the number of values generated.
The next line contains ‘N’ integers, values generated by the function.
Output format :
For each test case, print a single integer denoting the maximum distance between any two consecutive peaks or
two consecutive troughs.
Constraints :
1 <= 'T' <= 10
1 <= 'N' <= 10^5
-10^8 <= 'A[i]'' <= 10^8
Sample Input 1 :
2
8
5 10 5 7 3 2 4 5
8
5 10 10 10 10 10 10 5
Sample Output 1 :
21
4
7
Sample Input 2 :
2
5
12345
10
10 10 10 10 10 10 10 10 10 10
Sample Output 2 :
0
0
14.A program is required to find certain patterns in modeling data that have been provided in the form of an
array.
The program is looking for a non-increasing pattern in the data.
Example: Assume that in an array A, A[1] >= A[5] >= A[7] holds true Then, these three elements form one
such pattern. You are provided the input data for this program.
You need to print the maximum possible difference in the index of two elements of any such pattern found by
the program.
EXAMPLE :
Input: ‘N’ = 5, ‘arr’ = {8, 9, 10, 2, 9}.
Output: 2
22
In this case, the non-increasing patterns are [8, 2], [9, 2], [9, 9], [10, 2], [10, 9]. Out of which the maximum
difference between the indices of the pattern is 2 i.e. in the pattern [8, 2] and [9, 9].
Hence the output is 2.
Input Format :
The first line will contain the integer 'T', the number of test cases.
Each test case consists of two lines.
The first line contains an integer ‘N’, denoting the number of data elements in the array.
Followed by a line containing space-separated ‘N’ positive integers, denoting the elements of the array.
Output format :
For each test case, print one line denoting the maximum difference.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^5
1 <= ‘arr[i]’ <= 10^9
It is guaranteed that sum of ‘N’ over all test cases is <= 10^5
Time Limit: 1 sec
Sample Input 1 :
2
6
10 3 5 6 8 8
5
10 11 3 2 1
Sample Output 1 :
5
4
Sample Input 2 :
2
7
5555555
8
19 20 25 32 36 40 50 10000000
23
Sample Output 2 :
6
0
15.Charlie is dropping off his nephew Jake at the Science Museum in Malibu. Charlie tells Jake that he'll be back
to pick Jake up after exactly ‘X’ minutes. Since Jake does not want to waste any time, he decides to utilize all
the ‘X’ minutes (neither more nor less).
There are ‘N’ exhibition tours going on in the museum, connected by ‘M’ corridors. Entry into the museum is
free, but each exhibition tour has a cost. When walking around the museum, Jake will never skip an exhibition
when walking past it, even if he has visited it before. He can also visit an exhibition more than once back to
back since he's fond of them.
It is given that walking through the corridors from one exhibition to another takes a given amount of time ‘T’.
Find the minimum amount that has to be spent by Jake while visiting the exhibitions.
NOTE: Jake always starts and ends at exhibition 1, since the entrance is located there, Once you enter an
exhibition you have to complete its tour and pay its cost (that means the first visit of Jake will always be at
exhibition 1). You can revisit the same exhibition multiple times in a row.
EXAMPLE :
Input: ‘X’ = 3, ‘N’ = 2, ‘M’ = 1, ‘T’ = 2, ‘EXHIBITIONS’ = {{1, 1}, {1, 1}}, ‘CORRIDORS’ = {{1, 2}}
Output: 3
In this case, it is optimal to tour the first exhibition for ‘3’ minutes, the cost of one tour of the first exhibition is
‘1’, and the time spent is ‘1’, hence for ‘3’ tours the cost is ‘3’. Hence the output is ‘3’.
Input Format :
The first line will contain the integer 'C', the number of test cases.
The first line of each test case contains four integers, ‘X’, ‘N’, ‘M’, and ‘T’ separated by spaces.
Followed by ‘N’ lines, where the ‘i’th line contains two integers ‘t’ and ‘c’, where ‘t’ denotes the time taken to
tour the ‘i’th exhibition and ‘c’ denotes the cost of attending the ‘i’th exhibition.
Followed by ‘M’ lines, each containing two integers ‘u’ and ‘v’, denoting that there is a corridor from the
exhibition ‘u’ to the exhibition ‘v’.
Output format :
For each test case, print an integer denoting the minimum cost required to spend ‘X’ minutes or print -1 if it is
not possible to stay for exactly ‘X’ minutes.
Note : You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= ‘C’ <= 10
1 <= ‘X’ <= 10^3
1 <= ‘N’ <= 10^3
0 <= ‘M’ <= 10^3
24
1 <= ‘T’ <= 10^3
1 <= ‘t’, ‘c’ <= 10^3
1 <= ‘u’, ‘v’ <= ‘N’
It is guaranteed that sum of ‘X’ over all test cases is <= 10^3
It is guaranteed that sum of ‘N’ over all test cases is <= 10^3
It is guaranteed that sum of ‘M’ over all test cases is <= 10^4
Time Limit: 1 sec
Sample Input 1 :
2
6441
12
21
54
33
12
23
34
41
2211
13
11
12
Sample Output 1 :
5
6
For the second test case, Jake first visits exhibition 1 and revisits it again.
Thus, time taken = 1(Tour ex1) + 1(Tour ex1)
Amount Spent = 3(Cost of ex1) + 3(Cost of ex1)
Hence, the output will be: 6
Sample Input 2 :
2
5211
11
12
12
7311
21
25
22
12
21
Sample Output 2 :
4
-1
16.Given a number ‘N’, you have to count the number of pairs ‘{ X, Y }’, where “0 <= X <= N and 0 <= Y <=
N”, such that ‘F(X) + F(Y)’ is a prime number. ‘F(X)’ is defined as the Sum of digits of a number 'X'.
Note: You don’t need to print anything. It has already been taken care of. Just implement the given function.
Answers could be large so you have to return Answer modulo 1e9+7.
For example:
‘N’ = 3
5 pairs (0,2), (1,1), (0,3), (2,3), (1,2) satisfies the above property.
Hence answer is 5.
Input Format :
The first-line contains 'T,' denoting the number of Test cases.
For each Test case, there is a single integer “N”.
Output format :
Return the number of pairs (X, Y) modulo 1e9+7, satisfying the above property for each test case.
Constraints :
1<= ’T’ <=10
1<= ‘N’ <=10^30
0<= ‘X’<=’N’
0<= ‘Y’<=’N’
Sample Input 1 :
2
3
4
Sample Output 1 :
5
7
26
5 pairs (0,2), (1,1), (0,3), (2,3), (1,2) satisfies the above property.
Hence answer is 5.
Sample Input 2 :
2
7
2
Sample Output 2 :
14
3
17.Given an integer ‘N’ denoting the no. of particles initially and an array of sizes of these particles. These
particles can go into any number of simulations (possibly none).
In one simulation, two particles combine to give another particle with size as the difference between their size
of them (possibly 0).
Print the smallest particle that can be formed after some number of simulations.
NOTE: We can have zero simulations as well.
EXAMPLE :
Input: ‘N’ = 3, ‘arr’ = {30, 10, 8}
Output: 2
For the given input it is optimal to perform only one simulation selecting particles of sizes 10 and 8 resulting in
a particle of size 2.
Input Format :
The first line will contain the integer 'T', the number of test cases.
Each test case consists of two lines.
The first line of each test case contains one integer, 'N' denoting the length of the array of sizes of particles.
Followed by a line containing space-separated ‘N’ non-negative integers, denoting the elements of the array.
Output format :
For each test case, print the minimum particle size achievable after some number of simulations.
Note : You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= 'T' <= 10
1 <= 'N' <= 10^3
0 <= ‘arr[i]’ <= 10^3
It is guaranteed that sum of ‘N’ over all test cases is <= 10^3
It is guaranteed that sum of ‘arr[i]’ over all test cases is <= 10^3
27
Time Limit: 1 sec
Sample Input 1 :
2
4
1248
5
30 27 26 10 6
Sample Output 1 :
1
0
For the second test case, we can form a particle of size 0 by first performing a simulation on particles 26 and 6
which will result in a particle of size 20. Then we will again do a simulation with particles 30 and 20 which will
result in a particle of size 10, and then we will do a simulation with another particle of size 10 already present
resulting in a particle of size 0.
Hence, the output will be: 0
Sample Input 2 :
2
7
10 20 11 9 5 6 1
3
10 4 7
Sample Output 2 :
0
1
18.Sam is given a rectangular paper having dimensions ‘H * W’, where ‘H’ is the height and ‘W’ is the width.
Sam wants to fold the paper so its dimensions are ‘H1 * W1’. To achieve these dimensions he can fold the
given sheet. The paper can only be folded parallel to its edges and after folding, the dimensions should be
integers.
There could be many ways to get the desired dimensions, Sam is interested to know what is the minimum
number of moves required. Can you help Sam with this calculation?
EXAMPLE :
Input: ‘H’ = 8, 'W' = 4, ‘H1’ = 6, ‘W1’ = 1
28
Output: 3
In this case, We will perform the following folds on the given paper, First fold along the long edge 6 units from
the end of a side, resulting in a paper that is 6*4. Next, fold along the width 2 units from the 4 unit edge to have
6*2. Fold along the center of the 2 unit edge to achieve a 6*1 page in three folds.
Input Format :
The first line will contain the integer ‘T’, the number of test cases.
Each test case consists of two lines.
The first line of input contains two integers, ‘H’ and ‘W’ separated by spaces.
Followed by a line containing two space-separated integers, ‘H1’ and ‘W1’ denoting the final dimensions of the
paper Sam wants.
Output format :
For each test case, print the minimum number of moves required to achieve the dimensions specified by Sam.
Note : You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= ‘T’ <= 10
1 <= ‘H’, ‘W’ <= 10^15
1 <= ‘H1’ <= ‘H’
1 <= ‘W1’ <= ‘W’
Time Limit: 1 sec
Sample Input 1 :
2
23
22
44
11
Sample Output 1 :
1
4
29
Explanation Of Sample Input 1 :
For the first test case, we will fold the edge having 3 units of length into 2 units of length in one move and we
will get the desired dimensions.
Hence, the output will be: 1
For the second test case, we will first fold along the height of the sheet two times first by folding it to 2 units of
height and then in the next step to 1 unit of height. Similarly for the width. Hence in total four moves.
Hence, the output will be: 4
Sample Input 2 :
2
38
38
29 19
32
Sample Output 2 :
0
8
19.Given a tree with ‘N’ nodes, we must separate a connected component with exactly ‘K’ nodes.
A tree is a connected undirected graph without cycles. A connected component of an undirected graph is a
subgraph with a path between each pair of nodes. You are given queries specifying ‘K’ We need to find the
minimum edges to be removed for each query.
Example:
N= 5
Edges = [ (1,2) , (1,3) , (1,4) , (1,5) ]
Queries = [ 1, 2, 4]
We can delete between (1,2), and the subtree of 2 contains one node. Hence we can obtain a component with
one element by deleting one edge.
We can delete between (1,3),(1,4), and (1,5), and the component with 1 and 2 contains precisely two nodes.
Hence we can obtain a component with two elements by deleting three edges.
We can delete between (1,5) and the component with 1,2,3,4 contains exactly four nodes. Hence we can obtain a
component with four elements by deleting one edge.
Input Format
The First-line contains 'T,' denoting the number of Test cases.
For each Test case:
The first line contains an integer, 'N', where 'N' denotes the number of nodes in the tree.
Next, 'N-1' lines contain two integers ‘a’ and ‘b’, denoting an undirected edge between ‘a’ and ‘b’.
The following line contains a single integer ‘Q’, denoting the number of queries.
The following ‘Q’ lines contain a single integer ‘K’ for that query.
30
Output format :
For each test case
Print the output to each query,i.e., the minimum edges to be removed for each [Link] it is not possible, then
print ‘-1’.
Print the answer to each test case in a new line.
Constraints :
1 <= T <= 10
1 <= N <= 3000
1 <= Q <= 3000
1 <= K <= N
Sample Input 1 :
2
5
12
13
14
15
3
1
2
4
7
12
13
24
25
36
37
2
2
3
Sample Output 1 :
131
21
We can delete between (1,2), and the subtree of 2 contains one node. Hence we can obtain a component with
one element by deleting one edge.
31
We can delete between (1,3),(1,4), and (1,5), and the component with 1 and 2 contains precisely two nodes.
Hence we can obtain a component with two elements by deleting three edges.
We can delete between (1,5) and the component with 1,2,3,4 contains exactly four nodes. Hence we can obtain a
component with four elements by deleting one edge.
Thus Answer to the test case is [ 1, 3, 1].
N= 7
Edges = [ (1,2) , (1,3) , (2,4) , (2,5) , (3,6) , (3,7)]
Queries = [ 2 , 3]
We can delete between (1,2) and (2,5), and the component with 2 and 4 contains precisely two nodes. Hence
we can obtain a component with two-element by deleting two edges.
We can delete between (1,2), and the component with 2,4 and 5 contains exactly three nodes. Hence we can
obtain a component with three elements by deleting one edge.
Hence Answer to the test case is [ 2, 1].
Sample Input 2 :
2
6
12
13
24
15
36
4
1
2
4
5
9
12
13
24
25
36
37
48
49
4
1
2
4
5
Sample Output 2 :
1111
1 2 1 1
32
20.You are given an array of strings. There are ‘N’ strings in the array. You can perform the following operation
on any string of the array.
Rotate the string. Rotate here means removing the first element from the string and adding it to the end.
Example :
‘N’ = 4, ‘ARR’ = [11234, 34112, 41123, 11234]
The first and last strings are already equal and if we rotate the 2nd string twice and 3rd string once then all the
strings will be equal and a number of operations will be 3. Which is optimal.
You can perform the above operation on a string any number of times. You have to make all the strings of the
array equal by performing the minimum possible above operations or return -1 if they can not be made equal.
Input Format :
The first line will contain the integer 'T', denoting the number of test cases.
For each test case, the first line will contain a single integer 'N', the number of elements of the array.
The next line contains ‘N’ strings denoting the elements of the array. Strings only contain numbers and max
length of the string is L = 20.
Output format :
For each test print the minimum number of operations required to make the string equal or return -1.
Note : You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 200
1 <= L <= 20
Sample Input 1 :
2
4
11234 34112 41123 11234
2
111 222
Sample Output 1 :
3
-1
Sample Input 2 :
33
2
5
12345 23415 43251 31254 53124
3
123 231 231
Sample Output 2 :
-1
1
21.Amit and Raj are playing a new game in which there are ‘N’ numbers of piles given by an array ‘PILES’.
Each pile contains any number of stones in it. ‘PILES[ i ]’ denotes the number of stones in the ‘ith’ pile. They
are also given an integer ‘K’.
In each turn, a player removes some number of stones from any pile.
The number of stones removed has to be an integer multiple of ‘K’. If the number of stones is less than ‘K’ then
any number of stones can be removed from the pile.
The person who removes the last stone wins the game. Amit takes the first turn. Find the winner of the game.
Example:
Input: ‘N’ = 2, ‘K’ = 2, ‘NUMS’ = [2, 3]
Output: AMIT
Turn 1: Possible moves for Amit are: removing 2 stones from the 1st pile or 2 stones from the 2nd pile. But the
optimal choice is to remove 2 stones from the 1st pile.
So, Amit removes 2 stones from the 1st pile, ‘PILES’ = [0, 3].
Turn 2: Possible move for Raj is: removing 2 stones from the 2nd pile.
So, Raj removes 2 stones from the 2nd pile, ‘PILES’ = [0, 1].
Turn 3: Possible move for Amit is: removing 1 stone from the 2nd pile.
So, Amit removes 1 stone from the 2nd pile, ‘PILES’ = [0, 0].
Since Amit removes the last stone, he is the winner of the game.
Input Format :
The first line will contain the integer 'T', denoting the number of test cases.
The first line of each test case contains two integers ‘N’ and ‘K’ where ‘N’ denotes the length of the array
‘PILES’ and ‘K’ is an integer number.
The second line of each test case contains ‘N’ integers denoting the number of stones in each pile.
Output format :
For each test case, print who wins the game. If Amit wins the game print “AMIT” or else print “RAJ”.
Note : You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 10^5
34
1 <= K <= 1000
Sum of ‘N’ <= 10^5
1 <= PILES[i] <= 10^3
Sample Input 1 :
2
21
22
25
21
Sample Output 1 :
RAJ
AMIT
Sample Input 2 :
2
6 10
20 30 10 20 30 10
55
2 5 10 10 3
Sample Output 2 :
RAJ
RAJ
35