Given an array containing positive and negative numbers. The array represents checkpoints from one end to other end of street. Positive and negative values represent amount of energy at that checkpoint. Positive numbers increase the energy and negative numbers decrease. Find the minimum initial energy required to cross the street such that Energy level never becomes 0 or less than 0.
Note : The value of minimum initial energy required will be 1 even if we cross street successfully without losing energy to less than and equal to 0 at any checkpoint. The 1 is required for initial check point.
Examples :
Input : arr[] = {4, -10, 4, 4, 4}
Output: 7
Suppose initially we have energy = 0, now at 1st
checkpoint, we get 4. At 2nd checkpoint, energy gets
reduced by -10 so we have 4 + (-10) = -6 but at any
checkpoint value of energy can not less than equals
to 0. So initial energy must be at least 7 because
having 7 as initial energy value at 1st checkpoint
our energy will be = 7+4 = 11 and then we can cross
2nd checkpoint successfully. Now after 2nd checkpoint,
all checkpoint have positive value so we can cross
street successfully with 7 initial energy.
Input : arr[] = {3, 5, 2, 6, 1}
Output: 1
We need at least 1 initial energy to reach first
checkpoint
Input : arr[] = {-1, -5, -9}
Output: 16
Brute force approach :
- For every possible initial energy level (starting from 1), simulate the crossing of the street using that energy level and check if the energy level remains positive at all times.
- Return the minimum initial energy level that ensures that the energy level never becomes zero or negative.
Below is the code for above approach :
C++
#include<bits/stdc++.h>
using namespace std;
bool check( int arr[], int n, int initEnergy) {
int energy = initEnergy;
for ( int i = 0; i < n; i++) {
energy += arr[i];
if (energy <= 0) {
return false ;
}
}
return true ;
}
int minInitialEnergy( int arr[], int n) {
int minEnergy = 1;
while (!check(arr, n, minEnergy)) {
minEnergy++;
}
return minEnergy;
}
int main() {
int arr[] = {4, -10, 4, 4, 4};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << minInitialEnergy(arr, n);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static boolean check( int [] arr, int n, int initEnergy)
{
int energy = initEnergy;
for ( int i = 0 ; i < n; i++) {
energy += arr[i];
if (energy <= 0 ) {
return false ;
}
}
return true ;
}
static int minInitialEnergy( int [] arr, int n)
{
int minEnergy = 1 ;
while (!check(arr, n, minEnergy)) {
minEnergy++;
}
return minEnergy;
}
public static void main(String[] args)
{
int [] arr = { 4 , - 10 , 4 , 4 , 4 };
int n = arr.length;
System.out.println(minInitialEnergy(arr, n));
}
}
|
Python3
def check(arr, n, initEnergy):
energy = initEnergy
for i in range (n):
energy + = arr[i]
if energy < = 0 :
return False
return True
def minInitialEnergy(arr, n):
minEnergy = 1
while not check(arr, n, minEnergy):
minEnergy + = 1
return minEnergy
arr = [ 4 , - 10 , 4 , 4 , 4 ]
n = len (arr)
print (minInitialEnergy(arr, n))
|
C#
using System;
namespace EnergyCheck
{
class GFG
{
static bool Check( int [] arr, int n, int initEnergy)
{
int energy = initEnergy;
for ( int i = 0; i < n; i++)
{
energy += arr[i];
if (energy <= 0)
{
return false ;
}
}
return true ;
}
static int MinInitialEnergy( int [] arr, int n)
{
int minEnergy = 1;
while (!Check(arr, n, minEnergy))
{
minEnergy++;
}
return minEnergy;
}
static void Main( string [] args)
{
int [] arr = { 4, -10, 4, 4, 4 };
int n = arr.Length;
Console.WriteLine(MinInitialEnergy(arr, n));
}
}
}
|
Javascript
function check(arr, n, initEnergy) {
let energy = initEnergy;
for (let i = 0; i < n; i++) {
energy += arr[i];
if (energy <= 0) {
return false ;
}
}
return true ;
}
function minInitialEnergy(arr, n) {
let minEnergy = 1;
while (!check(arr, n, minEnergy)) {
minEnergy++;
}
return minEnergy;
}
let arr = [4, -10, 4, 4, 4];
let n = arr.length;
console.log(minInitialEnergy(arr, n));
|
Output :
7
Time Complexity : O(2^n)
Auxiliary Space : O(n)
We take initial minimum energy 0 i.e; initMinEnergy = 0 and energy at any checkpoint as currEnergy = 0. Now traverse each checkpoint linearly and add energy level at each i’th checkpoint i.e; currEnergy = currEnergy + arr[i]. If currEnergy becomes non-positive, then we need at least “abs(currEnergy) + 1” extra initial energy to cross this point. Therefore we update initMinEnergy = (initMinEnergy + abs(currEnergy) + 1). We also update currEnergy = 1 as we now have the required extra minimum initial energy for next point.
Below is the implementation of above idea.
C++
#include<bits/stdc++.h>
using namespace std;
int minInitialEnergy( int arr[], int n)
{
int initMinEnergy = 0;
int currEnergy = 0;
bool flag = 0;
for ( int i=0; i<n; i++)
{
currEnergy += arr[i];
if (currEnergy <= 0)
{
initMinEnergy += abs (currEnergy) +1;
currEnergy = 1;
flag = 1;
}
}
return (flag == 0)? 1 : initMinEnergy;
}
int main()
{
int arr[] = {4, -10, 4, 4, 4};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << minInitialEnergy(arr, n);
return 0;
}
|
Java
class GFG {
static int minInitialEnergy( int arr[], int n)
{
int initMinEnergy = 0 ;
int currEnergy = 0 ;
boolean flag = false ;
for ( int i = 0 ; i < n; i++) {
currEnergy += arr[i];
if (currEnergy <= 0 ) {
initMinEnergy += Math.abs(currEnergy) + 1 ;
currEnergy = 1 ;
flag = true ;
}
}
return (flag == false ) ? 1 : initMinEnergy;
}
public static void main(String[] args)
{
int arr[] = { 4 , - 10 , 4 , 4 , 4 };
int n = arr.length;
System.out.print(minInitialEnergy(arr, n));
}
}
|
Python3
def minInitialEnergy(arr):
n = len (arr)
initMinEnergy = 0 ;
currEnergy = 0
flag = 0
for i in range (n):
currEnergy + = arr[i]
if currEnergy < = 0 :
initMinEnergy + = ( abs (currEnergy) + 1 )
currEnergy = 1
flag = 1
return 1 if flag = = 0 else initMinEnergy
arr = [ 4 , - 10 , 4 , 4 , 4 ]
print (minInitialEnergy(arr))
|
C#
using System;
class GFG {
static int minInitialEnergy( int []arr, int n)
{
int initMinEnergy = 0;
int currEnergy = 0;
bool flag = false ;
for ( int i = 0; i < n; i++) {
currEnergy += arr[i];
if (currEnergy <= 0)
{
initMinEnergy += Math.Abs(currEnergy) + 1;
currEnergy = 1;
flag = true ;
}
}
return (flag == false ) ? 1 : initMinEnergy;
}
public static void Main()
{
int []arr = {4, -10, 4, 4, 4};
int n = arr.Length;
Console.Write(minInitialEnergy(arr, n));
}
}
|
Javascript
<script>
function minInitialEnergy(arr, n) {
let initMinEnergy = 0;
let currEnergy = 0;
let flag = 0;
for (let i = 0; i < n; i++) {
currEnergy += arr[i];
if (currEnergy <= 0) {
initMinEnergy += Math.abs(currEnergy) + 1;
currEnergy = 1;
flag = 1;
}
}
return (flag == 0) ? 1 : initMinEnergy;
}
let arr = new Array(4, -10, 4, 4, 4);
let n = arr.length;
document.write(minInitialEnergy(arr, n));
</script>
|
PHP
<?php
function minInitialEnergy( $arr , $n )
{
$initMinEnergy = 0;
$currEnergy = 0;
$flag = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$currEnergy += $arr [ $i ];
if ( $currEnergy <= 0)
{
$initMinEnergy += abs ( $currEnergy ) + 1;
$currEnergy = 1;
$flag = 1;
}
}
return ( $flag == 0) ? 1 : $initMinEnergy ;
}
$arr = array (4, -10, 4, 4, 4);
$n = sizeof( $arr );
echo minInitialEnergy( $arr , $n );
?>
|
Time Complexity : O(n)
Auxiliary Space : O(1)
This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.