From 7af4b0ef450ed8b6e95af49030486d8458d36aa6 Mon Sep 17 00:00:00 2001 From: gulatikeshav <133638164+gulatikeshav@users.noreply.github.com> Date: Sat, 21 Oct 2023 09:30:47 +0530 Subject: [PATCH] Added kadanesAlgo.java --- kadanesAlgo.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 kadanesAlgo.java diff --git a/kadanesAlgo.java b/kadanesAlgo.java new file mode 100644 index 0000000..821c9b7 --- /dev/null +++ b/kadanesAlgo.java @@ -0,0 +1,32 @@ +import java.util.Scanner; + +public class kadanesAlgo { + +public static int kadanes(int numbers[]){ + int maximumSum = Integer.MIN_VALUE; + int currentSum = 0; + + for(int i = 0; i < numbers.length; i++){ + currentSum = currentSum + numbers[i]; + if(currentSum < 0){ + currentSum = 0; + } + maximumSum = Math.max(currentSum ,maximumSum); + } + return maximumSum; +} + +public static void main(String args[]){ + Scanner sc = new Scanner(System.in); + + int numbers[] = {-2, -3, 4, -1, -2, 1, 5, -3}; + + int maxSum = kadanes(numbers); + System.out.println("Maximum Sum of Sub-Array is : " + maxSum); + + + sc.close(); +} +} + +