-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCityBiker.java
More file actions
69 lines (51 loc) · 2.17 KB
/
CityBiker.java
File metadata and controls
69 lines (51 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*Once upon a time, a city biker embarked on an exciting road trip. This journey was laid out across a sequence of n+1 points, each with varying altitudes. The biker's adventure commenced at point 0, where the altitude was a humble 0 meters above sea level.
During his expedition, he encountered different terrains. For every segment between points i and i+1, where 0≤i<n, the biker faced varying challenges in altitude. Each segment was represented by an integer array called gain of length n, where gain[i] denoted the net gain in altitude between points i and i+1.
The biker's goal was to find the highest altitude he reached during his journey. This altitude was determined by the net gain in altitude at each point along the way. It could be a thrilling peak or a serene valley, depending on the varying gains between consecutive points.
Input Format
The first line of the input contains an integer n - the number of elements.
The next line contains n space-separated integers.
Output Format
City Biker needs to return the highest altitude of a point.
Constraints
n == gain.length
1 <= n <= 100
-100 <= gain[i] <= 100
Sample Testcase 0
Testcase Input
5
-5 5 1 5 -3
Testcase Output
6
Explanation
The biker's altitude gains were as follows:
From point 0 to 1: -5 (net loss, altitude at point 1: 0+(-5)=-5)
From point 1 to 2: +5 (net gain, altitude at point 2: (-5)+5=0)
From point 2 to 3: +1 (net gain, altitude at point 3: 0+1=1)
From point 3 to 4: +5 (net gain, altitude at point 4: 1+5=6)
From point 0 to 1: -3 (net loss, altitude at point 5: 6+(-3)=3)
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
int arr[]=new int[size];
for(int i=0;i<size;i++){
arr[i]=sc.nextInt();
}
int totalSum=0,currSum=0;
for(int i=0;i<size;i++)
{
totalSum+=arr[i];
if(totalSum > currSum)
{
currSum=totalSum;
}
}
System.out.println(currSum);
}
}