Question:
Lexicographic Smallest
A string S consists of a lowercase letter and an integer K.
You must break the entire S into substrings of length K.
Rearrange these subarrays to form the lexicographically smallest string.
Print the rearranged string array.
Function Description
In the provided code snippet, implement the provided reOrder(...) method to print
the rearranged string array. You can write your code in the space below the
phrase “WRITE YOUR LOGIC HERE”.
Multiple test cases will be running, so the Input and Output should match exactly as
provided.
The base Output variable result is set to a default value -404 that can be modified.
Additionally, you can add or remove these output variables.
Input Format
The first line contains a string, denoting the string S.
The second line contains an integer, denoting the integer K.
Sample Input
abcda -- denotes S
2 -- denotes K
Constraints
1 <= Size of S <= 103
1 <= K <= Size of S
Output Format
The output contains a string denoting the rearranged string array.
Sample Output
aabcd
Explanation
S = “abcda” and K = 2
S can be broken into “ab” + “cd” + “a” (substrings of size K = 2).
The best way to rearrange these substrings to form the lexicographically smallest string
is → “a” + “ab” + “cd” = “aabcd”.
Hence, the output is aabcd.
Used in number of tests :
Attempts :
2581
Q:
21
Q Id:
1363244
Category:
Coding
Points:
15
Assigned difficulty:
Medium
Difficulty Index: 0.28 Medium
Topics:
Array Operations
Question:
Tickets Left
There are N people standing in a queue at a ticket counter. Each person needs to
buy A[i] number of tickets.
However, a person can purchase a maximum of 6 tickets at a time.
If a person needs more than 6 tickets, they must buy 6 tickets first, then rejoin
the queue at the end to purchase the remaining tickets.
If a person buys all the required tickets, they leave the queue immediately.
Print the number of tickets left to buy if all the people in a line visited the
ticket counter once.
Function Description
In the provided code snippet, implement the provided ticketsLeft(...) method to
print the number of tickets left to buy if all the people in a line visited the ticket counter
once. You can write your code in the space below the phrase “WRITE YOUR LOGIC
HERE”.
There will be multiple test cases running, so the Input and Output should match exactly
as provided.
The base Output variable result is set to a default value of -404, which can be
modified. Additionally, you can add or remove these output variables.
Input Format
The first line contains an integer N, denoting the number of people standing in a line.
The second line contains N space-separated integers of array A[i], denoting the number
of tickets required by each person in a line.
Sample Input
5 -- denotes N
15968 -- denotes A[i]
Constraints
1 <= N <= 104
Output Format
The output contains an integer denoting the number of tickets left to buy after all the
people in line have visited the ticket counter once.
Each remaining ticket count should be printed on a separate line in the order in which
the individuals rejoin the queue.
Sample Output
3
2
Explanation
N=5
A[i] = [1, 5, 9, 6, 8]
The number of tickets required by each person in a line is 1, 5, 9, 6, 8.
After all the people visit the counter once:
The number of tickets required by the first person is 0; they exited the counter.
The number of tickets required by the second person is 0; they exited the
counter.
The number of tickets required by the third person is 3; they joined the line again
from the end.
The number of tickets required by the fourth person is 0; they exited the counter.
The number of tickets required by the fifth person is 2; they joined the line again
from the end.
The person who needed 9 tickets is now left with 3 more tickets to buy.
The person who needed 8 tickets is now left with 2 more tickets to buy.
Hence, the output is:
3
2
Used in number of tests :
15
Attempts :
8913
Q:
23
Q Id:
1363228
Category:
Coding
Points:
15
Assigned difficulty:
Medium
Difficulty Index: 0.25 Hard
Topics:
Array Operations
Question:
Trouble Sum
Given is an array, A[i] of length N.
Find the number of tuples (i, j, k, l) such that the following holds:
● 1 <= i < j < k < l <= N
● A[i] + A[j] = A[k] + A[l]
Note
Print 0 if you are not able to find any such tuples.
Function Description
In the provided code snippet, implement the provided troubleSum(...) method to find
the number of tuples. You can write your code in the space below the phrase “WRITE
YOUR LOGIC HERE”.
There will be multiple test cases running so the Input and Output should match exactly
as provided. The base output variable result is set to a default value of -404 which can
be modified.
Additionally, you can add or remove these output variables.
Input Format
The first line contains one integer, N, denoting the size of the array.
The second line contains N space-separated non-negative integers, denoting the
elements of the array.
Sample Input
5 -- denotes N
11111 -- denotes A[i]
Constraints
4 <= N <= 3000
1 <= A[i] <= N
Output Format
The output contains a single integer denoting the number of tuples.
Sample Output
5
Explanation
Following are the valid tuples in the given array:
1. (1, 2, 3, 4).
2. (1, 2, 3, 5).
3. (1, 2, 4, 5).
4. (1, 3, 4, 5).
5. (2, 3, 4, 5).
Hence, the output is 5.
Used in number of tests :
Attempts :
2686
Q:
25
Q Id:
1363206
Category:
Coding
Points:
15
Assigned difficulty:
Medium
Difficulty Index: 0.28 Medium
Topics:
Array Operations
Question:
Maximum Average
There are N number of people in a group.
The net monthly income of each person and an integer, K, are given.
One day, the group finds a magic stone.
There are some conditions for using this stone:
1. Only one person in the group can use the stone at the most 2 times.
(Once is for making the average salary 0 and the other is to change the others'
incomes to K, except their own).
2. The person with the stone can change their income only for the benefit of the
other members.
3. The sum of the incomes of the group before using the stone should be 0.
(The person who will use the stone can use their salary to make the sum of the
group's salary 0).
Find the maximum average net income the group can get after getting the
magic stone to one of the people.
Note
You must decide which person can get the magic stone to maximize the average net
income.
The net income can be negative (in case there is a loss).
Surely, the average net income of the given incomes is not equal to 0.
The average net salary can be in the form of float/double.
Function Description
In the provided code snippet, implement the provided maximumAverage(...) method
to find the maximum average net income the group can get after getting the magic
stone to one of the people. You can write your code in the space below the
phrase “WRITE YOUR LOGIC HERE”.
There will be multiple test cases running, so the Input and Output should match exactly
as provided.
The base Output variable result is set to a default value of -404 which can be
modified. Additionally, you can add or remove these output variables.
Input Format
The first line contains two space-separate integers, N, denoting the number of people in
array A, and an integer, K.
The second line contains N space-separated integers, A1, A2,…, AN, denoting A[i] as
the net monthly income.
Sample Input
54 -- denotes N
1 -3 7 -5 2 -- denotes A[i]
Constraints
2 <= N <= 1^3
1 <= K <= 1^4
-1^4 <= A[i] <= 1^4
Output Format
The output contains a float value denoting the maximum average net income the group
can get after getting the magic stone to one of the people.
Sample Output
4.2
Explanation
A = [ 1, -3, 7, -5, 2 ]
So, the initial sum of the net income is 2.
First, make it 0 by choosing the right person to give the magic stone to.
The most suitable person for this purpose is a person with a net income 7.
They will give 2 units from their income to make the sum 0 (this is the first time they
use the stone).
Now, the 2nd time, except for the person with the magic stone, all the others will gain K,
which is 4.
The total average income will become:
Avg = ((4) + (4) + (5) + (4) + (4))/5 = 4.2.
Hence, the output is 4.2.
Used in number of tests :
Attempts :
1653
Q:
33
Q Id:
1353776
Category:
Coding
Points:
15
Assigned difficulty:
Medium
Difficulty Index: 0.29 Medium
Topics:
Dynamic Programming
Question:
Different Buildings
There are N buildings in an area.
Consider the following:
H[i] as the number of floors in the ith building and C[i] as the cost to build a new floor in
the ith building for all 1 <= i <= N.
Print the minimum cost required to build a new floor so that all adjacent
buildings have different heights.
Function Description
In the provided code snippet, implement the
provided differentBuildings(...) method to print the minimum cost required to
build a new floor so that all adjacent buildings have different heights. You can write your
code in the space below the phrase “WRITE YOUR LOGIC HERE”.
There will be multiple test cases running so the Input and Output should match exactly
as provided.
The base Output variable result is set to a default value of -404, which can be
modified. Additionally, you can add or remove these output variables.
Input Format
The first line contains an integer N, denoting the number of buildings.
The second line contains N space-separated integers, denoting the elements of array H.
The third line contains N space-separated integers, denoting the elements of array C.
Sample Input
4 -- denotes N
2213 -- denotes H[i]
2312 -- denotes C[i]
Constraints
1 <= N <= 105
1 <= H[i] <= 109
1 <= C[i] <= 109
Output Format
The output contains an integer denoting the minimum cost required to build a new floor
so that all adjacent buildings have different heights.
Sample Output
2
Explanation
N =4
H =[2213]
C =[2312]
The optimal way is to build one floor in the 1st building.
H=[3213]
Now, all adjacent buildings have different heights.
The cost of building a new floor in the 1 st building is 2.
The minimum cost required to build a new floor so that all adjacent buildings have
different heights is 2.
Hence, the output is 2.
Used in number of tests :
Attempts :
1229
Q:
37
Q Id:
1341512
Category:
Coding
Points:
15
Assigned difficulty:
Medium
Difficulty Index: 0.30 Medium
Topics:
Algorithms
Question:
Maximum Pairs
Two arrays, A[i] and B[i], of size N and M, are given.
You must form pairs from the elements of these two arrays.
The pair can be formed by selecting one element from one array and a second element
from another array such that the maximum difference between the pair's elements is at
most 1.
Print the maximum number of pairs formed from the two arrays.
Note
Integers N and M are positive.
Integer arrays A[i] and B[i] are positive.
Function Description
In the provided code snippet, implement the provided maxpairs(...) method to print
the maximum number of pairs formed from the two arrays. You can write your code in
the space below the phrase “WRITE YOUR LOGIC HERE”.
There will be multiple test cases running, so the Input and Output should match exactly
as provided.
The base Output variable result is set to a default value of -404, which can be
modified. Additionally, you can add or remove these output variables.
Input Format
The first line contains a single integer N, denoting the number of elements in the
array A.
The second line contains N space-separated integers, denoting the elements of
array A[i].
The third line contains a single integer M, denoting the number of elements in array B.
The fourth line contains M space-separated integers, denoting the elements of
array B[i].
Sample Input
4 -- denotes N
1462 -- denotes A[i]
5 -- denotes M
51579 -- denotes B[i]
Constraints
1 <= N <= 100
1 <= A[i] <= 100
1 <= M <= 100
1 <= B[i] <= 100
Output Format
The output contains a single integer denoting the maximum number of pairs formed
from the two arrays.
Sample Output
3
Explanation
N =4
A[i] = [1, 4, 6, 2]
M =5
B[i] = [5, 1, 5, 7, 9]
Choose 1 from array A and 1 from array B.
The pair will be (1, 1) with a difference of 0.
Choose 4 from array A and 5 from array B.
The pair will be (4, 5) with a difference of 1.
Choose 6 from array A and 5 from array B.
The pair will be (6, 5) with a difference of 1.
Search for the last element, i.e., 2, and obtain a pair.
Since 1 has already been used, you cannot make a pair.
So, the total pair is 3, which is the maximum possible.
Hence, the output is 3.
Used in number of tests :
Attempts :
1392
Q:
100
Q Id:
1319856
Category:
Coding
Points:
15
Assigned difficulty:
Medium
Difficulty Index: 0.33 Medium
Topics:
Array Operations
Question:
Minimum Money
Sophia knows that Joe will remain healthy if he eats at least K vegetables for two
consecutive days.
For example, if K = 3, and Joe ate 1 vegetable yesterday, today, Joe must eat a
minimum of 2 vegetables to remain healthy.
Sophia has created the plan for the next N days given in the array arr, where arr[i] is
the number of vegetables Joe will eat on the ith day.
However, she does not think this plan will keep Joe healthy.
Print the minimum money Sophia must spend so that Joe remains healthy.
Note
The cost of 1 vegetable is 1 dollar.
Function Description
In the provided code snippet, implement the provided findMinimumCost(...) method
to print the minimum money Sophia must spend so that Joe remains healthy. You can
write your code in the space below the phrase "WRITE YOUR LOGIC HERE".
There will be multiple test cases running, so the Input and Output should match exactly
as provided.
The base Output variable result is set to a default value of -404, which can be
modified. Additionally, you can add or remove these output variables.
Input Format
The first line contains an integer N, denoting the number of days for which Sophia has a
plan.
The second line contains an integer K, denoting the minimum number of vegetables
required for two consecutive days to keep Joe healthy.
The third line contains N space-separated integers of the array arr[i], denoting
the ith day plan.
Sample Input
3 -- denotes N
5 -- denotes K
2 0 1 -- denotes array arr[i]
Constraints
1 <= N <= 100
0 <= K <= 500
Output Format
The output contains a single integer denoting the minimum money Sophia must spend
so that Joe remains healthy.
Sample Output
4
Explanation
N=3
K=5
arr[i] = [2, 0, 1]
If you give 3 vegetables on the second day and 1 vegetable on the third day, Joe will
remain healthy, as arr[i] = [2, 3, 2].
The sum of arr[0] + arr[1] = 5 and arr[1] + arr[2] = 5.
So, Joe eats at least K = 5 vegetables for any two consecutive days.
Therefore, the minimum money Sophia must spend so Joe remains healthy is 4.
Hence, the output is 4.
Used in number of tests :
Attempts :
1579
Q:
180
Q Id:
1238908
Category:
Coding
Points:
15
Assigned difficulty:
Medium
Difficulty Index: 0.39 Medium
Topics:
Bit manipulation
Question:
Bit Manipulation: Maximum Value
An integer, N, is given.
Find the maximum value of ((i⊕j)⊕(i&j)) among all pairs of (i, j) of integers with 1
Here, ⊕ is the Bitwise XOR operator, and & is the Bitwise AND operator.
<= i < j <= N.
Find the maximum value.
Function Description
In the provided code snippet, implement the provided maximumvalue(...) method to
find the maximum value. You can write your code in the space below the phrase “WRITE
YOUR LOGIC HERE”.
There will be multiple test cases running, so the Input and Output should match exactly
as provided.
The base Output variable result is set to a default value of -404, which can be
modified. Additionally, you can add or remove these output variables.
Input Format
The first line contains a single integer N, denoting the range.
Sample Input
10
Constraints
1 <= N <= 10^5
Output Format
The output contains a single integer denoting the maximum value.
Sample Output
15
Explanation
Let i = 2 and j = 3.
Calculating ((i⊕j)⊕(i&j)) = ((2⊕3)⊕(2&3)) = ((1)⊕(2)) = 3.
Let i = 7 and j = 8.
Calculating ((i⊕j)⊕(i&j)) = ((7⊕8)⊕(7&8)) = ((15)⊕(0)) = 15.
Hence, the output is 15.