Kadane Algorithm

/** Author : Siddhant Swarup Mallick
 * Github : https://github.com/siddhant2002
 */

/** Program description - To find the maximum subarray sum */
 package com.thealgorithms.dynamicprogramming;

public class KadaneAlgorithm {
    public static boolean max_Sum(int a[] , int predicted_answer)
    {
        int sum=a[0],running_sum=0;
        for(int k:a)
        {
            running_sum=running_sum+k;
            // running sum of all the indexs are stored
            sum=Math.max(sum,running_sum);
            // the max is stored inorder to the get the maximum sum
            if(running_sum<0)
            running_sum=0;
            // if running sum is negative then it is initialized to zero
        }
        // for-each loop is used to iterate over the array and find the maximum subarray sum
        return sum==predicted_answer;
        // It returns true if sum and predicted answer matches
        // The predicted answer is the answer itself. So it always return true
    }
    /**
     * OUTPUT :
     * Input - {89,56,98,123,26,75,12,40,39,68,91}
     * Output: it returns either true or false
     * 1st approach Time Complexity : O(n)
     * Auxiliary Space Complexity : O(1)
     */
}
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.