PuzzlersWorld.com

  • Hacker Puzzle
  • Interview Puzzles
  • Number Puzzles
  • Maths Puzzles

Find a pythagorean triplet from an array

(3 votes, average: 1.33 out of 5)

March 28, 2013 by puzzler 1 Comment

Problem Statement:
Given an array a of n integers find all possible Pythagorean triplets from the array.

What is Pythagorean triplet?

A Pythagorean triple consists of three positive integers a, b, and c, such that a2 + b2 = c2. this formula is derived from Pythagoras theorem: “For every right angle triangle with side lengths a, b, and c=> a2 + b2 = c2“.

Pythagorean Triplet

Pythagorean Triplet

Solution:


Conceptually it is similar to Find Non duplicate pairs that sum to S and 3 SUM problem

First sort the array, then square each number, now for every element a[i] in the array find if a pair exist with sum as a[i], this can be done in O(n) and we have to do it for every i, so the complexity will be O(n^2).

  • Share on Whatsapp
  • Share on Facebook
  • Share on Twitter
Facebook Comments
Next Puzzle
Sort the array in O(N)

Checkout more Interview Questions Tags: Algorithm, Array, Difficult, Interview, Solved Puzzles

Comments

  1. harish says

    September 22, 2013 at 6:37 am

    i want triplet not formule……….

    Reply

Leave a Comment Cancel reply

Submit your Puzzle

You may also like

  • Find nth element from end of linked list
  • DS with insert, delete and getRandomElement in O(1)
  • Cake Cutting solution: Facebook hacker cup 2013 Round 2
  • Basic Concepts C/C++
  • Spinning Disc Direction Puzzle
  • Reverse a link list without using another
  • Sort the array in O(N)
  • Card Game solution: Facebook hacker cup 2013 Round1
  • Dead Pixels solution: Facebook hacker cup 2013 Round 1
  • RoboElection solution: Facebook hacker cup 2013 Round 2

Categories

  • Aive hi Puzzles
  • Akbar and Birbal
  • Alphabetical Puzzles
  • Bollywood Puzzles
  • Google Code jam
  • Hindi Riddles
  • Interview Puzzles
  • Interview Questions
  • Logical Puzzles
  • Malayalam Puzzles
  • Maths Puzzles
  • Miscellaneous
  • Number Puzzles
  • Picture Puzzles
  • Riddles
  • Tamil Puzzles
  • Technical

Social

  • View puzzlersworld’s profile on Twitter
privacy policy

Copyright © 2025 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in

  • Hacker Puzzle
  • Logo Puzzles
  • Optical Illusions
  • WhatsApp Puzzles
  • Picture Puzzles
  • Riddles
    ▼
    • Hindi Riddles
  • Bollywood Puzzles
  • Alphabetical Puzzles
  • Aive hi Puzzles
  • Interview Puzzles
  • Logical Puzzles
  • Interview Questions
    ▲
    • Data Structures
    • Binary Tree
    • Algorithms
    • Recursion Questions
    • Amazon Interview Questions
    • Snapdeal Interview Questions
    • Google Code jam
  • Technical
  • Akbar and Birbal
  • Number Puzzles
  • Maths Puzzles
  • Miscellaneous