such that . Given an unsorted array of unique integers in the text file, how many Pythagorean triplets are there?
Pythagorean triplets are triplets that satisfy the condition thatAs an explicit example, in the array , there are Pythagorean Triplets, namely and .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The python set is essentially a hashmap. Because of the nature of this data struture, checking if an object is within it takes a snappy ( O ( 1 ) ) time. I then just go through every pair a , b and check if a 2 + b 2 is in
L
. 0 is initially removed from the set. If it hadn't been we would admit solutions such as a , 0 , a . Thanks to itertools and the integer nature of booleans in python, the actual solution can be reduced to a concise one liner that runs in O ( n 2 ) .