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:

Spring Boot - Creating Docker Image
Tính đa hình (Polymorphism) trong Java
Marker Interface trong Java
Giới thiệu Google Guice – Binding
Java Timer
Netflix Archaius with Various Database Configurations
Most commonly used String methods in Java
Java Program to Find Nearest Neighbor for Static Data Set
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Spring WebClient Filters
DynamoDB in a Spring Boot Application Using Spring Data
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Interface trong Java 8 – Default method và Static method
Java Program to Find a Good Feedback Vertex Set
Java Program to Perform Insertion in a BST
What is Thread-Safety and How to Achieve it?
Spring – Injecting Collections
Java Program to Implement Doubly Linked List
Spring Boot - Build Systems
Registration – Password Strength and Rules
Java Program to Implement Miller Rabin Primality Test Algorithm
Get and Post Lists of Objects with RestTemplate
Apache Commons Collections MapUtils
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
XML Serialization and Deserialization with Jackson
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Lớp Collections trong Java (Collections Utility Class)
Display Auto-Configuration Report in Spring Boot
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Instance Profile Credentials using Spring Cloud