Home » Arrays(Dont Mess With Me) » Kadane Algorithm

Kadane Algorithm

Advertisements

// this is KADANE ALGOL…
// The Aim of the program as the title suggest is to find the largest sum of a contiguous sequence of array elements..
// (array containing at least one positive number)
// For example
// Array {4,-9,3,-2,4,-12} the sequence {3,-2,4} is the continuous sequence with largest sum 5


import java.io.*;
public class MaxSubsum
{
 public static int MaxSum(int[] array) {
 int maxsum = 0;
 int sum = 0;
 for (int i = 0; i < array.length; i++)
 {
 sum = sum + array[i];
 if (sum > maxsum)
 {
 maxsum = sum;

 }
 else if (sum < 0)
 {
 sum = 0;
 }
 }
 return maxsum;
 }
 public static void main (String[] args) throws IOException
 {
 int[] a=new int[25];
 int num=0,i=0;
 BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
 System.out.println("Enter the Number of element");
 num=Integer.parseInt(reader.readLine());
 System.out.println("Enter the array");
 for(i=1;i<=num;i++)
 {
 a[i]=Integer.parseInt(reader.readLine());
 }
 int value = MaxSum(a);
 System.out.println("The maximum sub sum is:"+value);
 }
}

Advertisements

2 Comments

  1. Kevin Lee says:

    Hi Appit,

    Your blog is such a complete read. I like your approach with Kadane Algorithm find if each of the two arrays have the same set of integers. Clearly, you wrote it to make learning a cake walk for me.

    We’re currently in an infinite loop between sales and support, neither of whom seem to be able to understand a basic issue. This enables the industry to summon command without putting in the infrastructure at all AWS Training . AWS has several configuration management solutions for AWS scalability, flexibility, availability and management.

    We want to purchase some sizeable reserved instances but are told that the only way to pay is all at once with a credit card. No split payments, no offer to pay by check, no offer to pay by ACH, no offer to pay by wire.

    Can someone explain to me how AWS serves enterprises if they only accept consumer methods of payment?

    Super likes !!! for this amazing post. I thinks everyone should bookmark this.

    Kind Regards,
    Kevin

  2. Hallo There,

    I learnt so much in such little time about Kadane Algorithm. Even a toddler could become smart reading of your amazing articles.

    I am currently working on a project that does not include storyboards and I am trying to create a user authentication and sign-in method. I was initially going to use FireBase but they did not have Carthage support so I decided to try out AWS. I initially tried using AWSCognitoIdentityProvider framework with my custom UI but the passwordauthentication method for signing in would not trigger a result. I moved onto the AWSAuthUI framework (plus AWSAuthCore, AWSFacebookSignIn, AWSGoogleSignIn, AWSUserPoolsSignIn) with the built in UI but I keep getting the error below before even getting to the login screen.

    Anyways great write up, your efforts are much appreciated.

    Merci,
    Preethi.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: