Sum of elements whose square root is present in the array
Last Updated :
12 Sep, 2022
Given an array arr[], the task is to find the sum of all those elements from the given array whose square root is present in the same array.
Examples:
Input: arr[] = {1, 2, 3, 4, 6, 9, 10}
Output: 13
4 and 9 are the only numbers whose square roots 2 and 3 are present in the array
Input: arr[] = {4, 2, 36, 6, 10, 100}
Output: 140
Naive Approach: To find the sum of elements whose square root is present in the given array, check for the square root of every element by iterating from arr[0] to arr[n] which will do the job but in O(n*n) complexity.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int getSum( int arr[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
double sqrtCurrent = sqrt (arr[i]);
for ( int j = 0; j < n; j++) {
double x = arr[j];
if (x == sqrtCurrent) {
sum += (sqrtCurrent * sqrtCurrent);
break ;
}
}
}
return sum;
}
int main()
{
int arr[] = { 2, 4, 5, 6, 7, 8, 9, 3 };
int n = sizeof (arr)/ sizeof (arr[0]);
cout<<(getSum(arr, n));
}
|
Java
public class GFG {
public static int getSum( int arr[], int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++) {
double sqrtCurrent = Math.sqrt(arr[i]);
for ( int j = 0 ; j < n; j++) {
double x = arr[j];
if (x == sqrtCurrent) {
sum += (sqrtCurrent * sqrtCurrent);
break ;
}
}
}
return sum;
}
public static void main(String args[])
{
int arr[] = { 2 , 4 , 5 , 6 , 7 , 8 , 9 , 3 };
int n = arr.length;
System.out.println(getSum(arr, n));
}
}
|
Python3
import math
def getSum(arr, n):
sum = 0
for i in range ( 0 , n):
sqrtCurrent = math.sqrt(arr[i])
for j in range ( 0 , n):
x = arr[j]
if (x = = sqrtCurrent):
sum + = (sqrtCurrent *
sqrtCurrent)
break
return int ( sum )
if __name__ = = '__main__' :
arr = [ 2 , 4 , 5 , 6 , 7 , 8 , 9 , 3 ]
n = len (arr)
print (getSum(arr, n))
|
C#
using System ;
public class GFG {
public static float getSum( int []arr, int n)
{
float sum = 0;
for ( int i = 0; i < n; i++) {
float sqrtCurrent = ( float )Math.Sqrt(arr[i]);
for ( int j = 0; j < n; j++) {
float x = ( float )arr[j];
if (x == sqrtCurrent) {
sum += (sqrtCurrent * sqrtCurrent);
break ;
}
}
}
return sum;
}
public static void Main()
{
int []arr = { 2, 4, 5, 6, 7, 8, 9, 3 };
int n = arr.Length;
Console.WriteLine(getSum(arr, n));
}
}
|
PHP
<?php
function getSum(& $arr , $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$sqrtCurrent = sqrt( $arr [ $i ]);
for ( $j = 0; $j < $n ; $j ++)
{
$x = $arr [ $j ];
if ( $x == $sqrtCurrent )
{
$sum += ( $sqrtCurrent * $sqrtCurrent );
break ;
}
}
}
return $sum ;
}
$arr = array (2, 4, 5, 6, 7, 8, 9, 3);
$n = sizeof( $arr );
echo (getSum( $arr , $n ));
?>
|
Javascript
<script>
function getSum(arr, n)
{
let sum = 0;
for (let i = 0; i < n; i++) {
let sqrtCurrent = Math.sqrt(arr[i]);
for (let j = 0; j < n; j++) {
let x = arr[j];
if (x == sqrtCurrent) {
sum += (sqrtCurrent * sqrtCurrent);
break ;
}
}
}
return sum;
}
let arr = [ 2, 4, 5, 6, 7, 8, 9, 3 ];
let n = arr.length;
document.write(getSum(arr, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(n2)
- Auxiliary Space: O(1)
Efficient Approach: We can create a HashSet of all the elements present in the array and then check for the square root of each element of the array in O(n) time.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int getSum( int arr[], int n)
{
int i, sum = 0;
set< int > hashSet;
for (i = 0; i < n; i++)
hashSet.insert(arr[i]);
for (i = 0; i < n; i++)
{
double sqrtCurrent = sqrt (arr[i]);
if ( floor (sqrtCurrent) != ceil (sqrtCurrent))
continue ;
if (hashSet.find(( int )sqrtCurrent) !=
hashSet.end())
{
sum += (sqrtCurrent * sqrtCurrent);
}
}
return sum;
}
int main()
{
int arr[] = { 2, 4, 5, 6, 7, 8, 9, 3 };
int n = sizeof (arr)/ sizeof (arr[0]);
cout << (getSum(arr, n));
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static int getSum( int arr[], int n)
{
int i, sum = 0 ;
Set<Integer> hashSet = new HashSet<>();
for (i = 0 ; i < n; i++)
hashSet.add(arr[i]);
for (i = 0 ; i < n; i++) {
double sqrtCurrent = Math.sqrt(arr[i]);
if (Math.floor(sqrtCurrent) != Math.ceil(sqrtCurrent))
continue ;
if (hashSet.contains(( int )sqrtCurrent)) {
sum += (sqrtCurrent * sqrtCurrent);
}
}
return sum;
}
public static void main(String args[])
{
int arr[] = { 2 , 4 , 5 , 6 , 7 , 8 , 9 , 3 };
int n = arr.length;
System.out.println(getSum(arr, n));
}
}
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int getSum( int []arr, int n)
{
int i, sum = 0;
HashSet< int > hashSet = new HashSet< int >();
for (i = 0; i < n; i++)
hashSet.Add(arr[i]);
for (i = 0; i < n; i++)
{
double sqrtCurrent = Math.Sqrt(arr[i]);
if (Math.Floor(sqrtCurrent) !=
Math.Ceiling(sqrtCurrent))
continue ;
if (hashSet.Contains(( int )sqrtCurrent))
{
sum += ( int )(sqrtCurrent * sqrtCurrent);
}
}
return sum;
}
public static void Main(String []args)
{
int []arr = { 2, 4, 5, 6, 7, 8, 9, 3 };
int n = arr.Length;
Console.WriteLine(getSum(arr, n));
}
}
|
Python3
import math
def getSum(arr, n):
sum = 0 ;
hashSet = set ();
for i in range (n):
hashSet.add(arr[i]);
for i in range (n):
sqrtCurrent = math.sqrt(arr[i]);
if (math.floor(sqrtCurrent) ! = math.ceil(sqrtCurrent)):
continue ;
if ( int (sqrtCurrent) in hashSet):
sum + = int (sqrtCurrent * sqrtCurrent);
return sum ;
arr = [ 2 , 4 , 5 , 6 , 7 , 8 , 9 , 3 ];
n = len (arr);
print (getSum(arr, n));
|
PHP
<?php
function getSum( $arr , $n )
{
$sum = 0;
$hashSet = array ();
for ( $i = 0; $i < $n ; $i ++)
array_push ( $hashSet , $arr [ $i ]);
$hashSet = array_unique ( $hashSet );
for ( $i = 0; $i < $n ; $i ++)
{
$sqrtCurrent = sqrt( $arr [ $i ]);
if ( floor ( $sqrtCurrent ) != ceil ( $sqrtCurrent ))
continue ;
if (in_array((int) $sqrtCurrent , $hashSet ))
{
$sum += ( $sqrtCurrent * $sqrtCurrent );
}
}
return $sum ;
}
$arr = array ( 2, 4, 5, 6, 7, 8, 9, 3 );
$n = count ( $arr );
print (getSum( $arr , $n ));
?>
|
Javascript
<script>
function getSum(arr, n)
{
let i, sum = 0;
let hashSet = new Set();
for (i = 0; i < n; i++)
hashSet.add(arr[i]);
for (i = 0; i < n; i++)
{
let sqrtCurrent = Math.sqrt(arr[i]);
if (Math.floor(sqrtCurrent) !=
Math.ceil(sqrtCurrent))
continue ;
if (hashSet.has(sqrtCurrent))
{
sum += (sqrtCurrent * sqrtCurrent);
}
}
return sum;
}
let arr = [ 2, 4, 5, 6, 7, 8, 9, 3 ];
let n = arr.length;
document.write(getSum(arr, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...