-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
59 lines (47 loc) · 1.54 KB
/
Copy pathMain.java
File metadata and controls
59 lines (47 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.*;
class Solution {
// Function to find maximum sum of subarrays
public int maxSubArray(int[] nums) {
/*
* Initialize maximum sum with
* the smallest possible integer
*/
int maxi = Integer.MIN_VALUE;
// Iterate over each starting index of subarrays
for (int i = 0; i < nums.length; i++) {
/*
* Iterate over each ending index
* of subarrays starting from i
*/
for (int j = i; j < nums.length; j++) {
/*
* Variable to store the sum
* of the current subarray
*/
int sum = 0;
// Calculate the sum of subarray nums[i...j]
for (int k = i; k <= j; k++) {
sum += nums[k];
}
/*
* Update maxi with the maximum of its current
* value and the sum of the current subarray
*/
maxi = Math.max(maxi, sum);
}
}
// Return the maximum subarray sum found
return maxi;
}
}
// Separate Main class
public class Main {
public static void main(String[] args) {
int[] arr = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
// Create an instance of Solution class
Solution sol = new Solution();
int maxSum = sol.maxSubArray(arr);
// Print the max subarray sum
System.out.println("The maximum subarray sum is: " + maxSum);
}
}