TCS Digital Coding Practice Set
TCS Digital Coding Practice Set
Example
If input is 2, output will be
00006
00028 00066
If input is 3, output will be
00006
00028 00066
00120 00190 00276
Formal input and output specifications are stated below
Input Format:
First line of input will contain number N that corresponds to the width of the bottom-most
layer of the Pyramid
Output Format:
The Pyramid constructed out of numbers in the series as per stated construction rules
Constraints:
0 < N <= 14
TCS Digital Coding
You are given an array A of size N. Your friend gave me an amazıng task
for you. Your friend likes one type of Sequence. So, he called that type of
sequence a fair sequence. You should select a fair sequence of maximum
length from an array. Here a fair sequence is nothing but you have to select
elements in a pattern like positive element, negative element, positive
element… (negative element, positive element, negative element, to form a
sequence.
Your task is to print the maximum sum of elements possible by selecting a
fair subsequence with maximum length.
Ex: If array A = [21, 12, 13, -21, -2]. Here your minimum length can be
which subsequences 21, -2. The sum is 19. Your task is to print the
maximum sum of elements possible by selecting a fair subsequence with
maximum length.
NOTE: You should select the elements in a fair sequence only.
Example – 1:
Input:
5 – N ( Number of elements in an array )
21 12 13 -21 -2 – Array A consists of N elements
Output:
19
Explanation:
Here you can select 21, -2 subsequences of maximum length 2. The
sum is 19 which is the maximum possible for a fair subsequence of
length 2.
Alice has introduced a new online game which has N levels i e., 1 to N.
She wants to get reviews for each level of this game so she will launch
only a single level of game on any particular day, and on that day users of
this game are allowed to play only that particular level. As there are N
levels so it will take exactly N days to launch all levels of the game, it is
not necessary that level of game will be launched in increasing order, she
will pick any random level on particular day and launch it, and it is obvious
that any level of the game can’t be launched twice. After completing the
level, the user will give reviews and she will give them one reward unit for
each level the user will complete. Alice has put one constraint on users that
only after completing any level of game, user will be able to play higher
levels. For E.g. If Alice has launched the 3rd level of the game on the first
day and if any user completes it then he will get one reward point, but now.
he can’t play the game with level 1 and level 2.
NOTE: If a user wants to skip to play on any number of days, he is free to
do that.
You are the best gamer, so you can easily complete all levels. As you want
to maximize your reward points, you want to play as many levels as you
can. Alice has already announced the order in which she will launch levels
of his game, your aim is to maximize your reward points.
Given number of levels of games(N) and order of level of games launched
one by one on each day. You have to output maximum reward points you
can earn.
Hint: You will play any game if and only if that number is becoming the
part of the longest subsequence in array of order of games.
Example 1:
Input:
5->N=5
2 1 3 4 5 -> Order of levels, Alice will launch one by one
Output:
4
Explanation:
If Alice plays the 2nd level of the game on the first day, then he will
not be able to play the 1st level of the game on the 2nd day. As after
completing the 2nd level, he will be able to play higher levels. From
3rd day he can play all upcoming levels as those levels are in
increasing order. So, possible sequences of levels of games played to
maximize rewards points are [2, 3, 4, 5] or [1, 3, 4, 5]. In both cases
he will get 4 reward points.
Example 2:
Input:
5->N=5
5 4 3 2 1 -> Order of levels, Alice will launch one by one
Output:
1
Explanation:
Alice has launched levels in decreasing order, so he will be able to
play exactly one level of game. After playing any level, there are no
higher levels on coming days, so maximum reward point is 1.
King Jeremy’s family has ruled a kingdom for N consecutive years
numbered from 1 to N. The year i is described by a prosperity value A[i].
This is a positive integer when there was prosperity in the year and a
negative integer when it was a difficult year for the kingdom. A historical
period is described with two integers, S and F as [S, F], i.e. [Start, Finish],
where S and F are from 1 to N, S <= F.
The target is to pick 2 historical periods, with the following 6 conditions:
1. The two periods shouldn’t have common years. For example (1,5) has
no common year with [6,7).
2. the First period should start earlier than the second one, 1.e. (1,5)
should be the first period, and then the [6,7] should start.
3. There should be a difference of more than K integers between the
Finish of the first period and the start of the 2nd period.
4. Sum of prosperity values for the years in the periods chosen should be
as large as possible.
Given N, A[ ], and K, give this maximum prosperity value as output.
E.g.
N = 5, number of years
K = 3, difference between 2 periods.
A = {-1, 1, 2, 3, -1} prosperity value for each year.
There is only 1 way to choose the two periods, which is [1,1] and [5,5) as
the difference has to be greater than 3 (value of K).
The prosperity values for these are
[1, 1] = -1
[5, 5] = -1
The Some of these values are -2. Hence, the answer is -2.
Example 1:
Input:
8 -> Input Integer, N
3 -> Input Integer, K
{5, 5, -1, -2, 3, -1, 2, -2} -> Input sequence, A[ ].
Output:
12 -> Output
Explanation:
In the above case: N equals to 8, K equals to 3, Al equals to {5, 5,-1,-
2,3,-1, 2, -2}. It is optimal to choose [1, 2] and [7, 7) periods. That is
the only optimal choice that you can make.
So the values will be
[1, 2] = 5 5
[7, 7] = 2
The sum of it will be 5 + 5 + 2 = 12
Example 2:
Input:
6 -> Input Integer, N
0 -> input Integer, K
{5, -1, 5, 0, -1, 9} -> Input sequence A[ ].
Output:
18 -> Output
Explanation:
In the above case: N equals 6, and K equals to 0. All equals to {5,-1,5,
0, 1,9}. It is optimal to choose [1, 3] and [6, 6] periods. But that is not
the only optimal choice that you can make So the values will be
[1, 3] = 5 – 1 5
[6, 6] = 9
Radha is attending her sister’s wedding, and there are a lot of things to do including
packing and food and decorating. Radha took up the responsibility for packing the
sweets, as no one was interested in doing such tedious work. She got an idea, where
she announced that we will pick a winner from a packing contest, and those who win
this game will be given a surprise gift. The best part is that more than 1 person can
be the winner in this game.
Radha, by default, is the first player. So, the game goes like this:
All the sweet boxes are represented with numbers, to differentiate them from
each other. Let us say that a box numbered ‘1’ will contain a different sweet
compared to the one which is numbered ‘2’.
Each player will be given different/same combinations of sweets boxes in
different/same quantities.
Each player has to pack all the respective boxes given to them, in one final
package.
As a default point, each person will be given the number of points equal to the
number of boxes they pack. Let us say they have 6 sweet boxes in their queue,
they will get 6 points as default.
If the final package contains 4 different types of sweets, then they will get an
additional 1 point.
If the final package contains 5 different types of sweets, then they will get
additional 2 points.
If the final package contains 6 or more different types of sweets, then they will
get additional 4 points.
Radha is Player 1, and the remaining players follow after that. The input will be in
the format given below:
For Player-1 (Radha):
1
6123457
This means there is only 1 player, and 6 is the number of boxes given to her. And
the remaining values (1 2 3 4 5 7).
If we have multiple participants then, say we have 3 participants
3
612345
41322
512233
This means we have 3 participants (N=3)
First participant:
Second participant:
Third participant:
Player 1: Radha
Has 6 boxes, so 6 points in the beginning.
She has 6 different types of sweets (from 1-6), hence 6 more points to that.
Total of 6 + 6 = 12 points
Player 2:
Has 9 boxes, so 9 points in the beginning.
She has only 3 different types of sweets (3,4 & 5), so no points to that.
Total of 9 points.
Input:
2 -> Input string. Integer N.
3 1 2 3 -> Input Integer, A[ ], Z[ ].
4 1 2 3 4 -> Input integer, A[ ], Z[ ].
Output:
2 -> Output
Explanation:
From the above inputs let’s calculate the total points for each player:
Player 1: Radha
Has 3 boxes, so 3 points in the beginning.
She has 3 different types of sweets (from 1-3), so no points to that.
Total of 3 points.
Player 2:
Has 4 boxes, so 4 points in the beginning.
She has only 3 different types of sweets (1 – 4), so 1 point to that.
Total of 3+1 = 4 points.
Reddington has invested his money in stock of N companies, but lately, he realized
that he has invested money in a very random manner. He wants to restructure the
money he invested. He decided on an integer K which he will use to modify the
amount invested either by putting k more amount for a particular company or
withdrawing the same K amount. He will withdraw from one company by selling a
stock) and will invest the same amount of K in another company (by buying
stock). For more than one transaction (either buy or sell) of stock in the same
company have some charges per month, such that he will not do more than one
transaction of a particular company means either he will buy or sell the stock of
amount K for a company or he will not do any transaction for that company. But he
can do as many transactions as he wants to be provided that in one company he will
do only one transaction. As you are his best friend so he reached out to you o for
help.
Reddington wants to keep the difference between the smallest and largest invested
amount minimal, can you please help him to get this minimum difference?
NOTE: Reddington can sell the stock of K from a particular company only if the
invested amount value for that particular company is higher than k.
Hint: To keep the difference minimum, if any transaction is needed then he needs to
sell the stock of amount from the company with the highest invested amount and
invest the same in the company with the smallest invested amount
Example 1:
Input:
2100 > N=100, K=100
100 1000 -> Invested amount for each company
Output:
700
Explanation:
Two companies C1 and C2 have invested amounts of 100 and 1000. From the
company, C2 Reddington will sell the stock of 100(K) units and buy the stock
of company C2. So, now the invested amount will be 200 and 900, and now
Reddington can’t do more transactions, so the minimum difference will be
900-200 = 700.
Example 2:
Input:
5 100 -> N=5, K=100
100 200 300 400 500 -> Invested amount for each
Output:
200
Explanation:
Five companies C1, C2, C3, C4, and C4 having invested amounts as 100, 200,
300, and 400,500 Reddington will sell the stock of C5 of 100 units and put it in
C1. So, the invested amount will be 200, 200, 300, 400, 400, Now Reddington
will sell the stock of C4 and put it in C2, then the invested amount will be 200,
300, 300, 300,02 400. Reddington can’t do more transactions now, so the
minimum difference will be 400 – 200 = 200.
Problem Statement-:Oneperson hands over the list of digits to
Mr. String, But Mr. String understands only strings. Within
strings also he understands only vowels. Mr. String needs
your help to find the total number of pairs that add up to a
certain digit D.
The rules to calculate digit D are as follows
Take all digits and convert them into their textual
representation
Next, sum up the number of vowels i.e. {a, e, i, o, u}
from all textual representation
This sum is digit D
Now, once digit D is known find out all unordered pairs of
numbers in input whose sum is equal to D. Refer example
section for better understanding.
Constraints
1 <= N <= 100
1 <= value of each element in second line of input <=
100
Number 100, if and when it appears in the input should be
converted to textual representation as a hundred and not as
one hundred. Hence the number of vowels in the number 100
should be 2 and not 4
Input
the First line contains an integer N which represents the number of elements to
be processed as input
the Second line contains N numbers separated by space
Output