-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaily167.java
46 lines (38 loc) · 1.03 KB
/
daily167.java
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
// Solution 1
class Solution {
public boolean checkIfExist(int[] arr) {
/**
linear approach
keep hashmap of numbers and occurences
if a number's double or half exists, return true
*/
HashMap<Integer, Integer> exists = new HashMap<Integer, Integer>();
for (int n : arr) {
if (n % 2 == 0 && exists.containsKey(n / 2))
return true;
if (exists.containsKey(n * 2))
return true;
exists.put(n, 1);
}
return false;
}
}
// Solution 2
class Solution {
public boolean checkIfExist(int[] arr) {
for(int i=0;i<arr.length;i++){
float t=(float)arr[i]/2;
int x=search(arr,t);
if(x!=i && x!=-1)return true;
}
return false;
}
public int search(int []arr,float target){
for(int i=0;i<arr.length;i++){
if((float)arr[i]==target){
return i;
}
}
return -1;
}
}