-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntervalSum.java
More file actions
67 lines (50 loc) · 1.69 KB
/
Copy pathIntervalSum.java
File metadata and controls
67 lines (50 loc) · 1.69 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
import java.util.Scanner;
import java.util.ArrayList;
public class IntervalSum {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Integer> arr = scanArray(in);
int sum = in.nextInt();
int[] hdtl = new int[2];
hdtl = findInterval(arr, sum);
System.out.println(hdtl[0] + " " + hdtl[1]);
}
public static int[] findInterval(ArrayList<Integer> arr, int sum) {
int[] res = new int[2];
if(arr.size() != 0) {
int partial_sum = arr.get(0);
int i = 0;
int j = 0;
while(j<arr.size()-1) {
if(partial_sum == sum) {
res[0] = i;
res[1] = j;
return res;
} else if(partial_sum < sum) {
partial_sum += arr.get(++j);
} else {
partial_sum -= arr.get(i++);
}
}
res[0] = -1;
res[1] = -1;
return res;
}
res[0] = -1;
res[1] = -1;
return res;
}
public static ArrayList<Integer> scanArray(Scanner in) {
// scan line of text
String line = in.nextLine();
// convert line of text into array of strings (tokens)
String[] tokens = line.split(" ");
// convert array of strings into array of integers
ArrayList<Integer> arr = new ArrayList<Integer>();
for (String token : tokens) {
if (!token.isEmpty()) // some tokens may be empty (e.g. with trailing spaces)
arr.add(Integer.parseInt(token));
}
return arr;
}
}