using
System;
using
System.Collections;
using
System.Collections.Generic;
public
class
GFG {
public
static
ArrayList getPerfectSquares(
int
n)
{
ArrayList perfectSquares =
new
ArrayList();
int
current = 1, i = 1;
while
(current <= n) {
perfectSquares.Add(current);
current = (
int
)Math.Pow(++i, 2);
}
return
perfectSquares;
}
public
static
int
maxPairSum(
int
[] arr)
{
int
n = arr.Length;
int
max, secondMax;
if
(arr[0] > arr[1]) {
max = arr[0];
secondMax = arr[1];
}
else
{
max = arr[1];
secondMax = arr[0];
}
for
(
int
i = 2; i < n; i++) {
if
(arr[i] > max) {
secondMax = max;
max = arr[i];
}
else
if
(arr[i] > secondMax) {
secondMax = arr[i];
}
}
return
(max + secondMax);
}
public
static
int
countPairsWith(
int
n, ArrayList perfectSquares, ArrayList nums)
{
int
count = 0;
for
(
int
i = 0; i < perfectSquares.Count; i++) {
int
temp = (
int
)perfectSquares[i] - n;
if
(temp > n && nums.Contains(temp))
count++;
}
return
count;
}
public
static
int
countPairs(
int
[] arr)
{
int
i, n = arr.Length;
int
max = maxPairSum(arr);
ArrayList perfectSquares = getPerfectSquares(max);
ArrayList nums =
new
ArrayList();
for
(i = 0; i < n; i++)
nums.Add(arr[i]);
int
count = 0;
for
(i = 0; i < n; i++) {
count += countPairsWith(arr[i], perfectSquares, nums);
}
return
count;
}
public
static
void
Main()
{
int
[] arr = { 2, 3, 6, 9, 10, 20 };
Console.WriteLine(countPairs(arr));
}
}