File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.util.* ;
3+ class Solution {
4+ public int [] solution (int [] sequence , int k ) {
5+ List<int[]> list = new ArrayList<> ();
6+ int start = 0 , end = 0 , sum = sequence[0 ];
7+ while (start < sequence. length && end < sequence. length) {
8+ if (sum == k) {
9+ list. add(new int [] {start, end});
10+ }
11+ if (sum <= k) {
12+ end++ ;
13+ if (end < sequence. length)
14+ sum += sequence[end];
15+ } else {
16+ if (start < sequence. length)
17+ sum -= sequence[start];
18+ start++ ;
19+ }
20+ }
21+ Collections . sort(list, new Comparator<int[]> () {
22+ @Override
23+ public int compare (int [] o1 , int [] o2 ) {
24+ int len1 = o1[1 ] - o1[0 ];
25+ int len2 = o2[1 ] - o2[0 ];
26+ return len1 - len2;
27+ }
28+ });
29+ int [] answer = {list. get(0 )[0 ], list. get(0 )[1 ]};
30+ return answer;
31+ }
32+ }
33+ ```
You can’t perform that action at this time.
0 commit comments