forked from anishrauniyar/EasySolutionMUM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquarePair.java
More file actions
54 lines (45 loc) · 1.41 KB
/
SquarePair.java
File metadata and controls
54 lines (45 loc) · 1.41 KB
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
import java.util.Scanner;
/*
* example: {9, 0, 2, -5, 7} have total 2 square pairs.
* since the square pairs are <2, 7> and <7, 9>.
* Note that <-5, 9> and <0, 9> are not square pairs, even though they sum to perfect squares,
* because both members of a square pair have to be greater than 0.
* Also <7,2> and <9,7> are not square pairs because the first number has to be less than
* the second number.
*/
public class SquarePair {
public static void main(String[] argr) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array: ");
int size = sc.nextInt();
int[] value = new int[size];
System.out.println("Enter the values: ");
for (int i = 0; i < size; i++) {
value[i] = sc.nextInt();
}
int squarePairCount = SquarePair.countSquarePair(value);
System.out.println("Total Square Pair is: " + squarePairCount);
sc.close();
}
private static int countSquarePair(int[] value) {
// TODO Auto-generated method stub
int flag = 0;
int a = 0, b = 0;
double sum = 0;
for (int i = 0; i < value.length; i++) {
for (int j = 0; j < value.length; j++) {
a = value[i];
b = value[j];
if (a < b && a > 0 && b > 0) {
sum = a + b;
if (Math.sqrt(sum) % 1 == 0) {
System.out.println("Pair: <" + a + ", " + b + ">");
flag++;
}
}
sum = 0;
}
}
return flag;
}
}