0% found this document useful (0 votes)
34 views52 pages

TCS Codevita Previous Year Questions

Question for preparation of tcs code

Uploaded by

Aditya Aditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views52 pages

TCS Codevita Previous Year Questions

Question for preparation of tcs code

Uploaded by

Aditya Aditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

TCS Codevita Previous Year Questions

By Sagar Pandey (Insta handle : sagar_mee_)

Question -:You are given a binary string B of length L which contains K ones and
remaining zeros. You are required to place the K ones in the binary string in such a way
that the longest consecutive zeros have the least length possible. Once such a binary
string is constructed, you are required to print the length of the contiguous block of
zeros, which has the largest length.

Constraints
0 <= K <= L

1 <= L <= 10^6

Input

Single line consisting of two space separated integers denoting L and K.

Output

Print a single integer denoting the length of the longest consecutive zeros as per the
problem.

Time Limit (secs)

Examples

Example 1

Input
31

Output

Explanation

B is of length 3 and it has 1 one’s.

So the possible strings as per the problem are 010, 001, 100.

In the first case, the maximum length of consecutive zeros is 1 whereas in the other two
cases it is 2. Hence the constructed binary string is 010 and the output is 1.

Example 2

Input

33

Output

Explanation

B is of length 3 and it has all three one’s. There is no block of zeros, hence the output is
0.

Question -: Consider a set of web pages, numbered from 1 to N. Each web page has
links to one or more web pages. Clicking on a link in a page, takes one to the other web
page. You are provided numbers of two web pages viz, starting web page and end web
page. Your task is to find the minimum number of clicks required to reach the end page
from the start page. If end page cannot be reached from start page, print -1 as the
output. For better understanding refer Examples section.
Constraints

0 < N <= 100

0 < L < 10

Input

First line contains an integer N denoting number of web pages.

Next N lines contain L space separated integers depicting linked webpage number(s)
from that webpage

Output

Print the minimum number of clicks required to open the end page from start page. If
not possible, print -1 as output.

Time Limit (secs)

Example 1

Input

24

15

23

5
23

Output

Explanation:

First line conveys that there is total 5 pages.

Second line conveys that there are links from page 1 to pages 2 and 4.

Third line conveys that there is a link from page 2 to page 1.

Fourth line conveys that there are links from page 3 to pages 1 and 5.

Fifth line conveys that there are links from page 4 to pages 2 and 3.

Sixth line conveys that there is a links from page 5 to page 5 itself.

Seventh line conveys that starting page is 2 and ending page is 3

From page 2, we can open only page 1. From page 1, we can open page 4. From page
4, we can open page 3. So, minimum 3 clicks are required, and this is the output.

Example 2

Input

1
23

Output

-1

Explanation:

First line conveys that there is total 3 pages.

Second line conveys that there are links from page 1 to page 2.

Third line conveys that there is a link from page 2 to page 1.

Fourth line conveys that there are links from page 3 to page 1.

Since there is no way to reach from page 2 to page 3, print -1 as output.

Question -: A picnic to a famous museum is being planned in a school for class VI.
When they reached the spot, the students started quarreling among themselves in the
queue. So the teacher came up with an idea of “good string” which is explained below.
Good String is provided as input. All letters in this string are good letters. Good letters
need to be used in further computations as explained below.
The teacher asked all the students to convert their names into good names with the
help of good string. While converting, they have to calculate the distance. Based on
that, she will arrange the students in a queue.
For converting a name into good name, for each letter i in the name, select the nearest
letter from the good name. Distance is calculated as the differences between the ASCII
values of i and selected good letter. If there are two letters which are equidistant from i,
select the letter which is nearest to the previously used good letter. In that case,
distance will be the difference of ASCII value of previously used good letter and
selected letter. If i is already present in the good string then no need to change it.
Initially, previous good letter will be the first letter of good string. Calculate the total
distance of the given name.
Given the name of the student who is confused of implementing this task. Help him to
calculate the total distance for his name.
Note: Letters from good string can be reused any number of times.

Constraints
1 <= len(good string) <= 100
1 <= len(name) <= 10^4
Good string will consist of lower, upper case alphabets, digits and symbols.
Name will consist of only space, lower and upper case alphabets.
Characters are case sensitive.
The ASCII values for all the characters in the good string and name will be between 32
to 126 (both inclusive).

Input
First line consists of good string.
Second line consists of the name of the student who is confused to implement the task.

Output
Print the total distance for that name.

Time Limit (secs)


1

