- Related Questions & Answers
- Count the nodes whose weight is a perfect square in C++
- Count pairs in array whose sum is divisible by K in C++
- Count pairs in array whose sum is divisible by 4 in C++
- Count pairs in a sorted array whose sum is less than x in C++
- Count all triplets whose sum is equal to a perfect cube in C++
- Program to count number of permutations where sum of adjacent pairs are perfect square in Python
- Count number of distinct pairs whose sum exists in the given array in C++
- C Program to find sum of perfect square elements in an array using pointers.
- Count pairs whose products exist in array in C++
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) in C++
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N) in C++
- Count pairs in a sorted array whose product is less than k in C++
- Program to count number of fraction pairs whose sum is 1 in python
- Count all pairs of adjacent nodes whose XOR is an odd number in C++
- Count pairs in a binary tree whose sum is equal to a given value x in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given with an array of N elements. The goal is to find the count of all pairs (Arr[i],Arr[j]) which have a sum which is a perfect square such that i!=j. That is Arr[i]+Arr[j] is a perfect square.

We will do this by calculating the sum of pairs and check if the square root of that sum is equal to the floor value of the square root. sqrt(Arr[i]+Arr[j])-floor( sqrt(Arr[i]+Arr[j] )==0.

Let’s understand with examples.

**Input** − Arr[]= { 4,3,2,1,2,4 } N=6

**Output** − Count of pairs with sum as perfect square − 2

**Explanation** −

Arr[1]+Arr[3]=4, sqrt(4)-floor(4)=0 4 is a perfect square. Arr[2]+Arr[4]=4, sqrt(4)-floor(4)=0 4 is a perfect square. Rest all pairs have sum 7,6,5,8 which are not perfect squares.

**Input** − Arr[]= { 3,3,3,3,3} N=5

**Output** − Count of pairs with sum as perfect square − 0

**Explanation** − All pairs have sum=6, which is not a perfect square.

We take an integer array Arr[] initialized with random numbers for size of gloves > 0.

Take a variable n which stores the length of Arr[].

Function countPairs(int arr[], int n) takes an array, its length as input and returns the pairs which have sum which is a perfect square.

Traverse array using two for loops for each element of the pair.

Outer Loop from 0<=i<n-1, inner loop i<j<n

Calculate sum of arr[i], arr[j] are positive.

Calculate square root of sum as sqrt(sum).

Now check if sqr-floor(sqr)==0. Which means sum is a perfect square. If true increment count.

At the end of all loops count will have a total number of pairs that have sum which is perfect square.

Return the count as result.

#include <bits/stdc++.h> #include <math.h> using namespace std; int countPairs(int arr[], int n){ int count=0; int sum=0; double sqr=0; for(int i=0;i<n-1;i++){ for(int j=i+1;j<n;j++){ sum=arr[i]+arr[j]; sqr=sqrt(sum); if( sqr-floor(sqr)==0 ){ count++; //cout<<endl<<"a :"<<arr[i]<<" b :"<<arr[j]; //to print } } } return count; } int main(){ int arr[] = { 1, 2, 4, 8, 5, 6 }; // Size of arr[] int n = sizeof(arr) / sizeof(int); cout <<endl<<"Pairs whose sum is perfect square :"<<countPairs(arr, n); return 0; }

If we run the above code it will generate the following output −

Pairs whose sum is perfect square :2

Advertisements