Chief Hopper Discussions | Algorithms | HackerRank
  • + 2 comments

    There are 99 numbers in test case #2, not just 5 :) If you go further, at some point energy will be so huge that 2*energy will be too big. if you can't see it just add debug line to your while, something like that:

    while (energy >= 0 && j <= n) {
    
         energy = 2*energy - h[j]; j++; 
    
         if (i == 1762) cout << j << ' ' << energy << '\n';
    
    }
    

    and look at the output.