Skip to main content
All Questions

Compute the optimal score you can guarantee in a two-player sorted-container game.

Unlock detailed company stats for this questionUpgrade

Given an integer N, an array F of size N and Q queries, each with integers G and R. G sets the game type: G = 0 means you must move if a valid move exists on your turn; G = 1 means you may skip your turn any number of times. R sets the range: only F[0] to F[R-1] participate.

You and a friend alternate turns and you play first. On your turn you select any element from the current range, remove it from F and add it to a container C, allowed only if C remains sorted in non-decreasing order. On your friend's turn they remove any one element from the range; your friend plays optimally to minimize your final score. If G = 0 and you have no legal element to add on your turn, you lose and your score is 0. The game continues until no elements remain in F[0..R-1]; if you get through to the end, your score is the sum of the elements in C. You maximize your score, the friend minimizes it.

For every query (G, R), print the optimal score you can guarantee on its own line.

Input: N Q, then F[0..N-1], then Q lines of G R. Example: input '6 1 / 10 20 30 40 50 60 / 0 3' gives output 30.

Format · Online assessment.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.