-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
45 lines (35 loc) · 1.14 KB
/
Copy pathsolution.java
File metadata and controls
45 lines (35 loc) · 1.14 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
class Solution {
public boolean isProduct(int[] arr, long target) {
// HashSet to store visited numbers
HashSet<Long> seen = new HashSet<>();
// Traverse array
for (long num : arr) {
// Special case for zero
if (num == 0) {
// If target is zero and any previous number exists
// then product can become zero
if (target == 0 && !seen.isEmpty()) {
return true;
}
// Store zero and move ahead
seen.add(num);
continue;
}
// Skip if target is not divisible
if (target % num != 0) {
seen.add(num);
continue;
}
// Required pair value
long needed = target / num;
// Check whether partner already exists
if (seen.contains(needed)) {
return true;
}
// Store current value
seen.add(num);
}
// No pair found
return false;
}
};