Practice Questions Day 4
Practice Questions Day 4
__________________________________________________________________________________
A string S consists of a series of words representing the secret passwords for a bank safety locker and
an integer K.
You must break the entire string S into substrings of length K.
Rearrange these substrings to form the lexicographically smallest string.
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 of -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
lockboxsecurity -- denotes S
3 -- 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
blcokssecurityox
Explanation
S = “lockboxsecurity” and K = 3
S can be broken into “loc” + “kbo” + “xse” + “cur” + “ity” (substrings of size K = 3).
The best way to rearrange these substrings to form the lexicographically smallest string is → “blc” +
“oks” + “security” + “ox” = “blcokssecurityox”.
Hence, the output is blcokssecurityox.
__________________________________________________________________________________
There are N people standing in line at a grocery store. Each person needs to buy A[i] number of
items.
However, a person can purchase a maximum of 6 items at a time.
• If a person needs more than 6 items, they must buy 6 items first, then rejoin the queue at the end
to purchase the remaining items.
• If a person buys all the required items, they leave the queue immediately.
Print the number of items left to buy if all the people in line visited the counter once.
Function Description:
In the provided code snippet, implement the provided itemsLeft(...) method to print the number of
items left to buy if all the people in line have visited the 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 line.
The second line contains N space-separated integers of array A[i], denoting the number of items
required by each person in line.
Sample Input
5 -- denotes N
3 7 5 9 4 -- denotes A[i]
Constraints
1 <= N <= 10^4
Output Format
The output contains an integer denoting the number of items left to buy after all the people in line
have visited the counter once.
Each remaining item count should be printed on a separate line in the order in which the individuals
rejoin the queue.
Sample Output
1
4
2
Explanation
N=5
A[i] = [3, 7, 5, 9, 4]
Find the number of tuples (i, j, k, l) such that the following holds:
Function Description:
In the provided code snippet, implement the provided productOfWeights(...) 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 integers, denoting the weights of the items.
Sample Input:
6 -- denotes N
Output Format:
Sample Output:
Explanation:
1. (1, 2, 4, 5)
2. (1, 2, 5, 6)
3. (3, 4, 5, 6)
Input Explanation
First line:
6
Second line:
362636
This is the array A[i] with 6 integers, representing the weights of 6 items.
Output Explanation
We are tasked with finding the number of tuples (i, j, k, l) such that:
In simpler terms:
We need to find combinations of four indices (i, j, k, l) where the first two numbers A[i] and
A[j] multiplied together equals the product of the last two numbers A[k] and A[l].
Now, let’s go through the given example and find the valid tuples:
Given Array:
A = [3, 6, 2, 6, 3, 6]
Step-by-Step Explanation:
Output:
3
There are 3 valid tuples where the condition A[i] * A[j] = A[k] * A[l] is satisfied.
__________________________________________________________________________________
Question 4: Maximum Average Temperature after Adjustment with the Temperature Adjustment
Machine
There are N cities, and the current temperature of each city is given. An integer K is also given, which
represents a target temperature for the cities.
o The first use is to adjust the city’s temperature in such a way that the sum of all
cities’ temperatures becomes zero.
o The second use is to adjust the temperature of all cities (except the selected city) to
the value K.
2. The person (or city in this case) using the machine can only adjust their own temperature
to help the other cities.
3. The sum of all cities' temperatures before using the machine should be 0.
o The city using the machine adjusts its temperature to ensure the sum of the
temperatures of all cities becomes zero.
Find the maximum possible average temperature that the group of cities can get after using the
Temperature Adjustment Machine on one of the cities.
Input Format:
The second line contains N space-separated integers representing the current temperature of each
city.
Sample Input:
46
-3 2 -1 3
Sample Output:
5.25
Explanation:
Given Data:
The temperatures of the 4 cities are [-3, 2, -1, 3], and the target temperature K = 6.
Steps:
o To make the sum zero, we must adjust the temperature of one city.
o The best city to choose is the one with the highest temperature, which is city 4 with
a temperature of 3.
o The city with the highest temperature (3) can use the machine to adjust their
temperature to 1. This will balance out the sum of temperatures to zero.
o After the adjustment, the temperatures of the other cities are changed to the target
temperature K = 6.
[1,6,6,6][1, 6, 6, 6][1,6,6,6]
C[i] is the cost to plant one new tree in the i-th garden plot.
The goal is to ensure that no two adjacent garden plots have the same number of trees. You need to
calculate the minimum cost to add trees to some garden plots so that all adjacent plots have
different numbers of trees.
Function Description:
In the provided code snippet, implement the gardenPlantation(...) method to print the minimum cost
required to add trees so that all adjacent garden plots have different numbers of trees. 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 garden plots.
The second line contains N space-separated integers, denoting the elements of array H,
where each element H[i] represents the number of trees planted in the i-th garden plot.
The third line contains N space-separated integers, denoting the elements of array C, where
each element C[i] represents the cost to plant a tree in the i-th garden plot.
Output Format:
The output should be a single integer denoting the minimum cost required to add trees so
that all adjacent garden plots have different numbers of trees.
Constraints:
Sample Input:
5536
4213
Sample Output:
2
Explanation:
The first two garden plots have the same number of trees (5), so we need to add a tree to
one of them.
o The cost to add a tree to garden plot 1 is 4 and to garden plot 2 is 2. We choose
garden plot 2 because the cost is lower.
o After adding a tree to garden plot 2, the tree counts become [5, 6, 3, 6].
Now, garden plots 2 and 3 have different numbers of trees, but garden plots 3 and 4 have the
same number (6).
o The cost to add a tree to garden plot 4 is 3, so we add a tree to garden plot 4.
o After adding a tree to garden plot 4, the tree counts become [5, 6, 3, 7].
The minimum cost to ensure all adjacent garden plots have different numbers of trees is 2.
Two arrays, A[i] and B[i], represent the weights of two types of gifts. Your task is to form the
maximum number of pairs of gifts where:
One gift is selected from array A, and the other from array B.
The weight difference between the two selected gifts should be at most 1.
Function Description:
In the provided code snippet, implement the maxGiftPairs(...) method to print the maximum number
of gift 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 array A
(representing weights of gifts of type 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
(representing weights of gifts of type B).
The fourth line contains M space-separated integers, denoting the elements of array B[i].
Output Format:
The output should be a single integer denoting the maximum number of gift pairs formed
from the two arrays.
Constraints:
Sample Input:
4 -- denotes N
5 -- denotes M
Sample Output:
Explanation:
N=4
A[i] = [1, 4, 6, 2]
M=5
B[i] = [5, 1, 5, 7, 9]
We can form the following pairs with the difference in weight being at most 1:
Emma is planning a toy-buying schedule for Olivia for the next N days. Olivia has a goal to collect at
least K toys on two consecutive days.
For example, if K = 10, and Olivia collects 3 toys yesterday, she must collect a minimum of 7 toys
today to reach the required sum.
Emma has already planned the number of toys Olivia will collect each day, represented by the array
toys[], where toys[i] denotes the toys Olivia plans to collect on the ith day.
However, Emma is worried that Olivia will not meet her goal on some days. She wants to ensure that
Olivia meets her goal of at least K toys for every two consecutive days.
Print the minimum number of extra toys Emma must buy so that Olivia meets her goal of at least K
toys for each consecutive pair of days.
Input Format:
The second line contains an integer K, denoting the minimum number of toys Olivia must
collect over any two consecutive days.
The third line contains N space-separated integers, denoting the toys Olivia plans to collect
on each day (toys[i]).
Output Format:
Print the minimum number of toys Emma must add so that Olivia collects at least K toys for
any two consecutive days.
Example:
Sample Input:
12
3614
Sample Output:
Explanation:
For days 2 and 3, the sum is 6 + 1 = 7, which is less than K = 12. We need to add 5 more toys
on day 3 to make the sum 12.
For days 3 and 4, the sum is 1 + 4 = 5, which is less than K = 12. We need to add 7 more toys
on day 4 to make the sum 12.
You are given an integer N and an array of N integers. Your task is to calculate the maximum value of
0≤i<j<N0 \leq i < j < N0≤i<j<N. Here, ⊕ is the Bitwise XOR operator, and & is the Bitwise AND
((i⊕j)⊕(i&j))((i \oplus j) \oplus (i \& j))((i⊕j)⊕(i&j)) for all possible pairs (i,j)(i, j)(i,j) in the array where
operator.
Function Description
You need to implement the maximumXORSum() function which will return the maximum XOR sum
possible among all pairs (i,j)(i, j)(i,j).
Input Format:
The first line contains an integer NNN, denoting the number of elements in the array.
The second line contains NNN space-separated integers, denoting the elements of the array
A[i]A[i]A[i].
Output Format:
The output will contain a single integer: the maximum value of ((i⊕j)⊕(i&j))((i \oplus j) \
oplus (i \& j))((i⊕j)⊕(i&j)) among all pairs (i,j)(i, j)(i,j).
Constraints:
Example:
Input:
3 5 7 9 12
Output:
15
Explanation:
Let’s calculate the value of the expression ((i⊕j)⊕(i&j))((i \oplus j) \oplus (i \& j))((i⊕j)⊕(i&j)) for
pairs of indices:
______________________________________________________________________________