Examples
Example 1
Input
(@HR*i{kcQl
Vyom
Output
10
Explanation
i
Previous good letter
Current good letter for i
V
(
R
y
R
{
o
{
l
m
l
l
The total distance will be |ASCII(V)-ASCII(R)| + |ASCII(y)-ASCII({)| + |ASCII(o)-ASCII(l)|
+ |ASCII(m)-ASCII(l)| = 4+2+3+1 = 10.

Example 2
Input
6*K4AQf]gpi
Nainika
Output
33
Explanation
i
Previous good letter
Current good letter for i
N
6
K
a
K
]
i


n
]
p
i


k
p
i
a
i
]
Initially, Previous good letter=6. Since K and Q are at the same distance from N, so we
select the character which is nearest to previous letter(6) which is K.
i is already present in the good string. So no need to change anything.
Therefore, total distance will be |ASCII(6)-ASCII(K)| + |ASCII(a)-ASCII(])| +
|ASCII(n)-ASCII(p)| + |ASCII(k)-ASCII(i)| + |ASCII(a)-ASCII(])| = 21+4+2+2+4 = 33.
Question -: A math game is introduced in a school competition to test the skills of
students. The game deals with Prime numbers.
The game rules are as follows:

● From the given set of distinct natural numbers as input, consider the smallest
natural number as q.
● Your task is to compute the smallest prime number (p) such that when p is
divided by all the distinct numbers in the input, except q, should result q as the
remainder.

Constraints :

● 1 < n < 11
● p < 10 ^ 10

Input :

Input consists of n+1 number of distinct natural numbers separated by spaces.

Output :

Print single integer p if such a p exists, else print “None”.

Time Limit : 1 secs

Example 1 :

Input :

3451
Output : 61

Explanation : Here the n+1 numbers are 3, 4, 5 and 1 where q=1 (the least of the
numbers)

The smallest number that leaves remainder 1 when divided by 3, 4 and 5 is 61 and is
prime. Hence, output is 61.

Example 2 :

Input :

3452

Output : None

Explanation : Here q=2. Any number that when divided by 4 leaving remainder 2 must
be an even number e.g., 6, 10, 14 etc. Hence it can’t be prime. Hence, output is “None”.

Question -: Consider a Jug of capacity L liters. Given N cups of different capacities Ci


(in liters), fill the Jug with the help of cups, according the specification.
The specification according to which the cups may be used to fill the Jug is as below

1. Cups can be used integral number of times i.e., zero or more times, but never

partially i.e., a cup of 1L can be used 0, 1, 2 etc. times, but never 0.5, 1.5, 2.5 ..

times

2. The Jug must not overflow because of cup filling the Jug
3. The number of distinct cups (i.e., different cup sizes) used to fill the Jug must be

maximized

4. The summation of number of times all cups are used must be minimized.

5. Consider point 3 to be more important than point 4 when meeting the

optimisation goals.

For better understanding of how cups can be used to fill the Jug, go through the
Examples section. Both examples clearly explain, when there are multiple ways to
achieve the objective, what is the correct answer and why.

Constraints :

0 < N < 10

0 < L <= 100

0 < Ci < L

Input :

First line contains an integer N denoting the number of cups available.

Second line contains N space separated integers denoting the capacity of the cups.

Third line contains an integer L which denotes the capacity of Jug in liters.

Output :

Output consists of two lines.


First line must comprise of N or less space delimited integers, in ascending order of cup
size, for the cups used to fill the Jug

Second line must comprise of equal number of space delimited integers which denote
the frequency i.e. the number of times the corresponding cup in first line is used to fill
the Jug.

Time Limit : 1 sec

Example 1 :

Input :

3 7 10 11

88

Output :

3 7 10 11

1261

Explanation :

The first and second lines indicate that you are provided with 4 cups of capacities – 3
liters, 7 liters, 10 liters and 11 liters. The third line indicates that the capacity of the Jug
is 88 liters.

One possible solution for filling the Jug is


7 10 11

523

i.e., one can use 7L cup for 5 times to get 35L. Next one can use the 10L cup twice.
After that the Jug will contain 55L. Finally, one can use 11L cup thrice. Thus, the Jug will
be filled. However, this solution uses cups of only 3 different capacities when 4 different
capacities are available. Hence the Jug is perhaps not filled according to the
specification. Let’s see if we can achieve our objective by using all 4 cup sizes.

We can use all the available cups if we use them as follows

3 7 10 11

1261

Hence, this is our final solution which adheres to the specification.

Example 2 :

Input :

2 5 10

50

Output :

2 5 10

523

Explanation :
The first and second lines indicate that you are provided with 3 cups of capacities – 2
liters, 5 liters, 10 liters. The third line indicates that the capacity of the Jug is 50 liters.

Here one can easily fill the Jug by using the 10L cup 5 times. However, this does not
obey the specifications. According to the specifications, one must use all available cups
of capacity 2L, 5L and 10L. If there are multiple ways in which the Jug can be filled by
using maximum number of distinct sized cups, then as per specifications one needs to
minimize the summation of number of times cups are used.

Consider the following solutions :

Solution 1

2 5 10

15 2 1

Solution 2

2 5 10

523

Both solutions use all available cups. However, sum of frequencies in Solution 1 is 18
(15 + 2 + 1), whereas sum of frequencies in Solution 2 is 10 (5 + 2 + 3). Solution 2
minimizes the summation of number of times any cup in used. This is also a
requirement as per the specification. Hence Solution 2 is the correct answer.

