XOR of all Array elements that are perfect squares
Last Updated :
09 Mar, 2023
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: 25
Explanation: Only 9, 16 are the perfect squares in the given array.
So the XOR of 9 and 16 is 25, XOR = 9 ^ 16 = 25
Input: arr[] = { 3, 9, 12, 13, 15 }
Output: 9
Explanation: Only 9 is the perfect square so the answer will be 9.
Approach: To solve the problem follow the below idea:
In order to find the XOR of perfect square elements in the array, simply iterate through the array and find if an element is a perfect square or not, and then calculate the XOR using.
Follow the below steps to compute the answer:
- Declare a variable ( say xor_arr) to store the answer and initialize it to 0.
- Iterate from the start of the array:
- For each element in the array, check if it is a perfect square or not.
- A simple way to check is if the ceil and floor of the square root of an element are equal then the number is a perfect square.
- Find the XOR of the perfect square element and store it in the result variable using the ‘^’ operator.
- Return the xor_arr variable that stores the XOR of all perfect square elements in the array.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool checkperfectsquare( int x)
{
return ceil ( sqrt (x)) == floor ( sqrt (x));
}
int xorOfArray( int arr[], int n)
{
int xor_arr = 0;
for ( int i = 0; i < n; i++) {
if (checkperfectsquare(arr[i])) {
xor_arr = xor_arr ^ arr[i];
}
}
return xor_arr;
}
int main()
{
int arr[] = { 3, 9, 16, 12, 13, 15 };
int N = 6;
cout << xorOfArray(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean checkperfectsquare( int x)
{
return Math.ceil(Math.sqrt(x))
== Math.floor(Math.sqrt(x));
}
static int xorOfArray( int [] arr, int n)
{
int xor_arr = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (checkperfectsquare(arr[i])) {
xor_arr = xor_arr ^ arr[i];
}
}
return xor_arr;
}
public static void main(String[] args)
{
int [] arr = { 3 , 9 , 16 , 12 , 13 , 15 };
int N = arr.length;
System.out.print(xorOfArray(arr, N));
}
}
|
Python3
import math
def checkperfectsquare(x):
return math.ceil(math.sqrt(x)) = = math.floor(math.sqrt(x))
def xorOfArray(arr, n):
xor_arr = 0
for i in range (n):
if checkperfectsquare(arr[i]):
xor_arr = xor_arr ^ arr[i]
return xor_arr
if __name__ = = '__main__' :
arr = [ 3 , 9 , 16 , 12 , 13 , 15 ]
N = len (arr)
print (xorOfArray(arr, N))
|
C#
using System;
class GFG {
static Boolean checkperfectsquare( int x)
{
return Math.Ceiling(Math.Sqrt(x))
== Math.Floor(Math.Sqrt(x));
}
static int xorOfArray( int [] arr, int n)
{
int xor_arr = 0;
for ( int i = 0; i < n; i++)
{
if (checkperfectsquare(arr[i])) {
xor_arr = xor_arr ^ arr[i];
}
}
return xor_arr;
}
public static void Main()
{
int [] arr = { 3, 9, 16, 12, 13, 15 };
int N = arr.Length;
Console.Write(xorOfArray(arr, N));
}
}
|
Javascript
function checkperfectsquare(x) {
return Math.ceil(Math.sqrt(x)) == Math.floor(Math.sqrt(x))
}
function xorOfArray(arr, n) {
let xor_arr = 0
for (let i = 0; i < n; i++) {
if (checkperfectsquare(arr[i])) {
xor_arr = xor_arr ^ arr[i]
}
}
return xor_arr
}
arr = [3, 9, 16, 12, 13, 15]
N = arr.length
console.log(xorOfArray(arr, N))
|
Time Complexity: O(Nlog(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...