Mike and Gems
Updated
Problem statement
On a planet lying on the brink of the Milky Way, called AFWP-700, there is a very rare type of gem. This gem has magical properties. This gem is very expensive but it affects the person carrying it. Depending on the potency of the collected gem, it may boost a person's stamina or reduce it. Mike, a researcher on planet AFWP-700 must navigate through the planet's tough terrain in a certain path from Base A to Base B. On the way, he must collect at most n gems in the given order and then reach Base B.
Mike must go through the path and if he encounters a gem, he must either pick it up or destroy it (to avoid being affected by it). Having researched the different potencies of the gem, a numerical value is known for the potency of the gems. Mike must keep his potency non-negative throughout the trip.
Output the maximum number of gems that Mike can collect.
Input Format for Custom Testing
The first line contains an integer n, the total gems along the path.
The next line contains n space separated integers (a_i for all 1 ≤ i ≤ n).
Output Format
A single integer containing the maximum number of gems that Mike can collect without having negative stamina at any point.
Function
maxGemsCollected(gemPotencies: int[]) → intExamples
Example 1
gemPotencies = [1, 2, 3, 4, -15]return = 4Mike can collect the first four gems without any hesitations but he cannot collect the last in any possible scenario.
Example 2
gemPotencies = [1, -2, 3, -4, 0]return = 4Here, Mike will always collect the first gem, then he will skip and break the second gem, collecting the third gem, totaling a stamina of 4. Now, he can collect the last 2 gems while keeping his stamina 0, but still not negative.
Constraints
1 ≤ n ≤ 10^5-10^9 ≤ a_i ≤ 10^9
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
- Task SchedulerAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Minimum Cost to Merge Values into OnePayPal · OAPayPal interview questions
- Maximum K-Star SumC3.ai · OAC3.ai interview questions
- Maximum Sum of a K-StarMicrosoft · OAMicrosoft interview questions
- Maximum ScoreNVIDIA · OANVIDIA interview questions
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Task SchedulingJP Morgan Chase · OAJP Morgan Chase interview questions
- Decreasing SubsequencesGoogle · OAGoogle interview questions
- Get Maximum AmountIBM · OAIBM interview questions
- Optimize TikTok Reels ViewingTikTok · OATikTok interview questions