Question -: This is a resource to task allocation problem. There are two types of tasks
viz. one that the human needs to do and the other which as amenable to machine
processing. A human-oriented task cannot be performed by machine and vice-versa.
Also, assume that all human workers are equally efficient. So, the same task will be
performed in the same time, irrespective of which human worker does that task.
Given the number of workers, number of tasks and time taken to finish each task,
compute which task was finished at what time and which worker performed that task.
Some rules and conventions that need to be adhered to while doing task allocation to
workers are as below.

● Machine processing for any task takes 0 minutes

● Time taken (in minutes) by a human worker to complete a particular task is

mentioned in the input

● Workers and tasks are identified by their suffixes i.e., Worker 1 is W1 and Task 1

is T1.

● Task information is provided in the input in form of a tuple that consists of 3

things viz. <Taskname, TaskType, CompletionTime> where

○ Taskname will be of form T1, T2, T3 etc.

○ TaskType will be a keyword which can be either {Manual, Machine}

○ CompletionTime is a number denoting time in minutes needed for manual

tasks and 0 for machine-bound tasks

● A worker will still need to be assigned even if a task can be done only by the

machine

● Tasks need to be processed in sequential order i.e., T1 has to be allocated

before T2

● Initially when all workers are free, worker Wi will be allocated task before worker

Wi+1 i.e., in general a worker with lower suffix will be assigned to the task before

worker with a higher suffix.


● If one or more workers are available at the same time, then the worker who

processed lower suffix task will get the next task allocated prior to the other

workers.

● Assume the switching time from one task to another as 0 min

Refer Examples section for better understanding of how tasks are allocated to workers

Compute which task was completed at what time and which worker performed that task.
The output specifications provide information on how output should be printed.

Constraints :

● 1 <= N <= 10

● 0 < M <= 100

Input :

First line contains an integer N which denotes the number of workers

Second line contains an integer M which denotes the number of tasks

Next M lines contain a task information tuple as described in the previous section.

Output :

Output should adhere to the following specifications.

● Each output should be a tuple comprising of 3 pieces of information viz <Worker

name, task name, time at which the task was completed>


● The tuple should be first sorted in ascending order of completion time and then in

ascending order of task name

For better understanding refer to the Examples section.

Time Limit : 1 secs

Example 1 :

Input :

T1 Machine

T2 Manual 5

T3 Machine

T4 Machine

Output :

W1 T1 0

W3 T3 0

W1 T4 0

W2 T2 5
Explanation :

Here, N=3 denotes number of workers W1, W2, W3.

First, Workers W1, W2, W3 will be assigned with tasks T1, T2, T3 at the 0th minute,
respectively.

Here, T1 is Machine-bound so it will be completed in 0th minute and then W1 will be


available for the remaining tasks. T2 is Manual and it will be completed in 5th minute.
So, W2 will be free after 5 minutes. T3 again is Machine-bound so it will be completed in
0th minute and then W3 will be available for remaining tasks.

Now, Workers available for pending tasks in 0th minute = {W1, W3}

Here, W1 will be assigned with next pending task first, because the worker W1
completed task with lower suffix (T1) than W3 (T3).

W1 will be assigned the next task T4 at 0th minute which is a Machine-bound task
which will also be completed in 0th minute.

As the number of pending tasks are zero, no further assignment takes place.

W2 will complete T2 at the end of 5th minute. Hence,

First line of output is “W1 T1 0”, where T1 is the task completed by Worker W1 0th
minute.

Second line of output is “W3 T3 0”, where T3 is the task completed by Worker W3 in 0th
minute.

Third line of output is “W1 T4 0”, where T4 is the task completed by worker W1 in 0th
minute.

Fourth line of output is “W2 T2 5”, where T2 is the task completed by W2 in 5th minute.
So, the output is as follows:

W1 T1 0

W3 T3 0

W1 T4 0

W2 T2 5

Example 2 :

Input :

T1 Machine

T2 Machine

T3 Machine

T4 Machine

Output :

W1 T1 0

W2 T2 0

W3 T3 0

W1 T4 0
Explanation :

Given N = 3 denotes number of workers W1, W2 and W3

First, Workers W1, W2, W3 will be assigned with tasks T1, T2, T3 in 0th minute,
respectively.

Here, T1, T2 and T3 are Machine-bound and will be completed in 0th minute, after
completion of these tasks W1, W2, W3 will be available for the remaining tasks.

Since W1 is the first worker in the list of available workers at 0th minute. W1 will pick up
the 4th task and will complete it in 0th minute, because it is also a machine bounded
task.

Hence the output is as follows:

W1 T1 0

W2 T2 0

W3 T3 0

W1 T4 0

Question – : There are two banks – Bank A and Bank B. Their interest rates vary. You
have received offers from both banks in terms of the annual rate of interest, tenure, and
variations of the rate of interest over the entire tenure.You have to choose the offer
which costs you least interest and reject the other. Do the computation and make a wise
choice.

The loan repayment happens at a monthly frequency and Equated Monthly Installment
(EMI) is calculated using the formula given below :
EMI = loanAmount * monthlyInterestRate / ( 1 – 1 / (1 +
monthlyInterestRate)^(numberOfYears * 12))

