Salvage Humankind
Updated
Problem statement
Cooper is stuck in the 5th dimension with TARS. They have to find a way to transmit gravitational data to Murph so that she can solve Dr. Brand's equation to harness gravity. The higher dimensional beings agreed to help them out, but only if they are able to solve a task for them.
Cooper and TARS are presented with n rocks. Each rock has a mass attached to it
(m1, m2, m3, m4, . . . mn). They can perform two types of operations on this
ordered set of rocks:
- Given two indexes
xandy, find the total mass of the rocks with indices in the rangextoy, i.e,m_x + m_x+1 + ... + m_yand print it. - They are also presented with a machine capable of altering the mass of any
rock. Presented again with two indexes
xandy, and a numberz, alter the masses of the rocks with indices in the rangextoysuch thatm_i(x <= i <= y) gets altered tom_i ^ z, where^is the xor operation.
They have been asked to perform ops number of operations, where each operation
can either be of the type 1 or 2.
Input Format and Constraints
- An integer
n, denoting the number of rocks. 1 ≤ n ≤ 10^5. - The second line contains
nintegers denoting the initial masses of the rocks. 0 ≤ m[i] ≤ 10^6. - The third line contains the number of operations,
ops, to be performed on the rocks. 1 ≤ ops ≤ 5*10^4. - The above is followed by
opsnumber of lines, where each line consists of:- An integer
type, which is either 1 or 2, denoting the operation performed. - If
typeis 1, it is followed by two integers,sande, denoting the range for which total mass is to be calculated. 1 ≤ s ≤ e ≤ n. - If
typeis 2, it is followed by three integers,s,e, andz.zis the number using which the mass of the rocks in the rangestoeis to be altered using^which is the xor operation. 1 ≤ s ≤ e ≤ n and 1 ≤ z ≤ 10^6. The changes made in this query persist for all future operations as well.
- An integer
Function
salvageHumankind(n: int, masses: int[], ops: int, queries: int[][]) → long[]Examples
Example 1
n = 4masses = [1, 5, 2, 4]ops = 3queries = [[1, 1, 4], [2, 2, 3, 4], [1, 1, 4]]return = [12, 12]- In the first query of type 1, the sum of masses of rocks in the range 1 to 4 is asked for, hence 12 is printed.
- In the second query of type 2, the masses of rocks in the range 2 to 3 is modified by performing xor with 4. The new array becomes -> 1 1 6 4.
- In the third query of type 1, the masses of rocks in the range 1 to 4 is asked for, hence, 12 is printed.
Example 2
n = 5masses = [10, 6, 1, 9, 2]ops = 3queries = [[1, 1, 5], [2, 1, 3, 8], [1, 2, 4]]return = [28, 32]- In the first query of type 1, the sum of masses of rocks in the range 1 to 5 is asked for, hence 28 is printed.
- In the second query of type 2, the masses of rocks in the range 1 to 3 is modified by performing xor with 8. The new array becomes -> 2 14 9 9 2.
- In the third query of type 1, the masses of rocks in the range 2 to 4 is asked for, hence, 32 is printed.
More Rubrik interview questions
- Colored Rooted TreeONSITE INTERVIEW · Seen Nov 2026
- Delete and Freeze GameOA · Seen Nov 2026
- Delete K Subsets for Maximum ScoreOA · Seen Nov 2026
- Distribute Elements Into Two Arrays IIOA · Seen Nov 2026
- Minimum Replacements to Sort the ArrayOA · Seen Nov 2026
- All Balanced Strings from Minimum Parenthesis InsertionsONSITE INTERVIEW · Seen Nov 2026
- Insert into a Left-Complete Binary TreeONSITE INTERVIEW · Seen Oct 2026
- Thread-Safe Request Counts by AttributeONSITE INTERVIEW · Seen Oct 2026
- Minimum Swaps To Binary PalindromeOA · Seen Apr 2026
- Get Minimum OperationsOA · Seen Nov 2024
Similar questions at other companies
- Maximize Shipment Order by Sorting One WindowAmazon · OA
- Palindromic Paths to the RootRippling · OARippling interview questions
- Optimize Discount Selection Under a Point BudgetPalantir · OAPalantir interview questions
- Count Prefix Subsets with a Given XORGoDaddy · OA
- Count Pairs with K Divisible Values in the IntervalChubb · OAChubb interview questions
- Maximum Subarray with Start and End WindowsInMobi · OAInMobi interview questions
- Maximum Index-Weighted Sum with Disjoint SwapsVisa · OAVisa interview questions
- Submask Sum QueriesIntuit · OAIntuit interview questions
- Competitive ForagingAkuna Capital · OAAkuna Capital interview questions
- Most Items Within Budget from a Start IndexHudson River Trading · OAHudson River Trading interview questions