Count of pairs in an array whose sum is a perfect square
Last Updated :
28 Mar, 2023
Given an array arr of distinct elements, the task is to find the total number of two element pairs from the array whose sum is a perfect square.
Examples:
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
Naive Approach: Use nested loops and check every possible pair for whether their sum is a perfect square or not. This technique is not effective when the length of the array is very large.
Efficient Approach:
- Store all the elements of the array in a HashSet named nums and save the sum of the maximum two elements in a variable named max.
- It is clear that the sum of any two elements from the array will not exceed max. So, find all the perfect squares which are ? max and save it in an ArrayList named perfectSquares.
- Now for every element in the array say arr[i] and for every perfect square saved in perfectSquares, check whether perfectSquares.get(i) - arr[i] exists in nums or not i.e. if there is any element in the original array that when added with the currently chosen element gives any perfect square from the list.
- If the above condition is satisfied, increment the count variable.
- Print the value of count in the end.
Below is the implementation of the above approach:
C++
// CPP implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return an ArrayList containing
// all the perfect squares upto n
vector<int> getPerfectSquares(int n)
{
vector<int> 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<int>(pow(++i, 2));
}
return perfectSquares;
}
// Function to print the sum of maximum
// two elements from the array
int maxPairSum(vector<int> &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<int> &perfectSquares, unordered_set<int> &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<int> &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<int> perfectSquares = getPerfectSquares(max);
// Contains all the array elements
unordered_set<int> 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<int> 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<Integer> getPerfectSquares(int n)
{
ArrayList<Integer> 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<Integer> perfectSquares,
HashSet<Integer> 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<Integer> perfectSquares =
getPerfectSquares(max);
// Contains all the array elements
HashSet<Integer> 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
<?php
// PHP implementation of the approach
// Function to return an ArrayList containing
// all the perfect squares upto n
function getPerfectSquares($n)
{
$perfectSquares = array();
$current = 1;
$i = 1;
// while current perfect square
// is less than or equal to n
while ($current <= $n)
{
array_push($perfectSquares, $current);
$current = (int)pow(++$i, 2);
}
return $perfectSquares;
}
// Function to print the sum of maximum
// two elements from the array
function maxPairSum($arr)
{
$n = count($arr);
$max;
$secondMax;
if ($arr[0] > $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
?>
JavaScript
<script>
// Javascript implementation of the approach
// Function to return an ArrayList containing
// all the perfect squares upto n
function getPerfectSquares(n)
{
let perfectSquares = [];
let current = 1;
let i = 1;
// while current perfect square
// is less than or equal to n
while (current <= n)
{
perfectSquares.push(current);
current = Math.pow(++i, 2);
}
return perfectSquares;
}
// Function to print the sum of maximum
// two elements from the array
function maxPairSum(arr)
{
let n = arr.length
let max;
let secondMax;
if (arr[0] > arr[1])
{
max = arr[0];
secondMax = arr[1];
}
else
{
max = arr[1];
secondMax = arr[0];
}
for (let 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)
{
let count = 0;
for (let i = 0; i < perfectSquares.length; i++)
{
let temp = perfectSquares[i] - n;
// temp > n is checked so that pairs
// (x, y) and (y, x) don't get counted twice
if (temp > n && nums.includes(temp))
count++;
}
return count;
}
// Function to count the pairs whose
// sum is a perfect square
function countPairs(arr)
{
let n = arr.length
// Sum of the maximum two elements
// from the array
let max = maxPairSum(arr);
// List of perfect squares upto max
let perfectSquares = getPerfectSquares(max);
// Contains all the array elements
let nums = [];
for (let i = 0; i < n; i++)
nums.push(arr[i]);
let count = 0;
for (let 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
let arr = new Array( 2, 3, 6, 9, 10, 20 );
document.write(countPairs(arr));
// This code is contributed by Saurabh jaiswal
</script>
Time Complexity: O(n*sqrt(max))
Auxiliary Space: O(n + sqrt(max))
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read
Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read