Constraints:

● 1 <= P <= 1000000


● 1 <=T <= 50
● 1<= N1 <= 30
● 1<= N2 <= 30

Input Format:

● First line: P principal (Loan Amount)


● Second line: T Total Tenure (in years).
● Third Line: N1 is the number of slabs of interest rates for a given period by Bank
A. First slab starts from the first year and the second slab starts from the end of
the first slab and so on.
● Next N1 line will contain the interest rate and their period.
● After N1 lines we will receive N2 viz. the number of slabs offered by the second
bank.
● Next N2 lines are the number of slabs of interest rates for a given period by Bank
B. The first slab starts from the first year and the second slab starts from the end
of the first slab and so on.
● The period and rate will be delimited by single white space.

Output Format: Your decision either Bank A or Bank B.


Explanation:

● Example 1
○ Input
○ 10000
○ 20
○ 3
○ 5 9.5
○ 10 9.6
○ 5 8.5
○ 3
○ 10 6.9
○ 5 8.5
○ 5 7.9
● Output: Bank B

● Example 2
○ Input
○ 500000
○ 26
○ 3
○ 13 9.5
○ 3 6.9
○ 10 5.6
○ 3
○ 14 8.5
○ 6 7.4
○ 6 9.6
● Output: Bank A

Question -: Snake and Ladder is a board game consisting of snakes and ladders,
where the player must reach End position, starting from Start position. Here, snake on
the board makes player demotes the player to a lower numbered square and ladder
promotes player to higher numbered square on the board.
For e.g., given below is the snake and ladder board, where S(‘pos’) represents snake
and ‘pos’ indicates where the player’s coin will move down to once a player lands on
that square. Similarly, L(‘pos’) indicates ladder and ‘pos’ indicates where the player’s
coin will move up to once the player lands on that square. Player always starts from the
Start square and must go towards End based on the rolls of the die.
You are supposed to find if it is possible for a player to reach the End or not, based on
die inputs. If it is possible, display ‘Possible’ with number of snakes and ladders
encountered during his/her play else display ‘Not possible’ along with number of
snakes, number of ladders and square where the player’s coin has ended at.
Note: A player can reach the end if he has the exact number on die input to reach end.
For e.g., if the player is at 99, he/she can reach end only if the die input is 1 and not any
other input. So, he/she must wait till input on die is 1.
The actual Snake and Ladder board will look as depicted below. This format will be
used for providing inputs.

End 99 S(2) 97 96 95 94 93 92 91
81 82 83 84 85 86 87 88 89 90
80 79 78 77 76 L(99) 74 73 72 71
61 62 S(22) 64 65 66 67 68 69 70
60 59 58 57 56 55 54 53 52 51
41 42 43 44 L(68) 46 47 48 49 50
40 39 S(6) 37 36 35 34 33 32 31
21 22 23 24 25 26 27 28 29 30
20 19 18 17 S(9) 15 14 13 12 11
Start 2 3 4 5 6 7 8 9 10
Constraints :

● 1 <= die_inputs <= 6

● Number of die inputs >= 0

Input :
First 10 lines contains snake and ladder board where each line has 10 tokens separated
by a space. The tokens can either be integers, Start, End, S(number), L(number) where

● Integer denotes the square number

● Start denotes the left bottom position on the board from where player start the

game

● End denotes the left top position on the board

● S(number) denotes that the current square has a snake that will take you down

to a square number mentioned in the parenthesis.

● L(number) denotes that the current square has a ladder that will take you up to a

square number mentioned in the parenthesis.

Second line contains die_inputs separated by a space.

Output :

Find if the player is possible to reach the End or not, based on die_inputs and the
board. If it is possible, display ‘Possible’ with number of snakes and ladders
encountered during his/her play else display ‘Not possible’ along with number of
snakes, number of ladders and the square where the player’s coin has ended at.

Print all the outputs delimited by a space.

Refer Examples section for better understanding.


Time Limit : 1 secs

Example 1 :

Input :

End S(Start) 98 97 96 95 94 93 92 91

81 82 83 84 L(98) 86 87 88 89 90

80 79 S(46) 77 76 75 74 73 72 71

61 62 63 64 65 66 67 68 69 70

60 59 58 57 56 55 S(25) 53 52 51

41 42 43 L(62) 45 46 47 48 49 50

40 39 38 37 36 35 34 33 32 31

21 22 23 L(74) 25 26 27 28 29 30

20 19 18 17 S(2) 15 14 13 12 11

Start 2 3 4 5 6 7 8 9 10

54241

Output :

Not possible 1 0 2

Explanation :
Based on die inputs, the player moves from start and goes to number 5 first on board
then to 9, 11, 15 and finally to S(2) i.e., square numbered 16. Now player encountered
snake which takes him to square numbered 2. So, output is ‘Not possible’, as the player
couldn’t reach the End and the number of snakes and ladders encountered during
his/her play are one and zero respectively.

Example 2 :

Input :

End 99 98 S(7) 96 95 94 93 92 91

81 82 L(99) 84 85 86 87 88 89 90

