给定一个数组对于不同元素,任务是从数组中找出两个元素对的总数,它们的和是一个完美的平方。
例子:
Input: arr[] = {2, 3, 6, 9, 10, 20}
Output: 2
Only possible pairs are (3, 6) and (6, 10)
Input: arr[] = {9, 2, 5, 1}
Output: 0
幼稚的方法:使用嵌套循环并检查每个可能的对,以确保它们的和是否为完美的平方。当阵列的长度很大时,此技术无效。
高效方法:
- 将数组的所有元素存储在名为nums的HashSet中,并将最多两个元素的和保存在名为max的变量中。
- 显然,数组中任何两个元素的总和不会超过max 。因此,发现所有的完全平方这是≤最大,并将其保存在一个ArrayList名为perfectSquares。
- 现在,对于数组中的每个元素说arr [i]以及对于perfectSquares中保存的每个完美正方形,请检查perfectSquares.get(i)– arr [i]是否以nums存在,即原始数组中是否有任何元素当与当前选择的元素一起添加时,可以从列表中给出任何理想的正方形。
- 如果满足上述条件,则增加计数变量。
- 最后打印计数值。
下面是上述方法的实现:
C++
// CPP implementation of the approach
#include
using namespace std;
// Function to return an ArrayList containing
// all the perfect squares upto n
vector getPerfectSquares(int n)
{
vector perfectSquares;
int current = 1, i = 1;
// while current perfect square is less than or equal to n
while (current <= n)
{
perfectSquares.push_back(current);
current = static_cast(pow(++i, 2));
}
return perfectSquares;
}
// Function to print the sum of maximum
// two elements from the array
int maxPairSum(vector &arr)
{
int n = arr.size();
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);
}
// Function to return the count of numbers that
// when added with n give a perfect square
int countPairsWith(int n, vector &perfectSquares, unordered_set &nums)
{
int count = 0;
for (int i = 0; i < perfectSquares.size(); i++)
{
int temp = perfectSquares[i] - n;
// temp > n is checked so that pairs
// (x, y) and (y, x) don't get counted twice
if (temp > n && find(nums.begin(), nums.end(), temp) != nums.end())
{
count++;
}
}
return count;
}
// Function to count the pairs whose sum is a perfect square
int countPairs(vector &arr)
{
int i, n = arr.size();
// Sum of the maximum two elements from the array
int max = maxPairSum(arr);
// List of perfect squares upto max
vector perfectSquares = getPerfectSquares(max);
// Contains all the array elements
unordered_set nums;
for (i = 0; i < n; i++)
{
nums.insert(arr[i]);
}
int count = 0;
for (i = 0; i < n; i++)
{
// Add count of the elements that when
// added with arr[i] give a perfect square
count += countPairsWith(arr[i], perfectSquares, nums);
}
return count;
}
// Driver code
int main()
{
vector arr = {2, 3, 6, 9, 10, 20};
cout << countPairs(arr) << endl;
return 0;
}
// This code is contributed by mits
Java
// Java implementation of the approach
import java.util.*;
public class GFG {
// Function to return an ArrayList containing
// all the perfect squares upto n
public static ArrayList getPerfectSquares(int n)
{
ArrayList perfectSquares = new ArrayList<>();
int current = 1, i = 1;
// while current perfect square is less than or equal to n
while (current <= n) {
perfectSquares.add(current);
current = (int)Math.pow(++i, 2);
}
return perfectSquares;
}
// Function to print the sum of maximum
// two elements from the array
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);
}
// Function to return the count of numbers that
// when added with n give a perfect square
public static int countPairsWith(
int n, ArrayList perfectSquares,
HashSet nums)
{
int count = 0;
for (int i = 0; i < perfectSquares.size(); i++) {
int temp = perfectSquares.get(i) - n;
// temp > n is checked so that pairs
// (x, y) and (y, x) don't get counted twice
if (temp > n && nums.contains(temp))
count++;
}
return count;
}
// Function to count the pairs whose sum is a perfect square
public static int countPairs(int arr[])
{
int i, n = arr.length;
// Sum of the maximum two elements from the array
int max = maxPairSum(arr);
// List of perfect squares upto max
ArrayList perfectSquares =
getPerfectSquares(max);
// Contains all the array elements
HashSet nums = new HashSet<>();
for (i = 0; i < n; i++)
nums.add(arr[i]);
int count = 0;
for (i = 0; i < n; i++) {
// Add count of the elements that when
// added with arr[i] give a perfect square
count += countPairsWith(arr[i], perfectSquares, nums);
}
return count;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2, 3, 6, 9, 10, 20 };
System.out.println(countPairs(arr));
}
}
Python3
# Python3 implementation of the approach
# Function to return an ArrayList containing
# all the perfect squares upto n
def getPerfectSquares(n):
perfectSquares = [];
current = 1;
i = 1;
# while current perfect square is
# less than or equal to n
while (current <= n):
perfectSquares.append(current);
i += 1;
current = int(pow(i, 2));
return perfectSquares;
# Function to print the sum of maximum
# two elements from the array
def maxPairSum(arr):
n = len(arr);
max = 0;
secondMax = 0;
if (arr[0] > arr[1]):
max = arr[0];
secondMax = arr[1];
else:
max = arr[1];
secondMax = arr[0];
for i in range(2, n):
if (arr[i] > max):
secondMax = max;
max = arr[i];
elif (arr[i] > secondMax):
secondMax = arr[i];
return (max + secondMax);
# Function to return the count of numbers that
# when added with n give a perfect square
def countPairsWith(n, perfectSquares, nums):
count = 0;
for i in range(len(perfectSquares)):
temp = perfectSquares[i] - n;
# temp > n is checked so that pairs
# (x, y) and (y, x) don't get counted twice
if (temp > n and (temp in nums)):
count += 1;
return count;
# Function to count the pairs whose
# sum is a perfect square
def countPairs(arr):
n = len(arr);
# Sum of the maximum two elements
# from the array
max = maxPairSum(arr);
# List of perfect squares upto max
perfectSquares = getPerfectSquares(max);
# Contains all the array elements
nums = [];
for i in range(n):
nums.append(arr[i]);
count = 0;
for i in range(n):
# Add count of the elements that when
# added with arr[i] give a perfect square
count += countPairsWith(arr[i],
perfectSquares, nums);
return count;
# Driver code
arr = [ 2, 3, 6, 9, 10, 20 ];
print(countPairs(arr));
# This code is contributed by mits
C#
// C# implementation of the approach
using System;
using System.Collections;
using System.Collections.Generic;
public class GFG {
// Function to return an ArrayList containing
// all the perfect squares upto n
public static ArrayList getPerfectSquares(int n)
{
ArrayList perfectSquares = new ArrayList();
int current = 1, i = 1;
// while current perfect square is less than or equal to n
while (current <= n) {
perfectSquares.Add(current);
current = (int)Math.Pow(++i, 2);
}
return perfectSquares;
}
// Function to print the sum of maximum
// two elements from the array
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);
}
// Function to return the count of numbers that
// when added with n give a perfect square
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;
// temp > n is checked so that pairs
// (x, y) and (y, x) don't get counted twice
if (temp > n && nums.Contains(temp))
count++;
}
return count;
}
// Function to count the pairs whose sum is a perfect square
public static int countPairs(int[] arr)
{
int i, n = arr.Length;
// Sum of the maximum two elements from the array
int max = maxPairSum(arr);
// List of perfect squares upto max
ArrayList perfectSquares = getPerfectSquares(max);
// Contains all the array elements
ArrayList nums = new ArrayList();
for (i = 0; i < n; i++)
nums.Add(arr[i]);
int count = 0;
for (i = 0; i < n; i++) {
// Add count of the elements that when
// added with arr[i] give a perfect square
count += countPairsWith(arr[i], perfectSquares, nums);
}
return count;
}
// Driver code
public static void Main()
{
int[] arr = { 2, 3, 6, 9, 10, 20 };
Console.WriteLine(countPairs(arr));
}
}
// This code is contributed by mits
PHP
$arr[1])
{
$max = $arr[0];
$secondMax = $arr[1];
}
else
{
$max = $arr[1];
$secondMax = $arr[0];
}
for ($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);
}
// Function to return the count of numbers that
// when added with n give a perfect square
function countPairsWith($n, $perfectSquares, $nums)
{
$count = 0;
for ($i = 0; $i < count($perfectSquares); $i++)
{
$temp = $perfectSquares[$i] - $n;
// temp > n is checked so that pairs
// (x, y) and (y, x) don't get counted twice
if ($temp > $n && in_array($temp, $nums))
$count++;
}
return $count;
}
// Function to count the pairs whose
// sum is a perfect square
function countPairs($arr)
{
$n = count($arr);
// Sum of the maximum two elements
// from the array
$max = maxPairSum($arr);
// List of perfect squares upto max
$perfectSquares = getPerfectSquares($max);
// Contains all the array elements
$nums = array();
for ($i = 0; $i < $n; $i++)
array_push($nums, $arr[$i]);
$count = 0;
for ($i = 0; $i < $n; $i++)
{
// Add count of the elements that when
// added with arr[i] give a perfect square
$count += countPairsWith($arr[$i],
$perfectSquares, $nums);
}
return $count;
}
// Driver code
$arr = array( 2, 3, 6, 9, 10, 20 );
echo countPairs($arr);
// This code is contributed by mits
?>
输出:
2
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。