﻿ Java exercises: Find the subarray with smallest sum from a given array of integers - w3resource # Java Exercises: 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) {
ArrayList<Integer> nums = new ArrayList<Integer>();
System.out.print(min_SubArray(nums));
}
public static int min_SubArray(ArrayList<Integer> nums) {
int[] nums1 = new int[nums.size()];
nums1 = nums.get(0);
int min = nums1;
for (int i = 1; i < nums.size(); ++i) {
nums1[i] = Math.min(nums.get(i), nums.get(i) + nums1[i - 1]);
min = Math.min(min, nums1[i]);
}
return min;
}
}
```
```

Sample Output:

```-4
```

Flowchart: Java Code Editor:

What is the difficulty level of this exercise?

﻿

## Java: Tips of the Day

initial:

Returns all the elements of an array except the last one. Use Arrays.copyOfRange() to return all except the last one

```public static <T> T[] initial(T[] elements) {
return Arrays.copyOfRange(elements, 0, elements.length - 1);
}
```

Ref: https://bit.ly/3zEKw9u