80 79 78 77 76 75 74 73 72 71

61 S(22) 63 64 65 66 67 68 69 70

60 59 58 S(14) 56 57 54 53 52 51

41 42 43 44 45 46 L(80) 48 49 50

40 39 38 37 36 35 34 33 32 31

21 22 23 L(63) 25 26 27 28 29 30

20 19 S(2) 17 16 15 14 13 12 11

Start 2 3 4 5 6 7 8 9 10

66654366656431

Output :

Possible 1 2
Explanation :

Based on the die inputs, the player encountered 1 snake and 2 ladders and was able to
reach the End.

Question – : Juan Marquinho is a geologist and he needs to count rock samples in


order to send it to a chemical laboratory. He has a problem: The laboratory only accepts
rock samples by a range of its size in ppm (parts per million).

Juan Marquinho receives the rock samples one by one and he classifies the rock
samples according to the range of the laboratory. This process is very hard because the
number of rock samples may be in millions.

Juan Marquinho needs your help, your task is to develop a program to get the number
of rocks in each of the ranges accepted by the laboratory.

Input Format: An positive integer S (the number of rock samples) separated by a blank
space, and a positive integer R (the number of ranges of the laboratory); A list of the
sizes of S samples (in ppm), as positive integers separated by space R lines where the
ith line containing two positive integers, space separated, indicating the minimum size
and maximum size respectively of the ith range.

Output Format: R lines where the ith line contains a single non-negative integer
indicating the number of the samples which lie in the ith range.

Constraints:

● 10 <= S <= 10000


● 1 <= R <= 1000000
● 1<=size of Sample <= 1000

Example 1

● Input: 10 2
● 345 604 321 433 704 470 808 718 517 811
● 300 350
● 400 700

Output: 2 4

Explanation:

There are 10 samples (S) and 2 ranges ( R ). The samples are 345, 604,811. The
ranges are 300-350 and 400-700. There are 2 samples in the first range (345 and 321)
and 4 samples in the second range (604, 433, 470, 517). Hence the two lines of the
output are 2 and 4

Example 2

● Input: 20 3
● 921 107 270 631 926 543 589 520 595 93 873 424 759 537 458 614 725 842
575 195
● 1 100
● 50 600
● 1 1000

Output: 1 12 20

Explanation:

There are 20 samples and 3 ranges. The samples are 921, 107 195. The ranges are
1-100, 50-600 and 1-1000. Note that the ranges are overlapping. The number of
samples in each of the three ranges are 1, 12 and 20 respectively. Hence the three lines
of the output are 1, 12 and 20.

Question : In this 3 Palindrome, Given an input string word, split the string into exactly 3
palindromic substrings. Working from left to right, choose the smallest split for the first
substring that still allows the remaining word to be split into 2 palindromes. Similarly,
choose the smallest second palindromic substring that leaves a third palindromic
substring.

If there is no way to split the word into exactly three palindromic substrings, print
“Impossible” (without quotes). Every character of the string needs to be consumed.

Cases not allowed –

● After finding 3 palindromes using above instructions, if any character of the


original string remains unconsumed.
● No character may be shared in forming 3 palindromes.

Constraints

● 1 <= the length of input sting <= 1000

Input

● First line contains the input string consisting of characters between [a-z].

Output

● Print 3 substrings one on each line.

Time Limit

Examples

Example 1

Input
nayannamantenet

Output

nayan

naman

tenet

Explanation

● The original string can be split into 3 palindromes as mentioned in the output.
● However, if the input was nayanamantenet, then the answer would be
“Impossible”.

Example 2

Input

aaaaa

Output

aaa

Explanation

● The other ways to split the given string into 3 palindromes are as follows –
● [a, aaa, a], [aaa, a, a], [aa, aa, a], etc.
● Since we want to minimize the length of the first palindromic substring using left
to right processing, the correct way to split is [a, a, aaa].
Example 3

Input

aaaabaaaa

Output

aaabaaa

Explanation

● The other ways to split the given string into 3 palindromes are as follows –
● [aaaa, b, aaaa], [aa, aabaa, aa], etc.
● Since we want to minimize the length of the first palindromic substring using left
to right processing, the correct way to split is [a, aaabaaa, a].

Question : Given an array of integers A, and an integer K find number of happy


elements.

Element X is happy if there exists at least 1 element whose difference is less than K i.e.
an element X is happy if there is another element in the range [X-K, X+K] other than X
itself.

Constraints

● 1 <= N <= 10^5


● 0 <= K <= 10^5
● 0 <= A[i] <= 10^9

Input
● First line contains two integers N and K where N is size of the array and K is a
number as described above.
● Second line contains N integers separated by space.

Output

● Print a single integer denoting the total number of happy elements.

Example 1

Input

63

5 5 7 9 15 2

Output

Explanation

Other than number 15, everyone has at least 1 element in the range [X-3, X+3]. Hence
they are all happy elements. Since these five are in number, the output is 5.

Example 2

Input

32

135

Output

3
Explanation

