Java Program to find the maximum subarray sum O(n^2) time(naive method)

This is a Java Program to find maximum subarray sum of an array. A subarray is a continuous portion of an array. The time complexity of the following program is O (n2).

Here is the source code of the Java program to find maximum subarray sum. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/*
 * Java Program to Find the maximum subarray sum O(n^2)time 
 * (naive method)
 */
import java.util.Scanner;
 
public class MaxSubarraySum1
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter number of elements in array");
        int N = scan.nextInt();
        int[] arr = new int[ N ];
        /* Accept N elements */
        System.out.println("Enter "+ N +" elements");
        for (int i = 0; i < N; i++)
            arr[i] = scan.nextInt();
        System.out.println("Max sub array sum  = "+ max_sum(arr));
    }
    public static int max_sum(int[] arr)
    {
        int N = arr.length, max = Integer.MIN_VALUE;
        for (int i = 0; i < N; i++)
        {
            int sum = 0;
            for (int j = i; j < N; j++)
            {
                sum += arr[j];
                if (sum > max)
                    max = sum;
            }
        }
        return max;    
    }
}
Enter number of elements in array
8
Enter 8 elements
-2 -5 6 -2 -3 1 5 -6
Max sub array sum  = 7

Related posts:

Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
What is Thread-Safety and How to Achieve it?
Java Program to Show the Duality Transformation of Line and Point
Custom Thread Pools In Java 8 Parallel Streams
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Introduction to Project Reactor Bus
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
A Guide to Iterator in Java
Convert a Map to an Array, List or Set in Java
Java Program to Generate Date Between Given Range
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Đồng bộ hóa các luồng trong Java
Java Program to Implement Flood Fill Algorithm
Java Program to Implement Sieve Of Sundaram
A Guide to Concurrent Queues in Java
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Spring Boot - Logging
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Inject Parameters into JUnit Jupiter Unit Tests
Guide to java.util.concurrent.Future
Java Program to Implement Red Black Tree
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Quick Guide to @RestClientTest in Spring Boot
Rate Limiting in Spring Cloud Netflix Zuul
Guide to the Java ArrayList
Java Program to Implement Rope
New Features in Java 11
Java Program to Implement Interpolation Search Algorithm
Jackson – Decide What Fields Get Serialized/Deserialized
Java Program to Find the Minimum value of Binary Search Tree
Spring Boot - Tracing Micro Service Logs
Java Program to Implement Stack