-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSumForTwoNum.java
More file actions
34 lines (29 loc) · 969 Bytes
/
SumForTwoNum.java
File metadata and controls
34 lines (29 loc) · 969 Bytes
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
package stonehee.baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
public class SumForTwoNum {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in));
int arrLen = Integer.parseInt(br.readLine());
int[] arr = Arrays.stream(br.readLine().split(" "))
.mapToInt(Integer::parseInt)
.toArray();
int x = Integer.parseInt(br.readLine());
System.out.println(findPair(arr, x));
}
private static int findPair(int[] arr, int x) {
Arrays.parallelSort(arr);
int cnt = 0;
int start = 0;
int end = arr.length - 1;
int sum = 0;
while(start < end) {
sum = arr[start] + arr[end];
if(sum == x) cnt++;
if(sum <= x) start++;
else end--;
}
return cnt;
}
}