forked from nagajyothi/InterviewBit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythagoreanTriplet.java
65 lines (61 loc) · 2.05 KB
/
PythagoreanTriplet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* Given an array of integers, write a function that returns true if there is a triplet (a, b, c) that satisfies a2 + b2 = c2.
*
Example:
Input: arr[] = {3, 1, 4, 6, 5}
Output: True
There is a Pythagorean triplet (3, 4, 5).
Input: arr[] = {10, 4, 6, 12, 5}
Output: False
There is no Pythagorean triplet.
*/
import java.util.*;
public class PythagoreanTriplet{
public static boolean bruteForce(int[] A){
//ArrayList<Integer> result = new ArrayList<Integer>();
for(int i = 0; i < A.length; i++){
for(int j = i+1; j < A.length; j++){
for(int k = j + 1; k < A.length; k++){
if(Math.pow(A[i], 2) + Math.pow(A[j], 2) == Math.pow(A[k], 2) ||
Math.pow(A[j], 2) + Math.pow(A[k], 2) == Math.pow(A[i], 2) ||
Math.pow(A[i], 2) + Math.pow(A[k], 2) == Math.pow(A[j], 2)){
System.out.print("[" + A[i] + " ," + A[j] + " ," + A[k] +"]");
return true;
}
}
}
}
return false;
}
public static boolean sortSolution(int[] A){
for(int i = 0; i < A.length; i++){
A[i] = A[i] * A[i];
}
Arrays.sort(A);
for(int i = A.length-1; i >= 2; i--){
int l = 0;
int r = i-1;
while(l < r){
if(A[l] + A[r] == A[i]){
System.out.print("[" + (int)Math.sqrt(A[l]) + " ," + (int)Math.sqrt(A[r]) + " ," + (int)Math.sqrt(A[i]) +"]");
return true;
}
if(A[l] + A[r] < A[i]){
l++;
}
else{
r--;
}
}
}
return false;
}
public static void main(String[] args){
int[] A = {3, 1, 4, 6, 5};
System.out.print("There is ");
if(!sortSolution(A)){
System.out.print("no ");
}
System.out.println(" Pythogorean triplet ");;
}
}