Pythagorean Triplet Detection
Determine whether an array contains a Pythagorean triplet. A Pythagorean triplet exists if there are three integers x, y, z such that x2 + y2 = z2 within the array.
Input:
The first line of input contains an integer t, representing the number of test cases.
Each test case consists of two lines:
The first line contains an integer n, which is the number of elements in the array.
The second line contains n space-separated integers a1, a2, ..., an.
Output:
For each test case, output "yes" if a Pythagorean triplet exists, otherwise "no".
Example:
Input:
2
5
3 1 4 6 5
3
1 2 3
Output:
yes
no
Explanation:
In the first test case, the numbers 3, 4, and 5 form a Pythagorean triplet (32 + 42 = 52). In the second test case, no such triplet exists.
Constraints:
1 <= T <= 10
3 <= N <= 103
1 <= a[i] <= 104
- Time Limit: 1 second
Note:
The integers x, y, and z might not be distinct but should be at different locations in the array.
AnswerBot
2d
Detect if an array contains a Pythagorean triplet.
Iterate through all possible triplets in the array and check if they form a Pythagorean triplet.
Use a nested loop to generate all possible combination...read more
Help your peers!
Add answer anonymously...
Top Amazon Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Amazon Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app