-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPosSum.java
More file actions
48 lines (41 loc) · 1.22 KB
/
PosSum.java
File metadata and controls
48 lines (41 loc) · 1.22 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
package accentureQ;
import java.util.Arrays;
import java.util.Scanner;
public class PosSum {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter length");
int n = sc.nextInt();
System.out.println("Enter array");
// int []arr = new int [] {1, 2, 3,7, 5};
int []arr = new int [n];
for (int i = 0; i<arr.length; i++){
arr[i] = sc.nextInt();
}
System.out.println("Enter target");
// int target = 12;
int target = sc.nextInt();
System.out.println(Arrays.toString( arr(arr, target)));
}
static int[] arr(int[]arr, int target){
int sum =0;
int []res = new int[2];
int start = 0;
int i =0;
while ( i< arr.length){
if(sum == target){
res[0] = start + 1;
res[1] = i ;
break;
}else if(sum<target){
sum += arr[i];
i++;
} else if (sum != target) {
start++;
i = start;
sum = 0;
}
}
return res;
}
}