GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java/Dynamic Programming/KadaneAlgorithm.java at master · longwu/Java Expand file tree/ Copy pathKadaneAlgorithm.javaMore file actions 55 lines (50 loc) · 1.02 KB / Copy pathKadaneAlgorithm.javaFile metadata and controls55 lines (50 loc) · 1.02 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 import java.util.Scanner; /** * Program to implement Kadane’s Algorithm to * calculate maximum contiguous subarray sum of an array * Time Complexity: O(n) * * @author Nishita Aggarwal * */ public class KadaneAlgorithm { /** * This method implements Kadane's Algorithm * * @param arr The input array * @return The maximum contiguous subarray sum of the array * */ static int largestContiguousSum(int arr[]){ int i,len=arr.length,cursum=0,maxsum=Integer.MIN_VALUE; if(len==0) //empty array return 0; for(i=0;i<len;i++){ cursum+=arr[i]; if(cursum>maxsum){ maxsum=cursum; } if(cursum<=0){ cursum=0; } } return maxsum; } /** * Main method * * @param args Command line arguments */ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n,arr[],i; n=sc.nextInt(); arr=new int[n]; for(i=0;i<n;i++){ arr[i]=sc.nextInt(); } int maxContSum=largestContiguousSum(arr); System.out.println(maxContSum); sc.close(); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |