-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
44 lines (32 loc) · 1006 Bytes
/
Copy pathsolution.cpp
File metadata and controls
44 lines (32 loc) · 1006 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
36
37
38
39
40
41
42
43
44
class Solution
{
public:
bool pythagoreanTriplet(vector<int> &arr)
{
int maxVal = 0;
// Find the maximum value in the array
for (int num : arr)
maxVal = max(maxVal, num);
// Frequency array to check presence of numbers
vector<bool> present(maxVal + 1, false);
for (int num : arr)
present[num] = true;
// Try all possible pairs (a, b)
for (int a = 1; a <= maxVal; a++)
{
if (!present[a])
continue;
for (int b = a; b <= maxVal; b++)
{
if (!present[b])
continue;
int cSquare = a * a + b * b;
int c = sqrt(cSquare);
// Check if c exists and is a perfect square
if (c <= maxVal && c * c == cSquare && present[c])
return true;
}
}
return false;
}
};