-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxSubarraySum.java
More file actions
49 lines (41 loc) · 1.4 KB
/
Copy pathMaxSubarraySum.java
File metadata and controls
49 lines (41 loc) · 1.4 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
import java.util.Scanner;
public class MaxSubarraySum {
public static void main(String[] args) {
// Input array
int[] nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
// Find and print the maximum subarray sum
System.out.print(max_SubArray(nums));
}
public static int max_SubArray(int[] nums) {
// Check if the input array is empty
if (nums.length < 1) {
return 0;
}
// Initialize variables to track the maximum sum, its start and end indices
int max = nums[0];
int max_Begin = 0;
int max_End = 0;
int begin = 0;
int end = 0;
int sum = 0;
while (end < nums.length) {
// Update the current sum with the value at the current end index
sum += nums[end];
if (sum < 0) {
// If the current sum becomes negative, reset it and update the beginning index
sum = 0;
begin = end + 1;
} else {
// If the current sum is greater than the maximum, update the maximum
if (sum > max) {
max = sum;
max_Begin = begin;
max_End = end;
}
}
end++;
}
// Return the maximum sum of the subarray
return max;
}
}