Created
August 16, 2020 10:54
-
-
Save vinodjayachandran/fd0bf7698eb6ac41002bb641fe7ec5e5 to your computer and use it in GitHub Desktop.
Kadane's Algorithm - Given an array of N integers, Find the maximum sum in a contiguous sub-array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Kadanes { | |
public static void main(String args[]) { | |
int max_value = Integer.MIN_VALUE; | |
// Convert input to string array | |
int [] inputArray = new int [args.length]; | |
for (int i = 0; i < args.length; i++) { | |
inputArray[i] = Integer.parseInt(args[i]); | |
} | |
int max_value_so_far = 0; | |
for (int i = 0; i < inputArray.length; i++) { | |
max_value_so_far = max_value_so_far + inputArray[i]; | |
if(max_value_so_far>max_value) { | |
max_value=max_value_so_far; | |
} | |
if(max_value_so_far<0) { | |
max_value_so_far = 0; | |
} | |
} | |
System.out.println(" Maximum Value is " + max_value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment