🐾 Cross the Threshold
Learn this problemProblem statement
There are n particles with initial energies given by initialEnergy. For an integer barrier, the final energy of particle i is max(initialEnergy[i] - barrier, 0).
Find the maximum non-negative integer value of barrier such that the sum of all final energies is at least the threshold th.
Complete getMaxBarrier with the following parameters:
int initialEnergy[n]: the initial energies of the particleslong th: the required energy threshold
Returns: int, the maximum barrier whose remaining total energy is at least th.
Function
getMaxBarrier(initialEnergy: int[], th: long) → intExamples
Example 1
initialEnergy = [4, 8, 7, 2, 1]th = 9return = 3With barrier = 3, the final energies are [1, 5, 4, 0, 0] and sum to 10. With barrier = 4, they sum to 7, which is below th = 9. Therefore, the maximum feasible barrier is 3.
Example 2
initialEnergy = [5, 2, 13, 10]th = 8return = 7At barrier = 7, the remaining energies are [0, 0, 6, 3] and sum to 9. At barrier = 8, the sum is 7. Thus, 7 is the largest feasible barrier.
Example 3
initialEnergy = [3, 9, 7]th = 6return = 5At barrier = 5, the remaining energies are [0, 4, 2] and sum to 6. Increasing the barrier to 6 lowers the sum to 4, so the answer is 5.
Constraints
2 ≤ n ≤ 10^51 ≤ initialEnergy[i] ≤ 10^91 ≤ th ≤ 10^14- It is guaranteed that
sum(initialEnergy) ≥ th, so a non-negative barrier always exists.
More Snowflake problems
- Minimum N-ary Tree Depth DeletionsPHONE SCREEN · Seen Jul 2026
- Simulate a Queued Multi-Rule Rate LimiterPHONE SCREEN · Seen Jul 2026
- Minimum Clicks Between Wiki PagesOA · Seen Jul 2026
- Closest Target CharacterPHONE SCREEN · Seen Jul 2026
- Horizontal Pod AutoscalerOA · Seen Jul 2026
- Minimum HeightOA · Seen Jul 2026
- Vowel SubstringOA · Seen Jun 2026
- String Formation (Also for AI/ML Software Engineer Intern :)OA · Seen Jun 2026