- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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.

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 prod=0; for(int i=0;i<n-1;i++){ for(int j=i+1;j<n;j++){ prod=arr[i]*arr[j]; if( ceil(log2(prod))==floor(log2(prod)) ){ count++; //cout<<endl<<"a :"<<arr[i]<<" b :"<<arr[j]; //to print } } } return count; } int main(){ int arr[] = { 2, 5, 8, 16, 128 }; int n = sizeof(arr) / sizeof(arr[0]); cout <<endl<<"Pairs whose product is power of 2:"<<countPairs(arr, n); return 0; }

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

Pairs whose product is power of 2:6

- Related Questions & Answers
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized in C++
- Count pairs (i,j) such that (i+j) is divisible by both A and B in C++
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] in C++
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++
- Count pairs in an array that hold i*arr[i] > j*arr[j] in C++
- Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix in C++
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i in C++
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j in C++
- Count the triplets such that A[i] < B[j] < C[k] in C++
- Program to find a pair (i, j) where nums[i] + nums[j] + (i -j) is maximized in Python?
- Maximum value of |arr[i] – arr[j] - + |i – j| in C++
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in C++
- Program to find number of pairs (i, j) such that ith and jth elements are same in Python

Advertisements