All numbers have at least 1 element in the range [X-2, X+2]. Hence they are all happy
elements. Since these three are in number, the output is 3.

Possible Solution

Input:

32

135

Question : Dr. Vishnu is opening a new world class hospital in a small town designed to
be the first preference of the patients in the city. Hospital has N rooms of two types –
with TV and without TV, with daily rates of R1 and R2 respectively.

However, from his experience Dr. Vishnu knows that the number of patients is not
constant throughout the year, instead it follows a pattern. The number of patients on any
given day of the year is given by the following formula –

(6-M)^2 + |D-15| ,

where M is the number of month (1 for jan, 2 for feb …12 for dec) and D is the date
(1,2…31).

All patients prefer without TV rooms as they are cheaper, but will opt for with TV rooms
only if without TV rooms are not available. Hospital has a revenue target for the first
year of operation. Given this target and the values of N, R1 and R2 you need to identify
the number of TVs the hospital should buy so that it meets the revenue target. Assume
the Hospital opens on 1st Jan and year is a non-leap year.

Constraints
● Hospital opens on 1st Jan in an ordinary year
● 5 <= Number of rooms <= 100
● 500 <= Room Rates <= 5000
● 0 <= Target revenue < 90000000

Input Format

● First line provides an integer N that denotes the number of rooms in the hospital
● Second line provides two space-delimited integers that denote the rates of rooms
with TV (R1) and without TV (R2) respectively
● Third line provides the revenue target

Output

● Minimum number of TVs the hospital needs to buy to meet its revenue target. If it
cannot achieve its target, print the total number of rooms in the hospital.

Test Case

Example-1 :

Input

20

1500 1000

7000000

Output

14

Explanation
Using the formula, the number of patients on 1st Jan will be 39, on 2nd Jan will be 38
and so on. Considering there are only twenty rooms and rates of both type of rooms are
1500 and 1000 respectively, we will need 14 TV sets to get revenue of 7119500. With
13 TV sets Total revenue will be less than 7000000

Example-2 :

Input

10

1000 1500

10000000

Output

10

Explanation

In the above example, the target will not be achieved, even by equipping all the rooms
with TV. Hence, the answer is 10 i.e. total number of rooms in the hospital.

Question : The parcel section of the Head Post Office is in a mess. The parcels that
need to be loaded to the vans have been lined up in a row in an arbitrary order of
weights. The Head Post Master wants them to be sorted in the increasing order of the
weights of the parcels, with one exception. He wants the heaviest (and presumably the
most valuable) parcel kept nearest his office.

You and your friend try to sort these boxes and you decide to sort them by interchanging
two boxes at a time. Such an interchange needs effort equal to the product of the
weights of the two boxes.

The objective is to reposition the boxes as required with minimum effort.


Input Format:

● The first line consists of two space-separated positive integers giving the number
of boxes (N) and the position of the Head Post Masters office (k) where the
heaviest box must be.
● The second line consists of N space-separated positive integers giving the
weights of the boxes. You may assume that no two weights are equal

Output Format:

● The output is one line giving the total effort taken to get the boxes in sorted order,
and the heaviest in position k.

Constraints:

N<=50 and Weights <= 1000

Sample Input 1:
52
20 50 30 80 70
Sample Output 1:
3600

Question -: Imagine you are an MLA of a district and there are N number of villages in
your constituency.

Your job is to vaccinate all the people in your constituency in minimum amount of time.
There are two centres where vaccination is going on. First centre takes m1 minutes as
average time for vaccinating one person and second centre takes m2 minutes as
average time.
Population of every village is known to you prior to the vaccination drive. Schedule all
the villagers to any centre such that overall time for vaccinating all the people of all the
villages will be minimum. Assume that there is no wait time in between vaccinating two
people. Also, people belonging to the same village will need to be vaccinated in the
same centre.

For example:

First centre takes 2 min as average time

Second centre takes 4 min as average time

Population data of 3 villages is known: 10 30 20

Number of people in 3 villages is given:

v1 = 10, v2 = 30, v3 = 20

Consider if schedule is drawn by distributing equal number of people to both centres,


then

First centre: 10 20 total time = (10 + 20) * 2 = 60 min

Second centre: 30 total time = (30) * 4 = 120 min

Hence,

minimum time required to vaccinate all the people will be = 120 min.

i.e., Maximum of time taken in first centre or second centre.

But if it is scheduled like this:

Constraints

0 < m1, m2 <= 20


0 < N < 10 ^ 3

0 < Population of village <= 100

Input

First line contains an integer m1 which is average time in minutes taken for vaccination
by the first centre

Second line contains an integer m2 which is average time in minutes taken for
vaccination by the second centre

Third line contains an integer N which is number of villages in the constituency

Fourth line contains N space delimited integers denoting the population of villages

Output

Single integer value denoting the maximum time taken in minutes to vaccinate all
villagers from all villages in your constituency

Time Limit (secs)

Examples

Example 1

Input

5
10 50 20 30 40

Output

180

Explanation:

Given the data of centre1 and centre2:

