Python Program to Find XOR of array elements which are divisible by given number
Last Updated :
23 Apr, 2023
Given an array arr[] containing integers of size N and a number k, the task is to find the XOR of array elements that are divisible by a given number k in Python.
Examples:
Input: arr[] = {3, 9, 16, 12, 13, 15} , k = 3
Output: 25
Explanation: Only 3,9,12,15 are divisible by 3, XOR = 3^9^12^15 = 9
Input: arr[] = { 3, 9, 12, 13, 15, 2 , 6, 18 } , k=2
Output:
Explanation: Only 12,2,6,18 are divisible by 2, XOR= 12^2^6^18 = 26
Approach:
In order to find the XOR of numbers that are divisible by a given number in the array, we simply iterate through the array and find if an element is divisible by k or not, and then we XOR using the ‘^’ operator. Therefore, the following steps are followed to compute the answer:
- Create a variable to store the XOR of the array elements as a result.
- For each element in the array, check if an element is divisible by a given number or not.
- Find the XOR of the number and store it in the result variable using the ‘^’ operator.
- Finally, the result variable stores the XOR of all array elements which are divisible by the given number k.
Below is the implementation of the above approach:
Python3
def xorOfArray(arr, k):
result = 0
for i in arr:
if i % k = = 0 :
result = result ^ i
return result
if __name__ = = '__main__' :
arr = [ 3 , 9 , 12 , 13 , 15 , 2 , 6 , 18 ]
k = 2
print (xorOfArray(arr, k))
|
Complexity Analysis:
Time Complexity: O(n)
Space Complexity: O(1)
Method 2: Using the filter() function :
Step-by-Step Approach :
- First, let’s define an array named my_array.
- Then define a divisor.
- Now, use the filter() function to create an iterator divisible_nums containing only the elements in my_array that are divisible by divisor.
- Initialize a variable Gfgresult to 0.
- Use a for loop to iterate over divisible_nums.
- For each element in divisible_nums, compute its XOR with the Gfgresult using the bitwise XOR operator ^ and store the result back in Gfgresult.
- Print the value of Gfgresult.
Python3
my_array = [ 3 , 9 , 12 , 13 , 15 , 2 , 6 , 18 ]
divisor = 2
divisible_nums = filter ( lambda x: x % divisor = = 0 , my_array)
Gfgresult = 0
for num in divisible_nums:
Gfgresult ^ = num
print (Gfgresult)
|
Complexity Analysis:
Time Complexity: O(n)
This is because the filter function takes O(n) time to create the iterator containing the elements that are divisible by the given number, where n is the length of the input array. Now the for loop iterates over the numbers that are divisible by divisor for a constant number of iteration, so we can say that the overall time complexity will be O(n).
Space Complexity: O(1)
This is because we are only using few variables to store the input array, the divisor, divisible_sum, and the Gfgresult and the space used by these variables does not depend on the size of the input array. So we can say that the overall space complexity will be O(1).
Method #3: using bit manipulation
- Initialize a variable xor to zero.
- For each bit position from the least significant bit to the most significant bit:
- Initialize a variable count to zero.
- For each element num in the array:
- If the num is divisible by the given number and the bit at the current bit position is set in num, increment count.
- If the count is odd, set the bit at the current bit position in xor.
- Return the final value of xor.
Python3
def xorOfArray(arr, k):
xor = 0
for i in range ( 31 , - 1 , - 1 ):
count = 0
for num in arr:
if num % k = = 0 and num & ( 1 << i):
count + = 1
if count % 2 :
xor | = 1 << i
return xor
if __name__ = = '__main__' :
arr = [ 3 , 9 , 12 , 13 , 15 , 2 , 6 , 18 ]
k = 2
print (xorOfArray(arr, k))
|
The time complexity is O(n * log(max_num)), where n is the length of the input array and max_num is the maximum value in the input array.
The space complexity is O(1) because we only use a fixed number of variables.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...