There are n particles present in a space and an array, initialEnergy[n]. The
finalEnergy[i] of the ith particle is max(initialEnergy[i] - barrier, 0).
The goal is to find the maximum possible integer value of the barrier such that the sum of final energies of the
particles is greater than or equal to a given threshold th.
Complete the function getMaxBarrier in the editor.
getMaxBarrier has the following parameter(s):
- 1.
int initialEnergy[n]: the initial energies of the particles - 2.
int th: the threshold
Returns
int: the maximum integer value of the barrier such that the sum of energies of
all the particles is greater than or equal to th
initialEnergy = [4, 8, 7, 2, 1] th = 9 return = 3

initialEnergy = [5, 2, 13, 10] th = 8 return = 7
initialEnergy = [3, 9, 7] th = 6 return = 5

2 ≤ n ≤ 105
1 ≤ initialEnergy[i] ≤ 109
1 ≤ th ≤ 1014
It is guaranteed that a non-negative value of barrier will exist in each case, i.e., ∑initialEnergy[i] for all i from 0 to n-1 is greater than or equal to threshold th.
- Effective Role PrivilegesPHONE SCREEN · Seen May 2026
- Closest Bathroom / Desk on a GridPHONE SCREEN · Seen May 2026
- Minimum Clicks Between Wiki PagesOA · Seen May 2026
- Minimum Index Distance Between Person and CakeOA · Seen May 2026
- Simple Array Rotation GameSeen Apr 2026
- Max Element Indexes After RotationsOA · Seen Mar 2026
- String Formation (Also for AI/ML Software Engineer Intern :)OA · Seen Mar 2026
- Grid Traversal (Infrastructure Automation Internship)Seen May 2025
public int getMaxBarrier(int[] initialEnergy, int th) {
// write your code here (solution hint: binary search + prefixsum)
}