-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxSumSubarray.java
More file actions
112 lines (94 loc) · 3.79 KB
/
Copy pathMaxSumSubarray.java
File metadata and controls
112 lines (94 loc) · 3.79 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.util.Arrays;
import java.util.Scanner;
class SubarrayResult {
int sum;
int start;
int end;
SubarrayResult(int sum, int start, int end) {
this.sum = sum;
this.start = start;
this.end = end;
}
}
class MaxSumSubarray {
private static SubarrayResult maxCrossingSum(int[] resources, int left, int mid, int right, int constraint) {
SubarrayResult bestResult = new SubarrayResult(0, -1, -1);
int currentSum = 0;
for (int i = mid; i >= left; i--) {
currentSum += resources[i];
if (currentSum > constraint) {
break;
}
if (currentSum > bestResult.sum) {
bestResult = new SubarrayResult(currentSum, i, mid);
}
int rightSum = currentSum;
for (int j = mid + 1; j <= right; j++) {
rightSum += resources[j];
if (rightSum > constraint) {
break;
}
if (rightSum > bestResult.sum) {
bestResult = new SubarrayResult(rightSum, i, j);
}
}
}
return bestResult;
}
private static SubarrayResult maxSubarraySum(int[] resources, int left, int right, int constraint) {
if (left > right) {
return new SubarrayResult(0, -1, -1);
}
if (left == right) {
if (resources[left] <= constraint) {
return new SubarrayResult(resources[left], left, left);
} else {
return new SubarrayResult(0, -1, -1);
}
}
int mid = left + (right - left) / 2;
SubarrayResult leftResult = maxSubarraySum(resources, left, mid, constraint);
SubarrayResult rightResult = maxSubarraySum(resources, mid + 1, right, constraint);
SubarrayResult crossResult = maxCrossingSum(resources, left, mid, right, constraint);
if (leftResult.sum >= rightResult.sum && leftResult.sum >= crossResult.sum) {
return leftResult;
} else if (rightResult.sum >= leftResult.sum && rightResult.sum >= crossResult.sum) {
return rightResult;
} else {
return crossResult;
}
}
public static SubarrayResult findMaxSubarray(int[] resources, int constraint) {
if (resources == null || resources.length == 0 || constraint <= 0) {
return new SubarrayResult(0, -1, -1);
}
return maxSubarraySum(resources, 0, resources.length - 1, constraint);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of tasks (resources): ");
int n = scanner.nextInt();
if (n <= 0) {
System.out.println("Number of tasks must be positive.");
scanner.close();
return;
}
int[] resources = new int[n];
System.out.println("Enter the resource values for each task:");
for (int i = 0; i < n; i++) {
resources[i] = scanner.nextInt();
}
System.out.print("Enter the resource constraint: ");
int constraint = scanner.nextInt();
SubarrayResult result = findMaxSubarray(resources, constraint);
if (result.sum == 0 && result.start == -1) {
System.out.println("No feasible subarray found within the constraint.");
} else {
System.out.println("Maximum resource utilization: " + result.sum);
int[] subarrayElements = Arrays.copyOfRange(resources, result.start, result.end + 1);
System.out.println("Subarray elements: " + Arrays.toString(subarrayElements));
System.out.println("Indices: from " + result.start + " to " + result.end);
}
scanner.close();
}
}