Infosys Reference Coding Questions
Infosys Reference Coding Questions
Infosys Reference Coding Questions
A shopkeeper has a sale to complete and has arranged the items being sold in an array.
Starting from the left, the shopkeeper sells each item at its full price minus the price of the
first lower or equal priced item to its right. If there is no item to the right that costs less
than or equal to the current item’s price, the current item is sold at full price.
● The items 0 and 1 are each discounted by 1 unit, the first equal or lower price to
the right
● Item 2, priced 1 unit sells at full price because there are no equal or lower priced
items to the right
● The next item, item 3 at 2 units, is discounted 2 units, as would the item 4 at 4 units
● The final item 5 at 2 units must be purchased at full price because there are no
lower prices to the right
The total cost is 1+2+1+0+2+2=8 units. The full price items are at indices [2, 5] using 0
based indexing.
Input Format
The first line of input contains T, the number of test cases. In the next T lines:
Output Format
Given an array of n elements and a number m, we need to find all distinct pairs existing in
the array whose pair sum is divisible by the given number m.
Print the total number of such pairs. Distinct pairs means (1, 2) and (2, 1) are the same, i.e.,
ordering of the pairs doesn't matter.
Example Input
10 9 4 5 7 2 8 20 21
15
Output
Explanation
The following pairs give a sum divisible by 15: 10,5, 10,20, 9,21, 7,8
Input Format
The first line of input contains T, the number of test cases. In the next 2*T lines:
Output Format
Mary has an n x m piece of paper that she wants to cut into 1 x 1 pieces according to the
following rules:
● She can only cut one piece of paper at a time, meaning she cannot fold the paper or
layer already-cut pieces on top of one another.
● Each cut is a straight line from one side of the paper to the other side of the paper.
For example, the diagram below depicts the three possible ways to cut a 3 x 2
piece of paper:
Given n and m, find and print the minimum number of cuts Mary must make to cut the
paper into n.m squares that are 1 x 1 unit in size.
Note : you have to write the complete code for taking input and print the result.
Input Format
A single line of two space-separated integers denoting the respective values of n and m.
Constraints
1<=n, m<=109
Output Format
Print a long integer denoting the minimum number of cuts needed to cut the entire paper
into 1 x 1 squares.
Sample Input
3 1
Sample Output
Explanation
Mary first cuts the 3 x 1 piece of paper into a 1 x 1 piece and a 2 x 1 piece. She then
cuts the 2 x 1 piece into two 1 x 1 pieces:
Because it took her two cuts to get n x m=3 pieces of size 1 x 1, we print 2 as our
answer.
Problem Statement - 4:
Alice is a kindergarten teacher. She wants to give some candies to the children in her class.
All the children sit in a line and each of them has a rating score according to his or her
performance in the class. Alice wants to give at least 1 candy to each child. If two children
sit next to each other, then the one with the higher rating must get more candies. Alice
wants to minimize the total number of candies she must buy.
For example, assume her students' ratings are [4, 6, 4, 5, 6, 2]. She gives the
students candy in the following minimal amounts: [1, 2, 1, 2, 3, 1]. She must buy a
minimum of 10 candies.
Task
You are given an integer n the number of children in the class and an array arr of integers
representing the ratings of each student. Write a program to print the minimum number
of candies Alice must buy.
Input Format
● The first line contains an integer, t denoting the number of test cases.
● The first line of each test case contains an integer, n denoting the size of the arr.
● The second line of each test case contains n space-separated integers describing
the elements of array,where arr[i] indicates the rating of the student at position i.
Constraints
● 1<=n<=105
● 1<=arr[i]<=105
Output Format
Output a single line containing the minimum number of candies Alice must buy.
Sample Input 0
1 2 2
Sample Output 0
Explanation 0
Here 1, 2, 2 is the rating. Note that when two children have equal rating, they are allowed
to have a different number of candies. Hence optimal distribution will be 1, 2, 1.
Sample Input 1
10
2 4 2 6 1 7 8 9 2 1
2 4 3 5 2 6 4 5
Sample Output 1
19
12
Explanation 1
Problem Statement - 5:
Given an array of n numbers find the largest difference in the starting and ending indexes
of the elements of all possible strictly decreasing sub-arrays in the array.
Output : 4
Explanation:
Here for element 1 the decreasing sub-array can be mapped till 18. It is 18, 10, 7, 3, 1.
Similarly, for 3 it can be mapped till 18 which is 18, 10, 7, 3. For 7 it can be mapped till 18
which is 18, 10, 7.
For 10 it can be mapped till 18 which is 18, 10. For 18 there is no continuous decreasing
sub-sequence.
Input Format
Problem Statement - 6:
There are n people in the party. Each person is wearing T-shirts with a number written on
the T-shirts. The numbers on the T-shirts can be unique or the same. In three turns 3
people leave the party one at a time. You are provided with the people remaining in the
party after every turn. You need to print the T-shirt number of people who left the party in
Input Format:
● The first line contains an integer, t denoting the number of test cases.
● Every test case contains five lines where :
● 1st line contains an integer n denoting number of people in the party initially.
● 2nd line contains n space separated integers denoting the T-shirt numbers of all
people who are in the party initially.
● 3rd line contains n-1 space separated integers denoting the T-shirt numbers of
remaining people who are in the party after 1st turn.
● 4th line contains n-2 space separated integers denoting the T-shirt numbers of
remaining people who are in the party after 2nd turn.
● 5th line contains n-3 space separated integers denoting the T-shirt numbers of
remaining people who are in the party after 3rd turn.
Output Format:
For every test case print 3 space separated integers denoting the T-shirt number of people
who left the party in the order they left. Print a new line at the end of each test case.
Constraints
● 3<=n<=105
● -105<=arr[i]<=105
Sample Input 0
11 5 1 2 7 3 1
11 5 2 7 3 1
5 2 7 3 1
5 7 3 1
Sample Output 0
1 11 2
Explanation 0
We can observe that one of the two 1's is missing after turn 1, similarly 11 is missing after
Problem Statement - 7:
Given a gold mine of m x n dimensions.
1. Each field in this mine contains a positive integer which is the amount of gold
present at that position.
2. Initially the miner is at first column but can be at any row.
3. He can move only int three directions: right, right-up and right-down. That is, east,
northeast and southeast from a given cell
Starting from any row (but always the first column), find out the maximum amount of gold
Input format
The first line contains T, the number of test cases. The following T lines contain 2 integers m
and n which tell us the rows and columns respectively. This is followed by all the 2D array
Output format
● For each test case, output the maximum number of gold collected
Example Input #1
4 4 1 3 1 5 2 2 4 1 5 0 2 3 0 6 1 2
Output #1
16
Explanation:
(2,0) -> (1,1) -> (1,2) -> (0,3) OR (2,0) -> (3,1) -> (2,2) -> (2,3)
Problem Statement - 8:
We have to choose the shortest substring from sentence such that all the elements of
Input format
● The first line contains T, the number of test cases. For each T line after that:
● The next line contains n space-separated words which are part of the seed
Example Input
2
Talk is cheap. Show me the code. Some talk is not cheap. show the code
The world is here and there. this is a life full of ups and downs. life is world.
Output
Problem Statement - 9:
The janitor of a high school is insanely efficient. By the end of the day, all of the waster
from the trash cans in the school has been shifted into plastic bags which can carry waster
weighting between 1.01 pounds and 3.00 pounds.
All of the plastic bags must be dumped into the trash cans outside the school. The janitor
can carry at most 3.00 pounds at once.
One trip is described as selecting a few bags which together don't weigh more than 3.00
pounds, dumping them in the outdoor trash can and returning to the school. The janitor
wants to make minimum number of trips to the outdoor trash can.
Given the number of plastic bags, n and the weights of each bag, determine the minimum
number of trips if the janitor selects bags in the optimal way.
For example, given n=5, plastic bags weighing weights [1.01, 1.99, 2.5, 1.5, 1.01],
the janitor can carry all of the trash out in 3 trips: [1.01+1.99, 2.5, 1.5+1.01]
Constraints
As part of his training, Rock Lee will run on a trail of length n labeled from 1 to n. Usually,
he runs m segments around the trail every day. His coach makes him start his run from 1
According to his coach's instructions, in the ith segment, Rock Lee has to start his run
from spot[i] and end at spot[i+1] (where 0 <= i < m-1). The trail is circular, so if
spot[i+1] < spot[i], he has to run past the last marker and continue running from
On each segment, Rock Lee visits each marker from beginning to end of the segment. Find
If there are multiple markers with the same number of visits, choose the smallest one.
Input Format
N M
spot[1..n]
Two integers N and M denoting the length of the trail, and the number of segments Rock
Then follow M space-separated integers denoting the ending points for each of the M
segments.
Sample Input
3 3
3 3 2
Sample Output
Explanation
For example, given a track or length N = 3, and M = 3 segments to run ending at spot = [3,
3, 2],
Write a program to determine whether a number N is equal to the sum of its proper
positive divisors (excluding the number itself).
Input format
Output format
Print YES, if N is equal to the sum of its proper positive divisors else print NO.
Constraints
1 ≤ T ≤ 100
1 ≤ N ≤ 109
Sample Input
3
28
Sample Output
YES
NO
YES
Explanation
6 = 1 + 2+ 3, so it is perfect.
5 != 1 , so it is not perfect.
28 = 1 + 2 + 4 + 7 + 14, so it is perfect.
You are given an integer array A of size N and an integer M. You have to distribute the
elements of array A into M groups such that the maximum sum of elements in each group
is minimum and the elements of array A allotted to any group is contiguous.
Write a program to determine the maximum sum of elements among all the groups.
Input format
Output format
For each test case, print the maximum sum of elements among all the groups in a new line.
Constraints
● 1≤T≤100
● 1≤M≤N≤10^4
● 1≤Ai≤10^5
Sample Input
5 3
1 2 3 4 5
Sample Output
Explanation
Given an array of n elements, find out all the crests and troughs along with their lengths.
Find the absolute difference between the indexes of longest and shortest crests as well as
troughs and display the maximum of both.
● An element is said to be crest if both it's previous and next elements are less than
the current element.
● An element is said to be trough if both it's previous and next elements are greater
than the current element.
Note: The first and last elements are neither crests nor troughs.
In case of multiple occurrences of shortest crest/trough consider the left most one as
shortest and right most one as longest.
Try to finish the task using O(1) extra space and traversing the array only once.
Input Format
● The first line contains an integer, t denoting the number of test cases.
● The first line of each test case contains an integer, n denoting the size of the arr.
● The second line of each test case contains n space-separated integers describing
the elements of array.
Constraints
● 4<=n<=109
● 0<=arr[i]<=n
Output Format
For every test case print the required output in a new line.
Sample Input 0
3 6 2 8 9 5 10 1
Sample Output 0
Explanation 0
The trough with minimum length(length-->6-2=4) exists at index 2 (troughs with length 4
exists at two indexes 2 and 5, but take trough at index 2 as shortest trough as it is left
most).
Print 2 as (difference between indexes of longest and shortest crests) 6-4 > 2-2 (difference
between indexes of longest and shortest troughs).
You are given Q queries and in each query, there are two numbers L and R. You have to
calculate the number of strong primes present in the range L and R inclusive.
Note
A strong prime is a prime number that is greater than the arithmetic mean of the nearest
prime above and below. Algebraically, a prime P said to be strong if
where n is their index in the ordered set of prime numbers, where Pi denotes the ith. prime.
Input format
● The first line of the Input contains an integer Q denoting the number of Queries.
● Then Q lines follow each containing two numbers L and R.
Output format
● For each query, print the number of Strong primes present in the range L to R
inclusively. The answer to each test case should come in a new line.
Constraint:
● 1<=Q<=105
● 1<=L<=R<=106
Sample Input:
10 20
20 30
30 50
Sample Output:
2
Explanation:
● For the range 10 to 20, there are 2 strong primes (11, 17).
● For the range 20 to 30, there is 1 strong prime (29).
● For the range 30 to 50, there are 2 strong primes (37, 41).
You are given a set of numbers. Using the binary representation of all the numbers from L
to R and have to count the special ones on the list.
Important notes:
For example, the binary representation of 4 is 100. In this, 1st and 2nd bits are not set
while 3rd bit is set. Also, this 3rd bit is a special one because 3 is a prime number.
Input format:
● First line of input contains a single integer T denoting the number of test cases
● T lines follow each containing two space-separated integer L and R
Output format:
For each test case, print a single integer denoting the number of special 1's on the list.
Constraints:
You are given two numbers L and R. You are required to find the total number of lucky
numbers present in between L and R(inclusive).
Note: A number is said to be lucky if the factors of that number are not repeated (power to
the factors can only be 1).
Input format:
1. The first line of the input contains an integer T denoting the number of test cases.
2. Each test case is described by a line containing two space - separated integers L and
R
Output format:
Sample Input:
2 4
Sample output:
Explanation:
In the sample case, 2 and 3 are prime itself and so are lucky numbers, Prime factorization
of 4 is 22 and since 2 is repeated it is not a lucky number. So the number of lucky numbers
is 2.
Bob and Alice start painting. There are N number of painting kits. The ith kit has a strength
of A[i].
They need to select kits. Bob got the first chance picked the minimum number of kits such
that he can make the painting quickly. The remaining kits are picked by Alice.
Bob can finish his painting before Alice, if and only if the total strength of his kits, is
greater than Alice's. Find the minimum number of kits for Bob.
Input format
Output format
Constraints