From 050a5c1d6d6ce0304ab40aaf395f9f8eff6a0514 Mon Sep 17 00:00:00 2001 From: Vandit <52314194+vanditkhurana@users.noreply.github.com> Date: Wed, 27 Oct 2021 23:02:19 +0530 Subject: [PATCH] Committing KadanesAlgo.java --- KadanesAlgo.java | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 KadanesAlgo.java diff --git a/KadanesAlgo.java b/KadanesAlgo.java new file mode 100644 index 0000000..dc4f82f --- /dev/null +++ b/KadanesAlgo.java @@ -0,0 +1,38 @@ +class KadanesAlgo +{ + // Function to find the maximum sum of a contiguous subarray + // in a given integer array + public static int kadane(int[] A) + { + // stores the maximum sum subarray found so far + int maxSoFar = 0; + + // stores the maximum sum of subarray ending at the current position + int maxEndingHere = 0; + + // traverse the given array + for (int i: A) + { + // update the maximum sum of subarray "ending" at index `i` (by adding the + // current element to maximum sum ending at previous index `i-1`) + maxEndingHere = maxEndingHere + i; + + // if the maximum sum is negative, set it to 0 (which represents + // an empty subarray) + maxEndingHere = Integer.max(maxEndingHere, 0); + + // update the result if the current subarray sum is found to be greater + maxSoFar = Integer.max(maxSoFar, maxEndingHere); + } + + return maxSoFar; + } + + public static void main(String[] args) + { + int[] A = { -2, 1, -3, 4, -1, 2, 1, -5, 4 }; + + System.out.println("The sum of contiguous subarray with the " + + "largest sum is " + kadane(A)); + } +}