You bought a new gaming laptop and like playing a game on it all day. It requires frequent charging due to high graphics usage. You want to know how much battery will be left after a number of playing and charging events.
Write a function that receives n records of the laptop's battery consumption and charging events. An events[i] value represents the number of minutes spent charging the laptop (positive value) or playing a game (negative value) . Every minute, the laptop charges 1% or loses 1% of its energy. The battery's charge cannot go over 100%. Return the laptop's final charge percentage given that the initial charge is 50%.
Example
n = 4
events = [10, -20, 61, -15]
Initially the laptop is charged to 50%. It is plugged in for events[0] = 10 minutes and is charged to 50 + 10 = 60. A game is played for 20 minutes at events[1] = -20 bringing the charge to 40. Charging for another 61 minutes stops when the charge reaches 100, then 15 minutes of play results in a final charge of 85.
Function Description
Complete the function getBattery in the editor below. The function must return an integer.
getBattery has the following parameter:
events[events[0],...events[n-1]]: an array of integers
Constraints
1 ≤ n ≤ 105
-100 ≤ events[i] ≤ 100
The battery charge never goes negative.
Input Format For Custom Testing
The first line contains an integer, n, denoting the number of elements in events.Each i line of the n subsequent lines (where 0 ≤ i < n) contains an integer describing events[i].
Sample Case 0
Sample Input For Custom Testing
4
25
-30
70
-10
Sample Output
90
Explanation
n = 4
events = [25, -30, 70, 10]
The battery starts at charge = 50%.
It is charged for 25 minutes, charge = 75%.
Then it is used for 30 minutes, charge = 45%.
After that, it is charged for 70 minutes, charge = 100% since it cannot charge over that.
Finally, it is used for 10 minutes, charge = 90%.
Sample Case 1
Sample Input For Custom Testing
3
-10
60
10
100
Explanation
n = 3
events = [-10, 60, 10]
The battery starts at 50%.
It is used for 10 minutes, charge = 40%.
Then it is charged for 60 minutes, charge = 100%.
Finally, it is used for 10 minutes, charge = 100%.
Solution :
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
class Result {
/*
* Complete the 'getBattery' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY events as parameter.
*/
public static int getBattery(List<Integer> events) {
// Write your code here
//Iterator i=events.iterator();
int t=50;
for(int k : events)
{
t+=k;
if(t>100)
{
t=100;
}
}
return t;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int eventsCount = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> events = new ArrayList<>();
for (int i = 0; i < eventsCount; i++) {
int eventsItem = Integer.parseInt(bufferedReader.readLine().trim());
events.add(eventsItem);
}
int result = Result.getBattery(events);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}