Python Program to Find XOR of array elements which are divisible by given number
Last Updated :
05 Sep, 2024
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:
Python
def xorOfArray(arr, k):
# variable to store result
result = 0
# check if each element
# is divisible by given number.
for i in arr:
if i % k == 0:
result = result ^ i
return result
# Driver Code
if __name__ == '__main__':
arr = [3, 9, 12, 13, 15, 2, 6, 18]
# Function call
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.
Python
# Define an array
my_array = [3, 9, 12, 13, 15, 2, 6, 18]
# Define the given number
divisor = 2
# Use the filter() function to create an iterator of elements that are divisible by the given number
divisible_nums = filter(lambda x: x % divisor == 0, my_array)
# Initialize a variable to store the XOR result
Gfgresult = 0
# Use a for loop to calculate the XOR of elements that are
# divisible by the given number and store the result in the result variable
for num in divisible_nums:
Gfgresult ^= num
# Print the XOR result
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.
Python
def xorOfArray(arr, k):
xor = 0
for i in range(31, -1, -1): # iterate through bit positions from MSB to LSB
count = 0
for num in arr:
if num % k == 0 and num & (1 << i): # check if num is divisible by k and bit i is set
count += 1
if count % 2: # if count is odd, set bit i in xor
xor |= 1 << i
return xor
# Driver Code
if __name__ == '__main__':
arr = [3, 9, 12, 13, 15, 2, 6, 18]
# Function call
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.
Similar Reads
Python program to find tuples which have all elements divisible by K from a list of tuples Given a list of tuples. The task is to extract all tuples which have all elements divisible by K. Input : test_list = [(6, 24, 12), (60, 12, 6), (12, 18, 21)], K = 6 Output : [(6, 24, 12), (60, 12, 6)] Explanation : Both tuples have all elements multiple of 6. Input : test_list = [(6, 24, 12), (60,
7 min read
Python Program to Find Numbers Divisible by Another Number We are given a list of numbers and a number. We have to find all the numbers in the list that are divisible by the given single number. Examples:Input: list=[8, 14, 21, 36, 43], num=3Output: 21, 36, 57Input: list=[2, 17, 25, 31, 48, 55], num=5Output: 25, 55In this article, we will discuss the differ
3 min read
Python Program to print all the numbers divisible by 3 and 5 for a given number Given the integer N, the task is to print all the numbers less than N, which are divisible by 3 and 5.Examples : Input : 50Output : 0 15 30 45 Input : 100Output : 0 15 30 45 60 75 90 Approach: For example, let's take N = 20 as a limit, then the program should print all numbers less than 20 which are
2 min read
Python Program to Print all Integers that Aren't Divisible by Either 2 or 3 We can input a set of integer, and check which integers in this range, beginning with 1 are not divisible by 2 or 3, by checking the remainder of the integer with 2 and 3. Example: Input: 10 Output: Numbers not divisible by 2 and 3 1 5 7 Method 1: We check if the number is not divisible by 2 and 3 u
7 min read
Python Program for Find remainder of array multiplication divided by n Write a Python program for a given multiple numbers and a number n, the task is to print the remainder after multiplying all the numbers divided by n. Examples: Input: arr[] = {100, 10, 5, 25, 35, 14}, n = 11Output: 9Explanation: 100 x 10 x 5 x 25 x 35 x 14 = 61250000 % 11 = 9Input : arr[] = {100, 1
3 min read