Infosys Coding Sheet
Infosys Coding Sheet
Sample Input
G
b[] = {15, 16, 17, 18}
IN
Sample Output
D
b[] = {11, 12, 13, 14}
O
2. Write a Program to sort a string of characters
C
Sample Input
E
s=”prepbytes”
IM
Sample Output
s=”beepprsty”
PR
Sample Input
s=”prepbytes”
Sample Output
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
1
PRIME CODING PRIME CODING PRIME CODING
4. Write a program to multiply two matrices and print the result through another
matrix.
Sample Input
G
m1[m][n] = { {1, 1}, {2, 2} }
m2[n][p] = { {1, 1}, {2, 2} }
IN
Sample Output
D
O
5. Given a string find the next permutation of the given string in C++.
C
Sample Input
s=”dcd”
E
Sample Output
IM
s=”ddc”
PR
6. Write a program to find the area of the incircle of a right angles triangle.
Sample Input
P = 5, B = 12, H = 13
Sample Output
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
2
PRIME CODING PRIME CODING PRIME CODING
12.56
7. Write a program that will find the missing characters that are needed to make
the string a panagram.
Sample Input
G
welcome to prepbytes
Sample Output
IN
adfghijknquvxz
D
O
8. Write a program that converts the given temperature of Fahrenheit into
celsius.
C
Sample Input
0
E
Sample Output
IM
32
PR
9. Write a program that will find the sum of all the prime numbers between 1 and
N.
Sample Input
10
Sample Output
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
3
PRIME CODING PRIME CODING PRIME CODING
17
10. Write a program to make the largest number from the digits of the array.
Sample Input
G
Sample Output
IN
998764543431
D
11. Given an array form a triangle such that the last row of the triangle contains
O
all the elements of the array and the row above it will contain the sum of two
elements below it.
C
Sample Input
Sample Output
IM
81
40 41
PR
21 19 22
11 10 9 13
4 7 3 6 7
12. Given the price of the stock on each day find the maximum profit you can
earn by selling them.
Sample Input
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
4
PRIME CODING PRIME CODING PRIME CODING
Sample Output
865
13. You are given a matrix that contains only 0 and 1 find the maximum size of a
G
rectangle that contains only 1.
Sample Input
IN
0 1 1 0
1 1 1 1
D
1 1 1 1
1 1 0 0
O
Sample Output
C
8
E
IM
14. Given the coordinates of the endpoints of two rectangles find whether they
overlap each other or not.
15. You are given two strings to find whether we can convert one string to
PR
Sample Input
Sample Output
Yes
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
5
PRIME CODING PRIME CODING PRIME CODING
Input Output
statistics 3
G
Hack the game 3
IN
Complete the function: GetUniqueLetter ()
He chose two integers, N and K, and decided to write down in a notebook all
D
integer arrays of length K (in the form a[1], a[2],…, a[K]), where every number a[i]
is divisible by a[i] (where 1 < i <= K).
O
Because the answer could be huge, print it modulo 10000.
Input:
C
//The first line contains an integer, n, which represents
the arrays' maximum possible value.
E
Input Output
PR
2
1
2
2
2
3
3
2 5
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
6
PRIME CODING PRIME CODING PRIME CODING
How many pieces can you possibly get in total? You have an
amazing N-length string S.
Input Format:
//S:: STRING
//The first line includes a string, S, which represents t
he string.
G
length(S) :: 1 -> 2 * 10^5
IN
zzzzz
ababcc
abccdcabacda 2
D
O
How do you swap two arrays in C++?
Answer:
C
Sample Input
A[ ] = {21, 22, 23, 24}
E
Sample Output
Sample Input
Output
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
7
PRIME CODING PRIME CODING PRIME CODING
988766752452
Sample Input
G
Output
Smallest Number: 1
IN
Largest Number: 98
D
Find the next permutation of the given string in C++
O
Answer:
Sample Input
C
s=”dcd”
Output
E
Answer:
Sample Input:
123
456
789
Output:
Rotated Matrix:
741
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
8
PRIME CODING PRIME CODING PRIME CODING
852
963
Sample Input: “A quick movement of the enemy will jeopardize six gunboats”
G
Output
IN
“Missing characters: flr”
D
How do you find the number of unique characters in a given
string? Write a program
Answer:
O
C
Sample Input: “Hello, World!”
Output
Sample Input:
a)
12
34
b)
43
21
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
9
PRIME CODING PRIME CODING PRIME CODING
Output
-3 -1
1 3
G
A:
IN
12
34
D
B:
56
78
O
Output
C
The output of multiplying the given matrices A and B is:
19 22
E
43 50
IM
a Program.
Answer:
Sample Input: 29
Output
Problem Statement:
While playing an RPG game, you were assigned to complete one of the hardest
quests in this game. There are n monsters you’ll need to defeat in this quest. Each
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
10
PRIME CODING PRIME CODING PRIME CODING
monster i is described with two integer numbers – poweri and bonusi. To defeat
this monster, you’ll need at least poweri experience points. If you try fighting this
monster without having enough experience points, you lose immediately. You will
also gain bonusi experience points if you defeat this monster. You can defeat
monsters in any order.
The quest turned out to be very hard – you try to defeat the monsters but keep
losing repeatedly. Your friend told you that this quest is impossible to complete.
Knowing that, you’re interested, what is the maximum possible number of
G
monsters you can defeat?
(Question difficulty level: Hardest)
IN
Input:
The first line contains an integer, n, denoting the number of monsters. The
D
next line contains an integer, e, denoting your initial experience.
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
11
PRIME CODING PRIME CODING PRIME CODING
Problem Statement:
Your birthday is coming soon and one of your friends, Alex, is thinking about a gift
for you. He knows that you really like integer arrays with interesting properties.
He selected two numbers, N and K and decided to write down on paper all integer
arrays of length K (in form a[1], a[2], …, a[K]), where every number a[i] is in range
from 1 to N, and, moreover, a[i+1] is divisible by a[i] (where 1 < i <= K), and give
you this paper as a birthday present.
Alex is very patient, so he managed to do this. Now you’re wondering, how many
G
different arrays are written down on this paper?
IN
Since the answer can be really large, print it modulo 10000.
Input:
The first line contains an integer, n, denoting the maximum possible value in
D
the arrays. O
The next line contains an integer, k, denoting the length of the arrays.
All possible arrays are [1, 1], [1, 2], [2, 2].[2, 1] is invalid
22 3
because 1 is not divisible by 2.
IM
32 5 All possible arrays are [1, 1], [1, 2], [1, 3], [2, 2], [3, 3].
Problem Statement:
PR
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
12
PRIME CODING PRIME CODING PRIME CODING
Sample cases:
5
15
6
2 One possible subsequence is: 5 12
5
G
12
1
IN
6
9
17
D
2 2 One possible subsequence is: 2 15
15
5
2
O
7
C
17
16
12
E
17
17
PR
Problem Statement :
You have been given a string S of length N. The given string is a binary string
which consists of only 0’s and ‘1’s. Ugliness of a string is defined as the decimal
number that this binary string represents.
Example:
“101” represents 5.
“0000” represents 0.
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
13
PRIME CODING PRIME CODING PRIME CODING
There are two types of operations that can be performed on the given string.
Initially, you have been given coins equal to the value defined in CASH. Your task
is to minimize the ugliness of the string by performing the above mentioned
operations on it. Since the answer can be very large, return the answer modulo
G
10^9+7.
Note:
IN
You can perform an operation only if you have enough number of coins to
perform it.
D
After every operation the number of coins get deducted by the cost for that
operation. O
Input Format
The first line contains an integer, N, denoting the number of character in the
C
string
The next line contains a string, S, denoting the the binary string
E
The next line contains an integer, CASH, denoting the total number of coins
IM
present initially
Next will contains an integer, A, denoting the cost to swap two characters.
Then the next line contains an integer, B, denoting the cost to flip a character.
PR
Constraints
1<=CASH <=10^5
1<=A<=10^5
1<=B<=10^5
Sample Input 1 :
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
14
PRIME CODING PRIME CODING PRIME CODING
41111712
Sample Output 1 :
Explanation:
6 111011 7 1 3
G
Sample Output 2:
IN
7
Explanation:
First swap 0 with the most significant 1, then use flip twice first on index one and
D
then on index two “111011”=>”0111111″=>”001111″=>”000111″ the value represented is
7.
O
Sample Input 3:
C
6
111011
E
3
IM
Sample Output 3:
PR
Explanation:
Flip the 3 most significant characters to get “000011” : the value represented by
this string is 3.N
Problem Statement :
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
15
PRIME CODING PRIME CODING PRIME CODING
For example:
If A=[2,4,6,8], then khaled can choose the subset [2,4,8] to achieve XOR=(2
XOR 4 XOR 8)=14.
Khaled wants to maximize the XOR of all the elements he chooses. Your task is to
G
help khaled to find the max XOR of a subset that he can achieve by choosing at
most N/2 elements?
IN
Input format:
D
Each line i of the N subsequent lines(where 0<=i<=N) contains an integer
describing Ai.
O
Constraints
C
1<=N<=120
1<=A[i]<=10^6
E
Sample Input 1
2
IM
2
PR
Sample Output 1
Explanation:
N=2, A=[1,2] khaled can choose the subset[2]. The xor of the elements in the
subset is 2. And the number of elements in the subset is 1 which is less than N/2.
Sample Output 2
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
16
PRIME CODING PRIME CODING PRIME CODING
Explanation:
N=4, A=[1,2,4,7] Khaled can choose the subset [7]. The xor of the elements in the
subset is 7, and the number of elements in the subset is 1 which is less than N/2.
Problem Statement :
G
Wael is well-known for how much he loves the bitwise XOR operation, while kaito
is well known for how much he loves to sum numbers, so their friend Resli
IN
decided to make up a problem that would enjoy both of them. Resil wrote down an
array A of length N, an integer K and he defined a new function called Xor- sum
as follows
D
Xor-sum(x)=(x XOR A[1])+(x XOR A[2])+(x XOR A[3])+…………..+(x XOR A[N])
Can you find the integer x in the range [0,K] with the maximum Xor-sum (x) value?
O
Print only the value.
C
Input format
describing Ai.
Constraints
PR
1<=N<=10^5
0<=K<=10^9
0<=A[i]<=10^9
Sample Input 1
0
989898
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
17
PRIME CODING PRIME CODING PRIME CODING
Sample Output 1
989898
Explanation:
Xor_sum(0)=(0^989898)=989898
Sample Input 2
3
G
1
IN
6
Sample Output 2
D
14 O
Explanation
Xor_sum(4)=(4^1)+(4^6)+(4^3)=14.
C
Sample Input 3
4
E
9
IM
7
4
0
PR
Sample Output 3
46
Explanation:
Xor_sum(8)=(8^7)+(8^4) +(8^0)+(8^3)=46
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
18
PRIME CODING PRIME CODING PRIME CODING
Problem Statement :
One of the first lessons IT students learn is the representation of natural numbers
in the binary number system (base 2) This system uses only two digits, 0 and 1. In
everyday life we use for convenience the decimal system (base 10) which uses
ten digits, from 0 to 9. In general, we could use any numbering system.
Computer scientists often use systems based on 8 or 16. The numbering system
based on K uses K digits with a value from 0 to K-1. Suppose a natural number M
is given, written in the decimal system To convert it to the corresponding writing in
G
the system based on K, we successively divide M by K until we reach a quotient
that is less than K
IN
The representation of M in the system based on K is formed by the final quotient
(as first digit) and is followed by the remainder of the previous divisionsFor
example :
D
If M=122 and K=8, 122 in base 10= 172 in base 8 This means that the number
O
In decimal system = 172 in octal system.
In some cases in the new representation all the digits of the number are the
same. For example 63 in base 10= 333 in base 4
IM
Given a number M in its decimal representation, your task is find the minimum
base B such that in the representation of M at base B all digits are the same.
PR
Input Format
Constraints
1 <= M = 10^12
Sample Input 1 :
41
Sample Output 1 :
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
19
PRIME CODING PRIME CODING PRIME CODING
40
Explanation :
Here 41 in base 40. will be 11 so it has all digits the same, and there is no smaller
base satisfying the requirements
Sample Input 2 :
34430
Sample Output 2 :
G
312
Explanation :
IN
Here 34430 in base 312 will have all digits the same and there is no smaller base
satisfying the requirements
D
O
Problem Statement :
C
Andy wants to go on a vacation to de-stress himself. Therefore he decides to take
a trip to an island. It is given that he has as many consecutive days as possible to
E
rest, but he can only make one trip to the island. Suppose that the days are
numbered from 1 to N. Andy has M obligations in his schedule, which he has
IM
already undertaken and which correspond to some specific days. This means that
ith obligation is scheduled for day Di. Andy is willing to cancel at most k of his
obligations in order to take more holidays.
PR
Your task is to find out the maximum days of vacation Andy can take by canceling
at most K of his obligations.
Input Format
The first line contains an integer N, denoting the total number of days
The next line contains an integer M denoting the total number of obligations.
The next line contains an integer K denoting the largest number of obligations
he could cancel
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
20
PRIME CODING PRIME CODING PRIME CODING
Constraints
1<=N<=10^6
1<=M<=2*10^6
1<=K<=2*10^6
1<=D[i]<=10^6
G
Sample Input 1:
IN
10
5
D
2
6 O
9
3
C
2
7
E
Sample Output 1 :
IM
5
Explanation:
Here he could cancel his 3rd and 4th obligation which makes vacation length 5.
PR
Sample input 2:
7
2
034
Sample Output 2:
3
Explanation:
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
21
PRIME CODING PRIME CODING PRIME CODING
Here he could not cancel any obligation since K=0, so the vacation length is 3.
Problem Statement :
You need to build a road in a rugged terrain. You know the sea level of each
segment of the rugged terrain, i.e., the i-th segment is Li meters from sea level.
You need to transform the terrain into a strictly downward sloping terrain for the
road, i.e., for each i-th segment where 2 <= i <= N, resultant Li-1 > Li. To do so,
G
you employ a powerful digging team to help you dig and reduce the sea level of
the segments. On day D, the team can reduce the sea level for each segment that
IN
you scheduled that day by 2D-1 meters each.
You are allowed to assign the team to dig on multiple segments and/or dig on the
D
same segments for multiple days.
Your task is to find the minimum number of days needed to transform the terrain
as per your requirements.
O
Input Format
C
N :: INTEGERThe first line contains an integer, N, denoting the number of elements
in L. N :: 1 -> 10^5
E
10^9
Sample Output 1 :1
Problem Statement :
You are given an array of size N. You need to change this array into a mountain.
By mountain we mean, the either ends of the array should have equal elements.
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
22
PRIME CODING PRIME CODING PRIME CODING
Then as we move towards the middle from both ends, the next element is just one
more than the previous one. So, it would have a peak in the middle and decrease if
you go towards either end, just like a mountain.
Examples of mountains are [1, 2, 3, 2, 1] or [6, 7, 8, 8, 7, 6]. But the array [1, 2, 4, 2,
1] is not a mountain because from 2 to 4 the difference is 2. The array [1, 2, 3, 1] is
also not a mountain because the elements 2 and 3 are not equal from both ends.
You need to find the minimum number of elements that should be changed to
make the array a mountain. You can make the elements negative or zero as well.
G
Input FormatN :: INTEGERThe first line contains an integer, N, denoting the
number of elements in array. N :: 1 -> 10^5
IN
array :: INTEGER ARRAYEach line i of the N subsequent lines (where 0 ≤ i < N)
contains an integer describing i-th element of array. array[i] :: 1 -> 10^6
D
Sample Input 1:512345
Sample Output 1 :2
Sample input 2:9111232111
O
C
Sample Output 2:4
Problem Statement :
E
some contiguous pieces such that after cutting, all the pieces are equal to one
another.
PR
You can’t rearrange the characters in the cut pieces or join the pieces together.
You want to make the number of pieces as large as possible. What is the
maximum number of pieces you can get?
Note: You can observe that you may not want to cut the string at all, therefore the
number of pieces is 1. Hence, the answer always exists.
Input Format
S :: STRINGThe first line contains a string, S, denoting the string.length(S) :: 1 -> 2
* 10^5
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
23
PRIME CODING PRIME CODING PRIME CODING
Sample Output 1 :5
Problem Statement :
G
Today you decided to go to the gym. You currently have energy equal to E units.
There are N exercises in the gym. Each of these exercises drains Ai amount of
IN
energy from your body.
You feel tired if your energy reaches 0 or below. Calculate the minimum number of
exercises you have to perform such that you become tired. Every unique exercise
D
can only be performed at most 2 times as others also have to use the machines.
O
If performing all the exercises does not make you feel tired, return -1.
Input FormatE :: INTEGERThe first line contains an integer, E, denoting the
C
Energy.E :: 1 -> 10^5
Sample Output 1 :4
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
24
PRIME CODING PRIME CODING PRIME CODING
Problem Statement :
There is a battle between heroes and villains going on. You have M heroes, all of
them have the same health H. There are N villains, health of the i-th villain is Vi.
When a hero, with health H battles a villain with health Vi, one of the three
scenarios can happen:
if H > Vi: The villain is defeated, and the health of the hero is decreased by Vi if H
< Vi: The villain wins, his health is not affected, and the hero is no longer able to
fight. if H = Vi: Both are considered defeated, and neither can fight.
G
The heroes start fighting villains one by one in the same order, first villain 1 then
IN
villain 2 and so on. It might be possible that before defeating all the villains, all the
heroes are defeated. Therefore, to ensure the victory of the heroes, you want to
remove some villains from the front.
D
Your task is to find the minimum number of villains you need to remove from the
front such that the victory of the heroes is guaranteed.
O
Note: If in the last battle, both the hero and villain are defeated and no more
heroes or villains remain, it would still be considered a victory since all the villains
C
are defeated.
Input Format
E
Sample Output 1 :0
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
25
PRIME CODING PRIME CODING PRIME CODING
G
IN
D
O
C
E
IM
PR
PRIME CODING
Infosys Coding Sheet
PRIME CODING PRIME CODING
26