Check if the sum of perfect squares in an array is divisible by x | C ++ implementation of the approach ; Function that returns true if the sum of all the perfect squares of the given array are divisible by x ; If arr [ i ] is a perfect square ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int arr [ ] , int x , int n ) { long long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { double x = sqrt ( arr [ i ] ) ; if ( floor ( x ) == ceil ( x ) ) { sum += arr [ i ] ; } } if ( sum % x == 0 ) return true ; else return false ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 9 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int x = 13 ; if ( check ( arr , x , n ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }