-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1346_CheckNOrDoubleExit.cpp
87 lines (74 loc) · 2.08 KB
/
1346_CheckNOrDoubleExit.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <vector>
#include <algorithm>
#include <iostream>
void printVector(std::vector<int> numVec);
/*
Given an array arr of integers, check if there exist two indices i and j such that :
i != j
0 <= i, j < arr.length
arr[i] == 2 * arr[j]
Example 1:
Input: arr = [10,2,5,3]
Output: true
Explanation: For i = 0 and j = 2, arr[i] == 10 == 2 * 5 == 2 * arr[j]
Example 2:
Input: arr = [3,1,7,11]
Output: false
Explanation: There is no i and j that satisfy the conditions.
*/
class Solution
{
public:
bool checkIfExist(std::vector<int> &arr)
{
std::unordered_map<int, int> seen;
for (int i = 0; i < arr.size(); i++)
{
if ((arr[i] % 2) == 0)
{
// EVEN
// std::cout << "EVEN.\n";
// std::cout << (arr[i]>>1) << "\n";
// std::cout << (arr[i]<<1) << "\n";
if (seen.contains(arr[i] >> 1) ||
seen.contains(arr[i] << 1))
{
return true;
}
}
else
{
// ODD
// std::cout << "ODD.\n";
// std::cout << (arr[i]<<1) << "\n";
if (seen.contains(arr[i] << 1))
{
return true;
}
}
seen[arr[i]] = i;
}
return false;
}
};
int main()
{
Solution S1;
std::vector<int> vec1 = {10, 2, 5, 3};
std::vector<int> vec2 = {3, 1, 7, 11};
std::vector<int> vec3 = {3, 1, 7, 11, 22};
std::cout
<< "vec1: ";
printVector(vec1);
std::cout
<< "vec2: ";
printVector(vec2);
std::cout
<< "vec3: ";
printVector(vec3);
std::cout
<< "Exists in vec1?: " << (S1.checkIfExist(vec1) ? "true" : "false") << "\n"
<< "Exists in vec2?: " << (S1.checkIfExist(vec2) ? "true" : "false") << "\n"
<< "Exists in vec3?: " << (S1.checkIfExist(vec3) ? "true" : "false") << "\n"
<< "\n";
}