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 Implement Merge Sort Algorithm on Linked List
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Biểu thức Lambda trong Java 8 – Lambda Expressions
A Guide to the Java LinkedList
Java Program to Implement Binary Search Tree
Default Password Encoder in Spring Security 5
Ways to Iterate Over a List in Java
Mockito and JUnit 5 – Using ExtendWith
Spring Boot - Google OAuth2 Sign-In
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Spring Autowiring of Generic Types
Java String Conversions
Introduction to Using FreeMarker in Spring MVC
ExecutorService – Waiting for Threads to Finish
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Spring Boot - Hystrix
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Java Program to Implement the Monoalphabetic Cypher
Lập trình mạng với java
Java Program to Implement Stack using Two Queues
Guide to Java 8 groupingBy Collector
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
The Thread.join() Method in Java
Java Program to Perform LU Decomposition of any Matrix
XML-Based Injection in Spring
Spring REST API with Protocol Buffers
How to Use if/else Logic in Java 8 Streams
Java Program to Implement Lloyd’s Algorithm
How to Set TLS Version in Apache HttpClient
Java Program to Represent Graph Using Adjacency Matrix
Easy Ways to Write a Java InputStream to an OutputStream