-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxSubArrayWithSum.java
More file actions
41 lines (39 loc) · 1.28 KB
/
MaxSubArrayWithSum.java
File metadata and controls
41 lines (39 loc) · 1.28 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
// Given an unsorted array A of size N that contains only non-negative integers, find a continuous sub-array which adds to a given number S.
// In case of multiple subarrays, return the subarray which comes first on moving from left to right.
class maxsubarraywithsum{
public int[] maxarraySum(int[] arr,int s,int n){
int[] result = new int[2];
int sum = 0;
int start = 0;
int end = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
end = i;
if (sum == s) {
result[0] = start;
result[1] = end;
return result;
}
if (sum > s) {
while (sum > s) {
sum -= arr[start];
start++;
}
if (sum == s) {
result[0] = start;
result[1] = end;
return result;
}
}
}
result[0] = -1;
result[1] = -1;
return result;
}
public static void main(String[] args) {
maxsubarraywithsum m = new maxsubarraywithsum();
int[] arr = {1,2,3,7,5};
int[] result = m.maxarraySum(arr, 12, 5);
System.out.println(result[0] + " " + result[1]);
}
}