First centre takes 2 min as average time. Second centre takes 3 min as average time.
Your constituency has 5 villages.

Number of people in each of the 5 villages is given: 50 10 20 30 40

v1 = 50, v2 = 10, v3 = 20, v4 = 30, v5 = 40

If schedule looks like this:

First centre: 10 50 total time = (10 + 50) * 2 = 120 min

Second centre: 30 40 20 total time = (20 + 40 + 20) * 3 = 240 min

Minimum time required to vaccinate all the people will be = 240 min

But if the schedule is drawn like this:

First centre: 10 30 50 total time = (10 + 30 + 50) * 2 = 180 min

Second centre: 40 20 total time = (40 + 20) * 3 = 180 min

Minimum time required to vaccinate all the people will be = 180 min

Example 2

Input
1

100 90 70

Output

180

Explanation:

Given the data of centre1 and centre2:

First centre takes 1 min as average time. Second centre takes 2 min as average time.
There are 3 villages in your constituency.

Number of people in each of the 3 village is given: 100 90 70

v1 = 100, v2 = 90, v3 = 70

If schedule looks like this:

First centre: 100 90 total time = (100 + 90) * 1 = 190 min

Second centre: 70 total time = (70) * 2 = 140 min

Minimum time required to vaccinate all the people will be = 190 min

But if schedule is drawn like this:

First centre: 100 70 total time = (100 + 70) * 1 = 170 min

Second centre: 90 total time = (90) * 2 = 180 min


Minimum time required to vaccinate all the people will be = 180 min. Hence the output is
180.

Question -: This is a resource to task allocation problem. There are two types of tasks
viz. one that the human needs to do and the other which as amenable to machine
processing. A human-oriented task cannot be performed by machine and vice-versa.
Also, assume that all human workers are equally efficient. So, the same task will be
performed in the same time, irrespective of which human worker does that task.
Given the number of workers, number of tasks and time taken to finish each task,
compute which task was finished at what time and which worker performed that task.
Some rules and conventions that need to be adhered to while doing task allocation to
workers are as below.

● Machine processing for any task takes 0 minutes

● Time taken (in minutes) by a human worker to complete a particular task is

mentioned in the input

● Workers and tasks are identified by their suffixes i.e., Worker 1 is W1 and Task 1

is T1.

● Task information is provided in the input in form of a tuple that consists of 3

things viz. <Taskname, TaskType, CompletionTime> where

○ Taskname will be of form T1, T2, T3 etc.

○ TaskType will be a keyword which can be either {Manual, Machine}

○ CompletionTime is a number denoting time in minutes needed for manual

tasks and 0 for machine-bound tasks

● A worker will still need to be assigned even if a task can be done only by the

machine
● Tasks need to be processed in sequential order i.e., T1 has to be allocated

before T2

● Initially when all workers are free, worker Wi will be allocated task before worker

Wi+1 i.e., in general a worker with lower suffix will be assigned to the task before

worker with a higher suffix.

● If one or more workers are available at the same time, then the worker who

processed lower suffix task will get the next task allocated prior to the other

workers.

● Assume the switching time from one task to another as 0 min

Refer Examples section for better understanding of how tasks are allocated to workers

Compute which task was completed at what time and which worker performed that task.
The output specifications provide information on how output should be printed.

Constraints :

● 1 <= N <= 10

● 0 < M <= 100

Input :

First line contains an integer N which denotes the number of workers

Second line contains an integer M which denotes the number of tasks

Next M lines contain a task information tuple as described in the previous section.
Output :

Output should adhere to the following specifications.

● Each output should be a tuple comprising of 3 pieces of information viz <Worker

name, task name, time at which the task was completed>

● The tuple should be first sorted in ascending order of completion time and then in

ascending order of task name

For better understanding refer to the Examples section.

Time Limit : 1 secs

Example 1 :

Input :

T1 Machine

T2 Manual 5

T3 Machine

T4 Machine

Output :
W1 T1 0

W3 T3 0

W1 T4 0

W2 T2 5

Explanation :

Here, N=3 denotes number of workers W1, W2, W3.

First, Workers W1, W2, W3 will be assigned with tasks T1, T2, T3 at the 0th minute,
respectively.

Here, T1 is Machine-bound so it will be completed in 0th minute and then W1 will be


available for the remaining tasks. T2 is Manual and it will be completed in 5th minute.
So, W2 will be free after 5 minutes. T3 again is Machine-bound so it will be completed in
0th minute and then W3 will be available for remaining tasks.

Now, Workers available for pending tasks in 0th minute = {W1, W3}

Here, W1 will be assigned with next pending task first, because the worker W1
completed task with lower suffix (T1) than W3 (T3).

W1 will be assigned the next task T4 at 0th minute which is a Machine-bound task
which will also be completed in 0th minute.

As the number of pending tasks are zero, no further assignment takes place.

W2 will complete T2 at the end of 5th minute. Hence,

First line of output is “W1 T1 0”, where T1 is the task completed by Worker W1 0th
minute.
Second line of output is “W3 T3 0”, where T3 is the task completed by Worker W3 in 0th
minute.

