﻿ Java - Find the subarray with smallest sum from an array

# Java: Find the subarray with smallest sum from a given array of integers.

## Java Basic: Exercise-123 with Solution

Write a Java program to find the subarray with smallest sum from a given array of integers.

Pictorial Presentation: Sample Solution:

Java Code:

``````import java.util.*;

public class Main {
public static void main(String[] args) {
// Create an ArrayList to store integers
ArrayList nums = new ArrayList();
// Call the min_SubArray function and print the result
System.out.print(min_SubArray(nums));
}

public static int min_SubArray(ArrayList nums) {
// Create an array to store the same integers for dynamic programming
int[] nums1 = new int[nums.size()];
nums1 = nums.get(0);
// Initialize the minimum value to the first element
int min = nums1;
// Loop through the ArrayList to calculate minimum subarray sum
for (int i = 1; i < nums.size(); ++i) {
// Calculate the minimum of the current element and the sum of the previous subarray
nums1[i] = Math.min(nums.get(i), nums.get(i) + nums1[i - 1]);
// Update the minimum value if needed
min = Math.min(min, nums1[i]);
}
// Return the minimum subarray sum
return min;
}
}
```
```

Sample Output:

```-4
```

Flowchart: Java Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