diff --git a/Dynamic Programming/KadaneAlgorithm.java b/Dynamic Programming/KadaneAlgorithm.java new file mode 100644 index 000000000000..56d0ec6ff742 --- /dev/null +++ b/Dynamic Programming/KadaneAlgorithm.java @@ -0,0 +1,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;imaxsum){ + 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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy