Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Kadane Algorithm for maximum sub-array

import java.io.*;
// Java program to print largest contiguous array sum
import java.util.*;
 
class Kadane
{
    public static void main (String[] args)
    {
        int [] a = {-2, -3, 4, -1, -2, 1, 5, -3};
        System.out.println("Maximum contiguous sum is " +
                                       maxSubArraySum(a));
    }
 
    static int maxSubArraySum(int a[])
    {
        int size = a.length;
        int max_so_far = Integer.MIN_VALUE, max_ending_here = 0;
 
        for (int i = 0; i < size; i++)
        {
            max_ending_here = max_ending_here + a[i];
            if (max_so_far < max_ending_here)
                max_so_far = max_ending_here;
            if (max_ending_here < 0)
                max_ending_here = 0;
        }
        return max_so_far;
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java komplettes array ausgeben 
Java :: Creategoogle maps marker with custom image/bitmap 
Java :: list in list 
Java :: input method manager hide keyboard 
Java :: how to accept only numbers and whitespace in java 
Java :: what is java.io example 
Java :: prime numbers most efficient algorithm java 
Java :: thread dump 
Java :: polymorphism array with abstract class java 
Java :: tick mark android 
Java :: Java public no-arg constructors 
Java :: calculate tip and sales tax function 
Java :: Set value of input in android webview 
Java :: using addition to multiply in java 
Java :: pattern exercises for java 
Java :: interview questions on ds and algo 
Java :: banner generator spring boot 
Java :: java 2d array previous object 
Java :: print jtable in java 
Java :: FirebaseCrashlyticsPlugin.java uses or overrides a deprecated API. 
Java :: how to set to nothing a ComboBox in java 
Java :: add dynamic view in android from xml 
Java :: with uses in python 
Java :: Unrolling java - after 
Java :: java see you next happy year 
Java :: java scan next into array 
Java :: android notification addaction example 
Java :: current currency rates api 
Java :: java.sql.SQLException: Invalid column index 
Java :: decision tree drools using spring boot 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =