
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count of pairs in an array whose sum is a perfect square in C++
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.
Approach used in the below program is as follows
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.
Example
#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; }
Output
If we run the above code it will generate the following output −
Pairs whose sum is perfect square :2
- Related Articles
- 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 the nodes whose weight is a perfect square in C++
- Count pairs in a sorted array whose sum is less than x 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++
- Count all triplets whose sum is equal to a perfect cube in C++
- Count pairs whose products exist in array in C++
- Program to count number of fraction pairs whose sum is 1 in python
- C Program to find sum of perfect square elements in an array using pointers.
- Count pairs in a sorted array whose product is less than k in C++
- Building an array of specific size with consecutive element sum being perfect square in JavaScript
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N) in C++
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) in C++
- Count pairs in a binary tree whose sum is equal to a given value x in C++
