Java Program to Check if There are Any Pythagorean Triplets in the Array – Sanfoundry

This is the Java Program to Check if There are Any Pythagorean Triplets in the Array.

Problem Description

Given an array of integers, check for any triplet of elements say a, b, and c where all three elements form a Pythagorean triplet, that is, c2 = a2 + b2
Example:

Array = [1, 3, 4, 5]

Output: 3, 4 and 5

Problem Solution

Use three nested loops, and inside the innermost loop, check for the array elements satisfying the Pythagorean triplet equivalence, or not.

Program/Source Code

Here is the source code of the Java Program to Check if There are Any Pythagorean Triplets in the Array. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. //Java Program to Check if There are Any Pythagorean Triplets in the Array
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class PythagoreanTriplets {
  8.     // Function to find and display Pythagorean triplets
  9.     static void printPythagoreanTriplets(int[] array){
  10.         int i,j,k;
  11.         int x,y,z;
  12.         int count=0;
  13.         for(i=0;i<array.length;i++){
  14.             x=array[i];
  15.             for(j=0;j<array.length;j++){
  16.                 y=array[j];
  17.                 for(k=0;k<array.length;k++){
  18.                     z=array[k];
  19.                     if((z*z)==(x*x + y*y)){
  20.                         count++;
  21.                         System.out.println(x+", "+y+", "+z);
  22.                         return;
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.         System.out.println("No pythagorean triplets exist in the array");
  28.     }
  29.     // Function to read user input
  30.     public static void main(String[] args) {
  31.         int size;
  32.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  33.         System.out.println("Enter the array size");
  34.         try {
  35.             size = Integer.parseInt(br.readLine());
  36.         } catch (Exception e) {
  37.             System.out.println("Invalid Size");
  38.             return;
  39.         }
  40.         int[] array = new int[size];
  41.         int i;
  42.         System.out.println("Enter array elements");
  43.         for (i = 0; i < array.length; i++) {
  44.             try {
  45.                 array[i] = Integer.parseInt(br.readLine());
  46.             } catch (Exception e) {
  47.                 System.out.println("An error occurred");
  48.             }
  49.         }
  50.         printPythagoreanTriplets(array);
  51.     }
  52. }

Program Explanation

1. In function printPythagoreanTriplets(), triple nested loops are used to check every combination of array elements.
2. If any combination of elements satisfies the Pythagorean triplet criteria if((z*z)==(x*x + y*y)), then that triplet is printed and we return from the function.
3. If Pythagorean triplet existed, then a suitable message is displayed.

Runtime Test Cases

 
Case 1 (Positive Test Case):
 
Enter the array size
4
Enter array elements
1
3
4
5
3, 4, and 5
 
Case 2 (Negative Test Case):
 
Enter the array size
4
Enter array elements
1
2
3
4
No pythagorean triplets exist in the array

Sanfoundry Global Education & Learning Series – Java Programs.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Leave a Comment

Your email address will not be published. Required fields are marked *