Write an algorithm to find the energy produced at every second of the reaction is allowed to happen for n seconds
minInitEnergy(arr, n):
begin
initEnergy := 0
currEnergy := 0
flag := false
for i in range 0 to n, do
currEnergy := currEnergy + arr[i]
if currEnergy <= 0, then
initEnergy := initEnergy + absolute value of currEnergy + 1
currEnergy := 1
flag := true
end if
done
if flag is false, return 1, otherwise return initEnergy
end
Comments
Leave a comment