Count array elements whose perfect squares are present in the given array
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
#include <bits/stdc++.h>
using namespace std;
void countSquares( int arr[], int N)
{
int count = 0;
for ( int i = 0; i < N; i++) {
int square = arr[i] * arr[i];
for ( int j = 0; j < N; j++) {
if (arr[j] == square) {
count = count + 1;
}
}
}
cout << count;
}
int main()
{
int arr[] = { 2, 4, 5, 20, 16 };
int N = sizeof (arr) / sizeof (arr[0]);
countSquares(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void countSquares( int arr[], int N)
{
int count = 0 ;
for ( int i = 0 ; i < N; i++)
{
int square = arr[i] * arr[i];
for ( int j = 0 ; j < N; j++)
{
if (arr[j] == square)
{
count = count + 1 ;
}
}
}
System.out.print(count);
}
public static void main(String[] args)
{
int arr[] = { 2 , 4 , 5 , 20 , 16 };
int N = arr.length;
countSquares(arr, N);
}
}
|
Python3
def countSquares(arr, N):
count = 0 ;
for i in range (N):
square = arr[i] * arr[i];
for j in range (N):
if (arr[j] = = square):
count = count + 1 ;
print (count);
if __name__ = = '__main__' :
arr = [ 2 , 4 , 5 , 20 , 16 ];
N = len (arr);
countSquares(arr, N);
|
C#
using System;
class GFG{
static void countSquares( int [] arr, int N)
{
int count = 0;
for ( int i = 0; i < N; i++)
{
int square = arr[i] * arr[i];
for ( int j = 0; j < N; j++)
{
if (arr[j] == square)
{
count = count + 1;
}
}
}
Console.WriteLine(count);
}
static void Main()
{
int [] arr = { 2, 4, 5, 20, 16 };
int N = arr.Length;
countSquares(arr, N);
}
}
|
Javascript
<script>
function countSquares(arr, N)
{
var count = 0;
for ( var i = 0; i < N; i++) {
var square = arr[i] * arr[i];
for ( var j = 0; j < N; j++) {
if (arr[j] == square) {
count = count + 1;
}
}
}
document.write( count);
}
var arr = [2, 4, 5, 20, 16];
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
#include <bits/stdc++.h>
using namespace std;
int countSquares( int arr[], int N)
{
int count = 0;
unordered_map< int , int > m;
for ( int i = 0; i < N; i++) {
m[arr[i]] = m[arr[i]] + 1;
}
for ( int i = 0; i < N; i++) {
int square = arr[i] * arr[i];
count += m[square];
}
cout << count;
}
int main()
{
int arr[] = { 2, 4, 5, 20, 16 };
int N = sizeof (arr) / sizeof (arr[0]);
countSquares(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void countSquares( int arr[], int N)
{
int count = 0 ;
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
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++)
{
int square = arr[i] * arr[i];
count += mp.containsKey(square)?mp.get(square): 0 ;
}
System.out.print(count);
}
public static void main(String[] args)
{
int arr[] = { 2 , 4 , 5 , 20 , 16 };
int N = arr.length;
countSquares(arr, N);
}
}
|
Python3
from collections import defaultdict
def countSquares( arr, N):
count = 0 ;
m = defaultdict( int );
for i in range (N):
m[arr[i]] = m[arr[i]] + 1
for i in range ( N ):
square = arr[i] * arr[i]
count + = m[square]
print (count)
if __name__ = = "__main__" :
arr = [ 2 , 4 , 5 , 20 , 16 ]
N = len (arr)
countSquares(arr, N);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void countSquares( int []arr, int N)
{
int count = 0;
Dictionary< int , int > mp =
new Dictionary< int , int >();
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++)
{
int square = arr[i] * arr[i];
count += mp.ContainsKey(square)?mp[square]:0;
}
Console.Write(count);
}
public static void Main()
{
int []arr = { 2, 4, 5, 20, 16 };
int N = arr.Length;
countSquares(arr, N);
}
}
|
Javascript
<script>
function countSquares(arr, N)
{
var count = 0;
var m = new Map();
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++) {
var square = arr[i] * arr[i];
if (m.has(square))
count += m.get(square);
}
document.write( count);
}
var arr = [2, 4, 5, 20, 16];
var N = arr.length;
countSquares(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
13 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...