-
Problem Summary
-
Constraints
-
Intuition
-
Approach
-
Data Structures Used
-
Operations & Behavior Summary
-
Complexity
-
Multi-language Solutions
- C++
- Java
- JavaScript
- Python3
-
Step-by-step Detailed Explanation
-
Examples
-
How to use / Run locally
-
Notes & Optimizations
-
Author
Given an integer array arr, determine whether there exists a triplet (a, b, c) such that:
a^2 + b^2 = c^2All three values must come from the array and represent a valid Pythagorean triplet.
The task is to return true if such a triplet exists, otherwise return false.
1 ≤ arr.size() ≤ 10^51 ≤ arr[i] ≤ 1000
The equation of a Pythagorean triplet is:
a^2 + b^2 = c^2Since the maximum value of elements in the array is small (≤1000), I realized I could use a fast lookup structure to check whether a number exists in the array.
Instead of checking every triplet directly, I try every valid pair (a, b) that exists in the array. For each pair I compute:
c^2 = a^2 + b^2Then I check whether c exists in the array.
If it does, then a valid Pythagorean triplet exists.
-
First find the maximum value in the array.
-
Create a boolean presence array to mark which numbers exist.
-
Mark each number from the input array as present.
-
Iterate over all possible values of
a. -
For each
a, iterate over all possible values ofb. -
Compute
c² = a² + b². -
Compute
c = sqrt(c²). -
Check three conditions:
cmust be within the allowed rangec²must be a perfect squarecmust exist in the array
-
If all conditions are satisfied, return
true. -
If no valid triplet is found, return
false.
Boolean Array
Used to quickly check if a number exists in the input array. This allows constant time lookup.
| Operation | Purpose |
|---|---|
| Find maximum value | Determine frequency array size |
| Presence array | Store which values exist |
| Nested loops | Generate pairs (a, b) |
| Square computation | Check Pythagorean condition |
| Square root check | Validate perfect square |
Time Complexity
O(M^2)Where M is the maximum value in the array (≤1000).
Space Complexity
O(M)Used for the presence array.
class Solution {
public:
bool pythagoreanTriplet(vector<int>& arr) {
int maxVal = 0;
for(int num : arr)
maxVal = max(maxVal, num);
vector<bool> present(maxVal + 1, false);
for(int num : arr)
present[num] = true;
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);
if(c <= maxVal && c*c == cSquare && present[c])
return true;
}
}
return false;
}
};class Solution {
boolean pythagoreanTriplet(int[] arr) {
int maxVal = 0;
for(int num : arr)
maxVal = Math.max(maxVal, num);
boolean[] present = new boolean[maxVal + 1];
for(int num : arr)
present[num] = true;
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 = (int)Math.sqrt(cSquare);
if(c <= maxVal && c*c == cSquare && present[c])
return true;
}
}
return false;
}
}class Solution {
pythagoreanTriplet(arr) {
let maxVal = 0;
for (let num of arr)
maxVal = Math.max(maxVal, num);
let present = new Array(maxVal + 1).fill(false);
for (let num of arr)
present[num] = true;
for (let a = 1; a <= maxVal; a++) {
if (!present[a]) continue;
for (let b = a; b <= maxVal; b++) {
if (!present[b]) continue;
let cSquare = a*a + b*b;
let c = Math.floor(Math.sqrt(cSquare));
if (c <= maxVal && c*c === cSquare && present[c])
return true;
}
}
return false;
}
}class Solution:
def pythagoreanTriplet(self, arr):
maxVal = max(arr)
present = [False] * (maxVal + 1)
for num in arr:
present[num] = True
for a in range(1, maxVal + 1):
if not present[a]:
continue
for b in range(a, maxVal + 1):
if not present[b]:
continue
cSquare = a*a + b*b
c = int(cSquare ** 0.5)
if c <= maxVal and c*c == cSquare and present[c]:
return True
return False-
Determine the largest number in the array. This defines the range of possible values.
-
Create a boolean array called
presentwhere each index represents whether that value exists in the input array. -
Populate the
presentarray by marking each value from the input. -
Start iterating through possible values of
a. -
If
ais not present in the array, skip it. -
For every valid
a, iterate through possible values ofbstarting froma. -
Again skip if
bis not present. -
Compute
c² = a² + b². -
Compute
c = sqrt(c²). -
Verify that:
cdoes not exceed the maximum valuec²is a perfect squarecexists in the array
-
If all conditions hold, return
trueimmediately. -
If all combinations fail, return
false.
Example 1
Input
arr = [3, 2, 4, 6, 5]Output
trueExplanation
3^2 + 4^2 = 5^2Example 2
Input
arr = [3, 8, 5]Output
falseExample 3
Input
arr = [1,1,1]Output
false- Clone the repository
git clone <repo-url>- Navigate into the project
cd project-folder- Compile and run
C++
g++ solution.cpp
./a.outJava
javac Solution.java
java SolutionPython
python solution.py- The constraint
arr[i] ≤ 1000allows efficient use of a presence array. - Using a boolean lookup avoids repeated searching in the array.
- Perfect square validation ensures mathematical correctness.
- Early return improves performance when a valid triplet is found.