aboutsummaryrefslogtreecommitdiff
path: root/MaxSubArray.java
diff options
context:
space:
mode:
Diffstat (limited to 'MaxSubArray.java')
-rw-r--r--MaxSubArray.java19
1 files changed, 19 insertions, 0 deletions
diff --git a/MaxSubArray.java b/MaxSubArray.java
new file mode 100644
index 0000000..89698c2
--- /dev/null
+++ b/MaxSubArray.java
@@ -0,0 +1,19 @@
+import java.lang.*;
+import java.util.*;
+
+class MaxSubArray {
+ public static int maxSubArray(int[] nums) {
+ int currSum = 0;
+ int maxSum = 0;
+ for (int i : nums) {
+ currSum = Math.max(currSum + i, 0);
+ maxSum = Math.max(maxSum, currSum);
+ }
+ return maxSum;
+ }
+
+ public static void main(String[] args) {
+ int[] nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
+ System.out.println(maxSubArray(nums));
+ }
+}