0% found this document useful (0 votes)
19 views12 pages

Practice Questions Day 4

The document contains multiple programming questions related to various algorithms and data structures. Each question provides a problem statement, input format, output format, and sample inputs and outputs, covering topics such as string manipulation, queue processing, tuple counting, temperature adjustment, garden plantation, gift pairing, and toy counting. The questions require implementing specific functions to solve the problems as described.

Uploaded by

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

Practice Questions Day 4

The document contains multiple programming questions related to various algorithms and data structures. Each question provides a problem statement, input format, output format, and sample inputs and outputs, covering topics such as string manipulation, queue processing, tuple counting, temperature adjustment, garden plantation, gift pairing, and toy counting. The questions require implementing specific functions to solve the problems as described.

Uploaded by

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

PRACTICE QUESTIONS

__________________________________________________________________________________

Question : 1 : Bank Safety Locker Passwords

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.

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 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.
__________________________________________________________________________________

Question:2 : Items Left - Grocery Shopping

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]

The number of items required by each person in line is 3, 7, 5, 9, 4.


After all the people visit the counter once:
• The first person needs 0 items and exits the counter.
• The second person needs 1 item and re-enters the line.
• The third person needs 0 items and exits the counter.
• The fourth person needs 3 items and re-enters the line.
• The fifth person needs 0 items and exits the counter.

Hence, the output is:


1
4
__________________________________________________________________________________

Question:3 : Product of Weights

You are given an array, A[i], representing the weights of items.

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]

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

362636 -- denotes A[i]

Output Format:

The output contains a single integer denoting the number of tuples.

Sample Output:

Explanation:

The valid tuples are:

1. (1, 2, 4, 5)

2. (1, 2, 5, 6)

3. (3, 4, 5, 6)

Thus, the output is 3.

Input Explanation

First line:
6

 This represents the size of the array A[i], where N = 6.

 In other words, there are 6 elements in the array.

Second line:

362636

 This is the array A[i] with 6 integers, representing the weights of 6 items.

 The array is: A = [3, 6, 2, 6, 3, 6].

Output Explanation

We are tasked with finding the number of tuples (i, j, k, l) such that:

 1 <= i < j < k < l <= N

 A[i] * A[j] = A[k] * A[l]

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:

1. Tuple 1 (1, 2, 4, 5):

o A[1] = 3, A[2] = 6 → Product = 3 * 6 = 18

o A[4] = 6, A[5] = 3 → Product = 6 * 3 = 18

o Both products are equal, so this is a valid tuple.

2. Tuple 2 (1, 2, 5, 6):

o A[1] = 3, A[2] = 6 → Product = 3 * 6 = 18

o A[5] = 3, A[6] = 6 → Product = 3 * 6 = 18

o Both products are equal, so this is another valid tuple.

3. Tuple 3 (3, 4, 5, 6):

o A[3] = 2, A[4] = 6 → Product = 2 * 6 = 12

o A[5] = 3, A[6] = 6 → Product = 3 * 6 = 12

o Both products are equal, so this is also a valid tuple.

Output:
3

 There are 3 valid tuples where the condition A[i] * A[j] = A[k] * A[l] is satisfied.

Thus, the number of valid tuples is 3, which is the final output.

__________________________________________________________________________________

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.

One day, the group finds a "Temperature Adjustment Machine".

The machine has certain conditions:

1. Only one city can use the machine at most 2 times.

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 first line contains two space-separated integers, N and K:

 N represents the number of cities.

 K is the target temperature.

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:

1. Initial Sum Calculation:

o The initial sum of the temperatures is:

sum=(−3)+2+(−1)+3=1\text{sum} = (-3) + 2 + (-1) + 3 = 1sum=(−3)+2+(−1)+3=1

o To make the sum zero, we must adjust the temperature of one city.

2. Choosing the Best 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.

3. Adjust Other Cities' Temperatures:

o After the adjustment, the temperatures of the other cities are changed to the target
temperature K = 6.

o The final list of temperatures is:

[1,6,6,6][1, 6, 6, 6][1,6,6,6]

4. Calculate the Maximum Average Temperature:

o The new average temperature is:

average=1+6+6+64=5.25\text{average} = \frac{1 + 6 + 6 + 6}{4} = 5.25average=41+6+6+6=5.25

Hence, the maximum average temperature is 5.25.

Question 5: Garden Plantation

There are N garden plots in a row. Consider the following:

 H[i] is the number of trees planted in the i-th garden plot.

 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:

 1 <= N <= 10^5

 1 <= H[i] <= 10^9

 1 <= C[i] <= 10^9

Sample Input:

5536

4213

Sample Output:

2
Explanation:

 Initially, the garden plots have trees as [5, 5, 3, 6].

 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.

Question :6 : Maximum Gift Pairings

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.

The goal is to determine the maximum number of valid gift pairs.

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:

 1 <= N <= 100

 1 <= A[i] <= 100

 1 <= M <= 100

 1 <= B[i] <= 100

Sample Input:

4 -- denotes N

1462 -- denotes A[i]

5 -- denotes M

51579 -- denotes B[i]

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:

 (1, 1) with a difference of 0.

 (4, 5) with a difference of 1.

 (6, 5) with a difference of 1.

Thus, the total number of valid gift pairs is 3.


Question : 7 : Minimum Toy Count

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.

Problem Input and Output:

Input Format:

 The first line contains an integer N, denoting the number of days.

 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:

 Olivia’s plan for the next 4 days is [3, 6, 1, 4].


 For days 1 and 2, the sum is 3 + 6 = 9, which is less than K = 12. We need to add 3 more toys
on day 2 to make the sum 12.

 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.

Thus, the total cost (extra toys added) is 3 + 5 + 7 = 9.


__________________________________________________________________________________

Question : 8 : Maximum XOR Sum

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:

 1≤N≤10001 \leq N \leq 10001≤N≤1000

 1≤A[i]≤1051 \leq A[i] \leq 10^51≤A[i]≤105

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:

 For i=3i = 3i=3 and j=5j = 5j=5:

(3⊕5)⊕(3&5)=(6)⊕(1)=7(3 \oplus 5) \oplus (3 \& 5) = (6) \oplus (1) = 7(3⊕5)⊕(3&5)=(6)⊕(1)=7

 For i=5i = 5i=5 and j=7j = 7j=7:

(5⊕7)⊕(5&7)=(2)⊕(5)=7(5 \oplus 7) \oplus (5 \& 7) = (2) \oplus (5) = 7(5⊕7)⊕(5&7)=(2)⊕(5)=7

 For i=9i = 9i=9 and j=12j = 12j=12:

(9⊕12)⊕(9&12)=(5)⊕(8)=13(9 \oplus 12) \oplus (9 \& 12) = (5) \oplus (8) =


13(9⊕12)⊕(9&12)=(5)⊕(8)=13

 For i=7i = 7i=7 and j=12j = 12j=12:

(7⊕12)⊕(7&12)=(11)⊕(4)=15(7 \oplus 12) \oplus (7 \& 12) = (11) \oplus (4) =


15(7⊕12)⊕(7&12)=(11)⊕(4)=15

The maximum value is 151515, which is the output.

______________________________________________________________________________

You might also like