From e27790f67909358c7223de49230936198d1d4443 Mon Sep 17 00:00:00 2001 From: Suraj Sahani Date: Thu, 10 Feb 2022 12:38:55 +0530 Subject: [PATCH] Updating Solution Kadane's algoritham --- src/dynamicprogramming/LargestSumSubarray.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/dynamicprogramming/LargestSumSubarray.java b/src/dynamicprogramming/LargestSumSubarray.java index da204f0..78aaf8b 100644 --- a/src/dynamicprogramming/LargestSumSubarray.java +++ b/src/dynamicprogramming/LargestSumSubarray.java @@ -30,8 +30,17 @@ public static int getLargestSumFromArray(int[] arr) { return maxAtEnd; } - - + public int maxSubArray(int[] nums){ + int best = Integer.MIN_VALUE; + int curr=0 + for(int num:nums + { + curr=Math.max(num, num+curr); + best=Math.max(best, curr); + } + return best; + } + public static void main(String[] args) { int[] arr = {-4, 2, -5, 1, 2, 3, 6, -5, 1}; System.out.println(getLargestSumFromArray(arr));