-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMaddy_sol
More file actions
35 lines (27 loc) · 891 Bytes
/
Copy pathMaddy_sol
File metadata and controls
35 lines (27 loc) · 891 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
35
class HTF {
static boolean twoSum(int[] arr, int target){
int n = arr.length;
for (int i = 0; i < n; i++) {
// For each element arr[i], check every
// other element arr[j] that comes after it
for (int j = i + 1; j < n; j++) {
// Check if the sum of the current pair
// equals the target
if (arr[i] + arr[j] == target) {
return true;
}
}
}
// If no pair is found after checking
// all possibilities
return false;
}
public static void main(String[] args){
int[] arr = { 0, -1, 2, -3, 1 };
int target = -2;
if (twoSum(arr, target))
System.out.println("true");
else
System.out.println("false");
}
}