Third line of output is “W1 T4 0”, where T4 is the task completed by worker W1 in 0th
minute.

Fourth line of output is “W2 T2 5”, where T2 is the task completed by W2 in 5th minute.

So, the output is as follows:

W1 T1 0

W3 T3 0

W1 T4 0

W2 T2 5

Example 2 :

Input :

T1 Machine

T2 Machine

T3 Machine

T4 Machine

Output :
W1 T1 0

W2 T2 0

W3 T3 0

W1 T4 0

Explanation :

Given N = 3 denotes number of workers W1, W2 and W3

First, Workers W1, W2, W3 will be assigned with tasks T1, T2, T3 in 0th minute,
respectively.

Here, T1, T2 and T3 are Machine-bound and will be completed in 0th minute, after
completion of these tasks W1, W2, W3 will be available for the remaining tasks.

Since W1 is the first worker in the list of available workers at 0th minute. W1 will pick up
the 4th task and will complete it in 0th minute, because it is also a machine bounded
task.

Hence the output is as follows:

W1 T1 0

W2 T2 0

W3 T3 0

W1 T4 0

Questions - You are given N comma-separated Strings. You need to form all possible
legal subsets of these N strings. These subsets will be a combination of zero or more of
these N Strings After forming the subsets, they will be ranked in a particular onder. The
legal subset formation and ranking logic is as described below
● Rank 1 will always be an empty set

● Next N ranks will be the N Strings that appear in the order they are provided in

the input

● After N + 1 ranks, you need to combine N strings such that all legal combinations

are formed

● Legal combination simply means that while combinations are formed, the string

that appears to the left of a particular string in the input, can never appear to the

right of that particular string, when subsets are formed

● A subset with less elements will be ranked higher than a subset with more

elements (NOTE-Rank 1 is higher than rank 2)

● Refer Example 2 to get a better understanding of how subsets are formed and

ranked

● It is guaranteed that

● N>=1

● All N strings are unique

Example: you are having an input string “aa,cc,bb” in this string we can see we have
three strings which are comma separated. Now from this group of string we have to
create all possible subset of strings. 8 subsets can be formed from these strings. And
they are as follows:

1. {}

2. {aa}

3. {cc}
4. {bb}

5. {aa,}

Note: here we can see the ranks given to the subsets are first by size i.e., the subset
with lesser number of strings is ranked higher than the subset with higher size. If the
subsets have equal number of strings then, the combination which is formed earlier (by
virtue of combining strings in order they appear in input), gets a higher rank.

For example, rank of subset (aa,cc) > rank of (aa,bb) because string cc is appearing
prior to string bb in the input. Similarly, rank of (cc) > rank of (bb).

You are provided one rank R and for that you have to print the Rth subset from all legal
subsets.

Constraints:

0<N<=10^2

0<R<=10^18

Input

First line contains an integer N which is number of strings in group.

Second line contains an integer R, for which you have to find Rth subset from all legal
subsets.

Third line contains N comma-separated strings basis which the subsets should be
formed

Output:

From all possible legal subsets find the subset whose rank is R
Time Limit (secs)
1

Input

a,b

Output

a,b

Explanation:

Given that N = 2, given

Second line: Rank to be find: 4th

Third line: Given group of strings: a,b

Possible subsets & Rank

{}-1

{a} -2

{b}-3

{a, b}-4

Output – a,b (4th rank corresponds to a,b)

Question - In a Conference ,attendees are invited for a dinner after the conference.The
Co-ordinator,Sagar arranged around round tables for dinner and want to have an
impactful seating experience for the attendees.Before finalizing the seating
arrangement,he wants to analyze all the possible arrangements.These are R round
tables and N attendees.In case where N is an exact multiple of R,the number of
attendees must be exactly N//R,,If N is not an exact multiple of R, then the distribution of
attendees must be as equal as possible.Please refer to the example section before for
better understanding.
For example, R = 2 and N = 3
All possible seating arrangements are
(1,2) & (3)
(1,3) & (2)
(2,3) & (1)
Attendees are numbered from 1 to N.

Input Format:

● The first line contains T denoting the number of test cases.


● Each test case contains two space separated integers R and N, Where R
denotes the number of round tables and N denotes the number of attendees.

Output Format:

Single Integer S denoting the number of possible unique arrangements.

Constraints:

● 0 <= R <= 10(Integer)


● 0 < N <= 20 (Integer)

Sample Input 1:

1
25

Sample Output 1:

10

Explanation:

R = 2, N = 5

(1,2,3) & (4,5)

(1,2,4) & (3,5)

(1,2,5) & (3,4)

(1,3,4) & (2,5)

(1,3,5) & (2,4)

(1,4,5) & (2,3)

(2,3,4) & (1,5)

(2,3,5) & (1,4)

(2,4,5) & (1,3)

(3,4,5) & (1,2)

Arrangements like

(1,2,3) & (4,5)

(2,1,3) & (4,5)

(2,3,1) & (4,5) etc.


But as it is a round table,all the above arrangements are same.

THANK YOU ❤️

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy