0% found this document useful (0 votes)
18 views6 pages

Sample Question

The document describes a problem where given arrays A and B, integers X, Y, Z, and an initial sum, the goal is to perform N operations to maximize the sum by either subtracting from the sum, or decreasing X and Y or Y and Z and adding to the sum, while keeping X, Y, Z non-negative. Sample inputs and outputs are provided to illustrate the problem.

Uploaded by

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

Sample Question

The document describes a problem where given arrays A and B, integers X, Y, Z, and an initial sum, the goal is to perform N operations to maximize the sum by either subtracting from the sum, or decreasing X and Y or Y and Z and adding to the sum, while keeping X, Y, Z non-negative. Sample inputs and outputs are provided to illustrate the problem.

Uploaded by

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

Explanation-1

Sample-1: Easy Here, n = 5


A = [5, 5, 0, 3, 0]
You have an array A of integers with n elements. There
q = 5
are q queries to process and each query consists of four queries = [[0, 2, 1, 2], [0, 1, 6, 5], [2, 3, 8,
integers: l, r, x, and y. 0], [2, 4, 9, 6], [3, 4, 8, 9]]

For the subarray of A ranging from index l to r, you need to for query 1:
assign a sequence of integers for each subsequent element. l = 0, r = 2, x = 1, y = 2
The sequence should start from x and increase by y. This means: A[0] = 1
A[1] = 3
• A[l] will be assigned the value of x. A[2] = 5

• A[l+1] will be assigned the value of x + y. So, A = [1, 3, 5, 3, 0]

• A[l+2] will be assigned the value of x + 2*y. for query 2:


l = 0, r = 1, x = 6, y = 5
• Continuing this pattern, A[l+i] will be assigned the A[0] = 6
value of x + i*y, where i ranges from 0 to (r - l). A[1] = 11

Find the sum of all integers in A after processing all queries. So, A = [6, 11, 5, 3, 0]
Since answer can be large, return it modulo 109+7.
for query 3:
Input Format l = 2, r = 3, x = 8, y = 0
A[2] = 8
1. The first line contains an integer, n, denoting the A[3] = 8
number of elements in A.
So, A = [6, 11, 8, 8, 0]
2. Each line i of the n subsequent lines (where 0 ≤ i < n)
contains an integer describing A[i]. for query 4:
l = 2, r = 4, x = 9, y = 6
3. The next line contains an integer, q, denoting the
A = [6, 11, 9, 15, 21]
number of rows in queries.

4. Each line i of the q subsequent lines (where 0 ≤ i < q) for query 5:


l = 3, r = 4, x = 8, y = 9
contains 4 space separated integers each describing
the row queries[i]. A = [6, 11, 9, 8, 17]

5. The 4 space separated integers denote the value of l, r, Hence, answer is 6+11+9+8+17 = 51
x and y for the i-th query.

Constraints Sample Input 2


5
• 1 <= 𝒏 <= 10! 3
9
• 0 <= 𝑨[𝒊] <= 10" 2
5
• 1 ≤ 𝒒 ≤ 10! 4
5
• 0 <= 𝒒𝒖𝒆𝒓𝒊𝒆𝒔[𝒊][𝒋] <= 10!
1263
1228
Sample Input 1 1255
5 1318
5 1229
5
0 Sample output 2
3 37
0
5 Explanation 2
0212 Here, n = 5
0165 A = [3, 9, 2, 5, 4]
2380 q = 5
2496 queries = [[1, 2, 6, 3], [1, 2, 2, 8], [1, 2, 5,
3489 5], [1, 3, 1, 8], [1, 2, 2, 9]]

Sample output 1 for query 1:


51 l = 1, r = 2, x = 6, y = 3
Hence, answer is 3+1+1+1+10 = 16
So, A = [3, 6, 9, 5, 4]

for query 2:
l = 1, r = 2, x = 2, y = 8
So, A = [3, 2, 10, 5, 4]

for query 3:
l = 1, r = 2, x = 5, y = 5
So, A = [3, 5, 10, 5, 4]

for query 4:
l = 1, r = 3, x = 1, y = 8
So, A = [3, 1, 9, 17, 4]

for query 5:
l = 1, r = 2, x = 2, y = 9
A = [3, 2, 11, 17, 4]

Hence, answer is 3+2+11+17+4 = 37

Sample Input 3
5
0
1
0
0
1
5
1277
0136
1111
3491
2310

Sample output 3
16

Explanation 3
Here, n = 5
A = [0, 1, 0, 0, 1]
q = 5
queries = [[1, 2, 7, 7], [0, 1, 3, 6], [1,
1, 1, 1], [3, 4, 9, 1], [2, 3, 1, 0]]

for query 1:
l = 1, r = 2, x = 7, y = 7
A = [0, 7, 14, 0, 1]

for query 2:
l = 0, r = 1, x = 3, y = 6
A = [3, 9, 14, 0, 1]

for query 3:
l = 1, r = 1, x = 1, y = 1
A = [3, 1, 14, 0, 1]

for query 4:
l = 3, r = 4, x = 9, y = 1
A = [3, 1, 14, 9, 10]

for query 5:
l = 2, r = 3, x = 1, y = 0
A = [3, 1, 1, 1, 10]

External Document © 2024 Infosys Limited


Sample output-1:
Sample 2: Medium
0
You are given three integers X,Y and Z and two
arrays A and B both of length N. You are also given an
integer sum which is initially equal to 0.
Explanation-1:
th
You have perform N operations and in each i operation you
Here, N = 2, X = 1, Y = 2, Z = 2
must do only one of the following :
A = [0, 0]
1. Subtract B[i] from sum.
B = [10, 5]
2. Decrease both of X and Y by 1, then add A[i] * X * Y *
Z to sum.
It is given that in starting, sum = 0
3. Decrease both of Y and Z by 1, then add A[i] * X * Y *
Z to sum.
operation 1:
However, after each operation, X,Y and Z must all remain greater
than or equal to 0. Apply type 2 operation (i.e. Decrease both of X and Y by 1, then
add A[1]*X*Y*Z to sum)
Find the maximum sum you can obtain after performing all
operations. Since answer can be large, return it modulo 109+7. X = 0, Y = 1, Z = 2
sum = sum + 0*0*1*2 = 0
Input Format

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


operations. operation 2:
Apply type 3 operation (i.e. Decrease both of Y and Z by 1,
2. The next line contains an integer, X. then add A[2]*X*Y*Z to sum)
3. The next line contains an integer,Y. X = 0, Y = 0, Z = 1
4. The next line contains an integer, Z. sum = sum + 0*0*0*1 = 0

5. Each line i of the N subsequent lines (where 1 ≤ i ≤ N)


contains an integer describing A[i]. Hence, answer is the final value of sum i.e. sum = 0.

6. Each line i of the N subsequent lines (where 1 ≤ i ≤ N)


contains an integer describing B[i].
Sample Input-2:
Constraints
2
• 1 <= 𝑁 <= 10# 10
11
• 1 <= 𝑿 <= 10# 11
• 1 <= 𝒀 <= 10# 1
10
• 1 <= 𝒁 <= 10# 10
0
• 1 <= 𝑨[𝒊] <= 10$

• 1 <= 𝑩[𝒊] <= 10"


Sample output-2:
Sample Input-1:
9990
2
1
2
2 Explanation-2:
0 Here, N = 2, X = 10, Y = 11, Z = 11
0
A = [1, 10]
10
5 B = [10, 0]
It is given that in starting, sum = 0

operation 1:
Apply type 1 operation (i.e. Subtract B[1] from sum.) Apply type 2 operation (i.e. Decrease both of X and Y by 1,
then add A[2]*X*Y*Z to sum)
sum = sum - 10 = -10

X = 2, Y = 2, Z = 3
operation 2:
sum = sum + 2*2*2*3 = 23
Apply type 3 operation
(i.e. Decrease both of Y and Z by 1, then add
A[2]*X*Y*Z to sum) operation 3:

X = 10, Y = 10, Z = 10 Apply type 3 operation


sum = sum + 10*10*10*10 = 9990 (i.e. Decrease both of Y and Z by 1, then add A[3]*X*Y*Z
to sum)
X = 2, Y = 1, Z = 2
Hence, answer is the final value of sum i.e. sum = 9990.

sum = sum + 3*2*1*2 = 35


Sample Input-3:

3
Hence, answer is the final value of sum i.e. sum = 35.
3
3
3
1
2
3
1
2
3

Sample output-3:

35

Explanation-3:

Here, N = 3, X = 3, Y = 3, Z = 3

A = [1, 2, 3]
B = [1, 2, 3]

It is given that in starting, sum = 0

operation 1:
Apply type 1 operation
(i.e. Subtract B[1] from sum.)

sum = sum - 1 = -1

operation 2:

External Document © 2024 Infosys Limited


3
Sample 3 : Hard 4
3
You are given a tree with n nodes rooted at node 1. You are also
3
given an array color representing the colour of each node in the
tree.
Sample output-1:
A set of nodes is beautiful if it satisfies the following conditions:
5
• All nodes in the set have different colors.
Sample Explanation - 1:
• For any pair of nodes (u, v), either u is the ancestor of v or v is
the ancestor of u within the tree. Here, n = 5
p = [0, 1, 2, 1, 3]
You're given q queries where each query provides an
color = [4, 3, 4, 3, 5]
integer s representing a node in the tree.
q = 3
The answer to each query is the maximum size of a beautiful queries = [4, 3, 3]
set that can be formed by selecting nodes from the subtree
for query 1:
rooted at node s.
s = 4, means we need to select beautiful set of maximum size
Find the sum of answers to all queries. Since answer can be in the subtree of node 4.
large, return it modulo 109+7. we can select nodes {4} to form beautiful set of maximum size.

Notes: so, answer for this query is 1.

• The parent of node 1 is 0.


for query 2:
Input Format s = 3, means we need to select beautiful set of maximum size in
the subtree of 3.
• The first line contains an integer, N, denoting the number of
we can select nodes {3, 5} to form beautiful set of maximum
operations.
size.
• The next line contains an integer, X. so, answer for this query is 2.

• The next line contains an integer,Y.


for query 3:
• The next line contains an integer, Z.
s = 3, means we need to select beautiful set of maximum size in
the subtree of 3.
• Each line i of the N subsequent lines (where 1 ≤ i ≤ N)
contains an integer describing A[i]. we can select nodes {3, 5} to form beautiful set of maximum
size.
• Each line i of the N subsequent lines (where 1 ≤ i ≤ N) so, answer for this query is 2.
contains an integer describing B[i].

Constraints Hence, answer is 1 + 2 + 2 = 5.

• 1 <= 𝑁 <= 10#

• 1 <= 𝑿 <= 10# Sample input-2:


5
• 1 <= 𝒀 <= 10# 0
1
• 1 <= 𝒁 <= 10# 1
2
• 1 <= 𝑨[𝒊] <= 10$ 2
1
• 1 <= 𝑩[𝒊] <= 10" 5
4
Sample Input-1:
5
5 2
0 3
1 5
2 4
1 3
3
4
3
4 Sample output-2:
3
5 3
Sample Explanation - 2: 4
5
Here, n = 5 1
p = [0, 1, 1, 2, 2]

color = [1, 5, 4, 5, 2] Sample output-3:

q = 3 5
queries = [5, 4, 3] Sample Explanation - 3:

for query 1: Here, n = 5

s = 5, means we need to select beautiful set of maximum p = [0, 1, 1, 1, 3]


size in the subtree of node 5.
color = [5, 5, 5, 1, 5]
we can select nodes {5} to form beautiful set of maximum
q = 4
size.
queries = [2, 4, 5, 1]
so, answer for this query is 1.

for query 2:
for query 1:
s = 4, means we need to select beautiful set of maximum
size in the subtree of node 4. s = 2, means we need to select beautiful set of maximum size
in the subtree of node 2.
we can select nodes {4} to form beautiful set of maximum
size. we can select nodes {2} to form beautiful set of maximum size.

so, answer for this query is 1. so, answer for this query is 1.

for query 3: for query 2:

s = 3, means we need to select beautiful set of maximum s = 4, means we need to select beautiful set of maximum size in
size in the subtree of node 3. the subtree of 4.

we can select nodes {3} to form beautiful set of maximum we can select nodes {4} to form beautiful set of maximum size.
size.
so, answer for this query is 1.
so, answer for this query is 1.
for query 3:

s = 5, means we need to select beautiful set of maximum size in


Hence, answer is 1 + 1 + 1 = 3. the subtree of 5.

Sample Input-3: we can select nodes {5} to form beautiful set of maximum size.
5
0 so, answer for this query is 1.
1
for query 4:
1
1 s = 1, means we need to select beautiful set of maximum size
3
5 in the subtree of 1.
5
we can select nodes {1, 4} to form beautiful set of maximum size.
5
1 so, answer for this query is 2.
5
4 Hence, answer is 1 + 1 + 1 + 2 = 5.
2

For more information, contact [email protected]

© 2024 Infosys Limited, Bengaluru, India. All Rights Reserved. Infosys believes the information in this document is accurate as of its publication date; such information is subject to change without notice. Infosys
acknowledges the proprietary rights of other companies to the trademarks, product names and such other intellectual property rights mentioned in this document. Except as expressly permitted, neither this
documentation nor any part of it may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, printing, photocopying, recording or otherwise, without the
prior permission of Infosys Limited and/ or any named intellectual property rights holders under this document.

Infosys.com | NYSE: INFY Stay Connected

You might also like