Count array elements whose perfect squares are present in the given array
Last Updated :
13 Jan, 2022
Given an array arr[], the task is to find the count of array elements whose squares are already present in the array.
Examples:
Input: arr[] = {2, 4, 5, 20, 16}
Output: 2
Explanation:
{2, 4} has their squares {4, 16} present in the array.
Input: arr[] = {1, 30, 3, 8, 64}
Output: 2
Explanation:
{1, 8} has their squares {1, 64} present in the array.
Naive Approach: Follow the steps below to solve the problem:
- Initialize a variable, say, count, to store the required count.
- Traverse the array and for each and every array element, perform the following operations:
- Traverse the array and search if the square of the current element is present in the array.
- If the square found increment the count.
- Print count as the answer.
Below is the implementation of the above approach:
C++14
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the count of elements whose
// squares are already present int the array
void countSquares(int arr[], int N)
{
// Stores the required count
int count = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// Square of the element
int square = arr[i] * arr[i];
// Traverse the array
for (int j = 0; j < N; j++) {
// Check whether the value
// is equal to square
if (arr[j] == square) {
// Increment count
count = count + 1;
}
}
}
// Print the count
cout << count;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 2, 4, 5, 20, 16 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
countSquares(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the count of elements whose
// squares are already present int the array
static void countSquares(int arr[], int N)
{
// Stores the required count
int count = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
// Square of the element
int square = arr[i] * arr[i];
// Traverse the array
for (int j = 0; j < N; j++)
{
// Check whether the value
// is equal to square
if (arr[j] == square)
{
// Increment count
count = count + 1;
}
}
}
// Print the count
System.out.print(count);
}
// Driver Code
public static void main(String[] args)
{
// Given array
int arr[] = { 2, 4, 5, 20, 16 };
// Size of the array
int N = arr.length;
countSquares(arr, N);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python program for the above approach
# Function to find the count of elements whose
# squares are already present the array
def countSquares(arr, N):
# Stores the required count
count = 0;
# Traverse the array
for i in range(N):
# Square of the element
square = arr[i] * arr[i];
# Traverse the array
for j in range(N):
# Check whether the value
# is equal to square
if (arr[j] == square):
# Increment count
count = count + 1;
# Print count
print(count);
# Driver Code
if __name__ == '__main__':
# Given array
arr = [2, 4, 5, 20, 16];
# Size of the array
N = len(arr);
countSquares(arr, N);
# This code is contributed by shikhasingrajput
C#
// C# program of the above approach
using System;
class GFG{
// Function to find the count of elements whose
// squares are already present int the array
static void countSquares(int[] arr, int N)
{
// Stores the required count
int count = 0;
// Traverse the array
for(int i = 0; i < N; i++)
{
// Square of the element
int square = arr[i] * arr[i];
// Traverse the array
for(int j = 0; j < N; j++)
{
// Check whether the value
// is equal to square
if (arr[j] == square)
{
// Increment count
count = count + 1;
}
}
}
// Print the count
Console.WriteLine(count);
}
// Driver code
static void Main()
{
// Given array
int[] arr = { 2, 4, 5, 20, 16 };
// Size of the array
int N = arr.Length;
countSquares(arr, N);
}
}
// This code is contributed by divyeshrabadiya07
JavaScript
<script>
// Javascript program for the above approach
// Function to find the count of elements whose
// squares are already present int the array
function countSquares(arr, N)
{
// Stores the required count
var count = 0;
// Traverse the array
for (var i = 0; i < N; i++) {
// Square of the element
var square = arr[i] * arr[i];
// Traverse the array
for (var j = 0; j < N; j++) {
// Check whether the value
// is equal to square
if (arr[j] == square) {
// Increment count
count = count + 1;
}
}
}
// Print the count
document.write( count);
}
// Driver Code
// Given array
var arr = [2, 4, 5, 20, 16];
// Size of the array
var N = arr.length;
countSquares(arr, N);
</script>
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The optimal idea is to use unordered_map to keep the count of visited elements and update the variable count accordingly. Below are the steps:
- Initialize a Map to store the frequency of array elements and initialize a variable, say, count.
- Traverse the array and for each element, increment its count in the Map.
- Again traverse the array and for each element check for the frequency of the square of the element in the map and add it to the variable count.
- Print count as the number of elements whose square is already present in the array.
Below is the implementation of the above approach:
C++14
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the count of elements whose
// squares is already present int the array
int countSquares(int arr[], int N)
{
// Stores the count of array elements
int count = 0;
// Stores frequency of visited elements
unordered_map<int, int> m;
// Traverse the array
for (int i = 0; i < N; i++) {
m[arr[i]] = m[arr[i]] + 1;
}
for (int i = 0; i < N; i++) {
// Square of the element
int square = arr[i] * arr[i];
// Update the count
count += m[square];
}
// Print the count
cout << count;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 2, 4, 5, 20, 16 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
countSquares(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find the count of elements whose
// squares is already present int the array
static void countSquares(int arr[], int N)
{
// Stores the count of array elements
int count = 0;
// Stores frequency of visited elements
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
// Traverse the array
for (int i = 0; i < N; i++)
{
if(mp.containsKey(arr[i]))
{
mp.put(arr[i], mp.get(arr[i]) + 1);
}
else
{
mp.put(arr[i], 1);
}
}
for (int i = 0; i < N; i++)
{
// Square of the element
int square = arr[i] * arr[i];
// Update the count
count += mp.containsKey(square)?mp.get(square):0;
}
// Print the count
System.out.print(count);
}
// Driver Code
public static void main(String[] args)
{
// Given array
int arr[] = { 2, 4, 5, 20, 16 };
// Size of the array
int N = arr.length;
// Function Call
countSquares(arr, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python 3 program for the above approach
from collections import defaultdict
# Function to find the count of elements whose
# squares is already present int the array
def countSquares( arr, N):
# Stores the count of array elements
count = 0;
# Stores frequency of visited elements
m = defaultdict(int);
# Traverse the array
for i in range(N):
m[arr[i]] = m[arr[i]] + 1
for i in range( N ):
# Square of the element
square = arr[i] * arr[i]
# Update the count
count += m[square]
# Print the count
print(count)
# Driver Code
if __name__ == "__main__":
# Given array
arr = [ 2, 4, 5, 20, 16 ]
# Size of the array
N = len(arr)
# Function Call
countSquares(arr, N);
# This code is contributed by chitranayal.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the count of elements whose
// squares is already present int the array
static void countSquares(int []arr, int N)
{
// Stores the count of array elements
int count = 0;
// Stores frequency of visited elements
Dictionary<int, int> mp =
new Dictionary<int, int>();
// Traverse the array
for (int i = 0; i < N; i++)
{
if(mp.ContainsKey(arr[i]))
{
mp.Add(arr[i], mp[arr[i]] + 1);
}
else
{
mp.Add(arr[i], 1);
}
}
for (int i = 0; i < N; i++)
{
// Square of the element
int square = arr[i] * arr[i];
// Update the count
count += mp.ContainsKey(square)?mp[square]:0;
}
// Print the count
Console.Write(count);
}
// Driver Code
public static void Main()
{
// Given array
int []arr = { 2, 4, 5, 20, 16 };
// Size of the array
int N = arr.Length;
// Function Call
countSquares(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal
JavaScript
<script>
// Javascript program for the above approach
// Function to find the count of elements whose
// squares is already present int the array
function countSquares(arr, N)
{
// Stores the count of array elements
var count = 0;
// Stores frequency of visited elements
var m = new Map();
// Traverse the array
for (var i = 0; i < N; i++) {
if(m.has(arr[i]))
m.set(arr[i], m.get(arr[i])+1)
else
m.set(arr[i], 1);
}
for (var i = 0; i < N; i++) {
// Square of the element
var square = arr[i] * arr[i];
// Update the count
if(m.has(square))
count += m.get(square);
}
// Print the count
document.write( count);
}
// Driver Code
// Given array
var arr = [2, 4, 5, 20, 16];
// Size of the array
var N = arr.length;
// Function Call
countSquares(arr, N);
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Count elements in an Array that can be represented as difference of two perfect squares Given an array arr[], the task is to count the number of elements in the array that can be represented as in the form of the difference of two perfect square numbers. a^2 - b^2 Examples: Input: arr[] = {1, 2, 3} Output: 2 Explanation: There are two such elements that can be represented as difference
5 min read
Check if the Product of all Array elements is a Perfect Square or not Given an array arr[] consisting of N positive integers, the task is to check if the product of all the elements of the given array arr[] is a perfect square or not. If found to be true, then print Yes. Otherwise, print No. Examples: Input: arr[] = {1, 4, 100}Output: YesExplanation: The product of al
6 min read
Count of pairs in an array whose sum is a perfect square Given an array arr of distinct elements, the task is to find the total number of two element pairs from the array whose sum is a perfect square. Examples: Input: arr[] = {2, 3, 6, 9, 10, 20} Output: 2 Only possible pairs are (3, 6) and (6, 10) Input: arr[] = {9, 2, 5, 1} Output: 0 Naive Approach: Us
12 min read
XOR of all Array elements that are perfect squares Given an array arr[] containing N integers, the task is to find the XOR of all the elements of this array that are perfect squares. Examples: Input: arr[] = {3, 9, 16, 12, 13, 15} Output: 25Explanation: Only 9, 16 are the perfect squares in the given array.So the XOR of 9 and 16 is 25, XOR = 9 ^ 16
5 min read
Count ways to represent a number as sum of perfect squares Given an integer N, the task is to find the number of ways to represent the number N as sum of perfect squares. Examples: Input: N = 9Output: 4Explanation:There are four ways to represent 9 as the sum of perfect squares:1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 91 + 1 + 1 + 1 + 1 + 4 = 91 + 4 + 4 = 99 = 9
13 min read
Count of primes in a given range that can be expressed as sum of perfect squares Given two integers L and R, the task is to find the number of prime numbers in the range [L, R] that can be represented by the sum of two squares of two numbers.Examples: Input: L = 1, R = 5 Output: 1 Explanation: Only prime number that can be expressed as sum of two perfect squares in the given ran
7 min read