数组中存在平方根的元素的总和
给定一个数组arr[] ,任务是从给定数组中找到其平方根存在于同一数组中的所有元素的总和。
例子:
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
天真的方法:要找到给定数组中存在平方根的元素的总和,请通过从arr[0]迭代到arr[n]来检查每个元素的平方根,这将完成这项工作,但在O(n*n )复杂性。
下面是上述方法的实现:
C++
// CPP program to find the sum of all the elements
// from the array whose square root is present
// in the same array
#include
using namespace std;
// Function to return the required sum
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 sqrtCurrent is present in array
if (x == sqrtCurrent) {
sum += (sqrtCurrent * sqrtCurrent);
break;
}
}
}
return sum;
}
// Driver code
int main()
{
int arr[] = { 2, 4, 5, 6, 7, 8, 9, 3 };
int n = sizeof(arr)/sizeof(arr[0]);
cout<<(getSum(arr, n));
}
// This code is contributed by
// Surendra_Gangwar
Java
// Java program to find the sum of all the elements
// from the array whose square root is present
// in the same array
public class GFG {
// Function to return the required sum
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 sqrtCurrent is present in array
if (x == sqrtCurrent) {
sum += (sqrtCurrent * sqrtCurrent);
break;
}
}
}
return sum;
}
// Driver code
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
# Python3 program to find the sum of
# all the elements from the array
# whose square root is present in
# the same array
import math
# Function to return the required sum
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 sqrtCurrent is present in array
if (x == sqrtCurrent):
sum += (sqrtCurrent *
sqrtCurrent)
break
return int(sum)
# Driver code
if __name__ == '__main__':
arr = [ 2, 4, 5, 6, 7, 8, 9, 3]
n = len(arr)
print(getSum(arr, n))
# This code is contributed
# by 29AjayKumar
C#
// C# program to find the sum of all the elements
// from the array whose square root is present
// in the same array
using System ;
public class GFG {
// Function to return the required sum
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 sqrtCurrent is present in array
if (x == sqrtCurrent) {
sum += (sqrtCurrent * sqrtCurrent);
break;
}
}
}
return sum;
}
// Driver code
public static void Main()
{
int []arr = { 2, 4, 5, 6, 7, 8, 9, 3 };
int n = arr.Length;
Console.WriteLine(getSum(arr, n));
}
// This code is contributed by Ryuga
}
PHP
Javascript
C++
// C++ program to find the sum of all the elements
// from the array whose square root is present
// in the same array
#include
using namespace std;
// Function to return the required sum
int getSum(int arr[], int n)
{
int i, sum = 0;
// Initialization of hash map
set hashSet;
// Store each element in the hash map
for (i = 0; i < n; i++)
hashSet.insert(arr[i]);
for (i = 0; i < n; i++)
{
double sqrtCurrent = sqrt(arr[i]);
// If sqrtCurrent is a decimal number
if (floor(sqrtCurrent) != ceil(sqrtCurrent))
continue;
// If hash set contains sqrtCurrent
if (hashSet.find((int)sqrtCurrent) !=
hashSet.end())
{
sum += (sqrtCurrent * sqrtCurrent);
}
}
return sum;
}
// Driver code
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;
}
// This code is contributed by Rajput-Ji
Java
// Java program to find the sum of all the elements
// from the array whose square root is present
// in the same array
import java.util.*;
public class GFG {
// Function to return the required sum
public static int getSum(int arr[], int n)
{
int i, sum = 0;
// Initialization of hash map
Set hashSet = new HashSet<>();
// Store each element in the hash map
for (i = 0; i < n; i++)
hashSet.add(arr[i]);
for (i = 0; i < n; i++) {
double sqrtCurrent = Math.sqrt(arr[i]);
// If sqrtCurrent is a decimal number
if (Math.floor(sqrtCurrent) != Math.ceil(sqrtCurrent))
continue;
// If hash set contains sqrtCurrent
if (hashSet.contains((int)sqrtCurrent)) {
sum += (sqrtCurrent * sqrtCurrent);
}
}
return sum;
}
// Driver code
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#
// C# program to find the sum of all the elements
// from the array whose square root is present
// in the same array
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the required sum
public static int getSum(int []arr, int n)
{
int i, sum = 0;
// Initialization of hash map
HashSet hashSet = new HashSet();
// Store each element in the hash map
for (i = 0; i < n; i++)
hashSet.Add(arr[i]);
for (i = 0; i < n; i++)
{
double sqrtCurrent = Math.Sqrt(arr[i]);
// If sqrtCurrent is a decimal number
if (Math.Floor(sqrtCurrent) !=
Math.Ceiling(sqrtCurrent))
continue;
// If hash set contains sqrtCurrent
if (hashSet.Contains((int)sqrtCurrent))
{
sum += (int)(sqrtCurrent * sqrtCurrent);
}
}
return sum;
}
// Driver code
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));
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 program to find the sum of all the
# elements from the array whose square
# root is present in the same array
import math
# Function to return the required sum
def getSum(arr, n):
sum = 0;
# Initialization of hash map
hashSet = set();
# Store each element in the hash map
for i in range(n):
hashSet.add(arr[i]);
for i in range(n):
sqrtCurrent = math.sqrt(arr[i]);
# If sqrtCurrent is a decimal number
if (math.floor(sqrtCurrent) != math.ceil(sqrtCurrent)):
continue;
# If hash set contains sqrtCurrent
if (int(sqrtCurrent) in hashSet):
sum += int(sqrtCurrent * sqrtCurrent);
return sum;
# Driver code
arr = [ 2, 4, 5, 6, 7, 8, 9, 3 ];
n = len(arr);
print(getSum(arr, n));
# This code is contributed by mits
PHP
Javascript
输出:
13
有效方法:我们可以创建一个包含数组中所有元素的 HashSet,然后在 O(n) 时间内检查数组中每个元素的平方根。
下面是上述方法的实现:
C++
// C++ program to find the sum of all the elements
// from the array whose square root is present
// in the same array
#include
using namespace std;
// Function to return the required sum
int getSum(int arr[], int n)
{
int i, sum = 0;
// Initialization of hash map
set hashSet;
// Store each element in the hash map
for (i = 0; i < n; i++)
hashSet.insert(arr[i]);
for (i = 0; i < n; i++)
{
double sqrtCurrent = sqrt(arr[i]);
// If sqrtCurrent is a decimal number
if (floor(sqrtCurrent) != ceil(sqrtCurrent))
continue;
// If hash set contains sqrtCurrent
if (hashSet.find((int)sqrtCurrent) !=
hashSet.end())
{
sum += (sqrtCurrent * sqrtCurrent);
}
}
return sum;
}
// Driver code
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;
}
// This code is contributed by Rajput-Ji
Java
// Java program to find the sum of all the elements
// from the array whose square root is present
// in the same array
import java.util.*;
public class GFG {
// Function to return the required sum
public static int getSum(int arr[], int n)
{
int i, sum = 0;
// Initialization of hash map
Set hashSet = new HashSet<>();
// Store each element in the hash map
for (i = 0; i < n; i++)
hashSet.add(arr[i]);
for (i = 0; i < n; i++) {
double sqrtCurrent = Math.sqrt(arr[i]);
// If sqrtCurrent is a decimal number
if (Math.floor(sqrtCurrent) != Math.ceil(sqrtCurrent))
continue;
// If hash set contains sqrtCurrent
if (hashSet.contains((int)sqrtCurrent)) {
sum += (sqrtCurrent * sqrtCurrent);
}
}
return sum;
}
// Driver code
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#
// C# program to find the sum of all the elements
// from the array whose square root is present
// in the same array
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the required sum
public static int getSum(int []arr, int n)
{
int i, sum = 0;
// Initialization of hash map
HashSet hashSet = new HashSet();
// Store each element in the hash map
for (i = 0; i < n; i++)
hashSet.Add(arr[i]);
for (i = 0; i < n; i++)
{
double sqrtCurrent = Math.Sqrt(arr[i]);
// If sqrtCurrent is a decimal number
if (Math.Floor(sqrtCurrent) !=
Math.Ceiling(sqrtCurrent))
continue;
// If hash set contains sqrtCurrent
if (hashSet.Contains((int)sqrtCurrent))
{
sum += (int)(sqrtCurrent * sqrtCurrent);
}
}
return sum;
}
// Driver code
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));
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 program to find the sum of all the
# elements from the array whose square
# root is present in the same array
import math
# Function to return the required sum
def getSum(arr, n):
sum = 0;
# Initialization of hash map
hashSet = set();
# Store each element in the hash map
for i in range(n):
hashSet.add(arr[i]);
for i in range(n):
sqrtCurrent = math.sqrt(arr[i]);
# If sqrtCurrent is a decimal number
if (math.floor(sqrtCurrent) != math.ceil(sqrtCurrent)):
continue;
# If hash set contains sqrtCurrent
if (int(sqrtCurrent) in hashSet):
sum += int(sqrtCurrent * sqrtCurrent);
return sum;
# Driver code
arr = [ 2, 4, 5, 6, 7, 8, 9, 3 ];
n = len(arr);
print(getSum(arr, n));
# This code is contributed by mits
PHP
Javascript
输出:
13