githubEdit

08. Pythagorean Triplet

✅ GFG solution to the Pythagorean Triplet problem: determine whether three numbers in an array satisfy a² + b² = c² using hashing and mathematical observation. 🚀

The problem can be found at the following link: 🔗 Question Linkarrow-up-right

🧩 Problem Description

Given an array arr[], return true if there exists a triplet (a, b, c) from the array (where a, b, and c are at different indexes) that satisfies a² + b² = c², otherwise return false.

📘 Examples

Example 1

Input: arr[] = [3, 2, 4, 6, 5]
Output: true
Explanation: a=3, b=4, and c=5 forms a Pythagorean triplet.

Example 2

Input: arr[] = [3, 8, 5]
Output: false
Explanation: No such triplet possible.

Example 3

Input: arr[] = [1, 1, 1]
Output: false
Explanation: 1² + 1² = 2 ≠ 1², so no valid triplet exists.

🔒 Constraints

  • $1 \le \text{arr.size()} \le 10^5$

  • $1 \le \text{arr}[i] \le 10^3$

✅ My Approach

The optimal approach uses a Hash Set of Squares for O(1) existence lookup combined with a double-loop over all pairs:

Hash Set of Squares

  1. Precompute Squares:

    • Insert the square of every element (x * x) into an unordered_set.

    • This enables O(1) average-case lookup for any target value.

  2. Enumerate All Pairs:

    • Iterate over every pair (i, j) with i < j.

    • For each pair, compute the sum of squares: arr[i]² + arr[j]².

  3. Check Existence:

    • If the computed sum exists in the set, then there is some element c in the array such that c² = arr[i]² + arr[j]², which means a valid Pythagorean triplet exists — return true.

  4. Return Result:

    • If no such pair is found after checking all combinations, return false.

Key Insight: By storing squares (not values), we directly check the Pythagorean condition a² + b² = c² without needing to compute square roots, avoiding any floating-point precision issues.

📝 Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n²), as we iterate over all pairs (i, j) with i < j, which results in approximately n*(n-1)/2 iterations, and each lookup in the hash set takes O(1) average time.

  • Expected Auxiliary Space Complexity: O(n), as we store the square of each element in an unordered set of size at most n.

🧑‍💻 Code (C++)

chevron-right⚡ View Alternative Approaches with Code and Analysishashtag

📊 2️⃣ Frequency Array & Math (O(k²))

💡 Algorithm Steps:

  1. Build a freq[1001] array to store the count of each value present in arr.

  2. Collect all unique values (where freq[x] > 0) into a sorted list unique_nums.

  3. For every pair (a, b) from unique_nums (with a ≤ b):

    • Compute c2 = a² + b² and c = round(√c2).

    • If c ≤ 1000, c² == c2, and freq[c] > 0:

      • Validate element distinctness using frequency counts (e.g., if a == b, need freq[a] ≥ 2; if all three are equal, need freq[a] ≥ 3).

      • Return true if valid.

  4. Return false if no valid triplet is found.

📝 Complexity Analysis:

  • Time: O(k²), where k ≤ 1000 unique values, giving at most ~10⁶ operations in the worst case.

  • Auxiliary Space: O(1), as only a fixed-size array of 1001 elements is used regardless of input size.

Why This Approach?

  • Exploits the small value constraint (arr[i] ≤ 1000) for bounded iteration.

  • Uses O(1) extra space — no dynamic data structures needed.

  • Handles duplicate element edge cases precisely through frequency validation.

🆚 Comparison of Approaches

Approach

⏱️ Time

🗂️ Space

Pros

⚠️ Cons

🔢 Hash Set of Squares

🟡 O(n²)

🟡 O(n)

Simple logic, clean code, O(1) lookups

Extra hash memory, n can be large

⚙️ Frequency Array & Math

🟢 O(k²)

🟢 O(1)

Constant extra space, leverages constraints

Only works when max(arr[i]) is small

Best Choice by Scenario

Scenario

Recommended Approach

🏆 General input, straightforward implementation

🥇 Hash Set of Squares

🔧 Optimized for small value ranges with O(1) space

🥈 Frequency Array & Math

🧑‍💻 Code (Java)

🐍 Code (Python)

🧠 Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: 📬 Any Questions?arrow-up-right. Let's make this learning journey more collaborative!

If you find this helpful, please give this repository a star!


📍Visitor Count

Visitor counter

Last updated