Find XOR of Array Elements Divisible by Given Number in Python



In this article, we will discuss how to compute the XOR of array elements that are divisible by a given number. The XOR (exclusive OR) is a binary operation that compares the bits of two operands. If the bits are different then it will return 1 whereas it returns 0 if the bits are the same. In this approach, we will use the property of XOR and apply this to this problem. The solution to this problem is shown with a relevant example. Let's take an example to demonstrate the computation. Consider a simple example for understanding purposes, the array ?arr' = [1,2,3,4,5]. We will calculate the XOR of its elements followed by step-by-step process.

  • Initialize xor_value to 0.

  • Begin iterating over each element:

    • First element num = 1, perform xor_value^num. Since xor_value = 0, result is 1. Update xor_value = 1.

    • 2nd element num = 2, perform xor_value^num. Current xor_value = 1, result is 3. Update xor_value = 3.

    • 3rd element num = 3, perform xor_value^num. Current xor_value = 3, result is 0. Update xor_value = 0.

    • 4th element num = 4, perform xor_value^num. Current xor_value = 0, result is 0. Update xor_value = 4.

    • 5th element num = 5, perform xor_value^num. Current xor_value = 4, result is 1. Update xor_value = 1.

  • Iteration is completed and the final xor_value is 1.

  • Return xor_value as the result.

In the given example, The XOR of the array [1,2,3,4,5] is 1.

We can use this computation to any array to obtain the XOR operation of its elements.

Example 1: Python Program to find XOR of array elements that are divisible by given number

Code Explanation and Design Steps

Step 1: Open Jupyter Notebook in the Anaconda prompt and start writing the code in its cell.

Step 2: Use this function ?xor_divisible_elements' which takes two parameters ?arr' as an input array and ?divisor' is a number by which elements must be divisible.

Step 3: Initialize ?xor_value' to 0.

Step 4: The XOR value of the divisible number will be stored in ?xor_value' for all elements.

Step 5: Use the ?for' loop to iterate to each element of ?num' in the ?arr'.

Step 6: In the ?for' loop, using modulo operator (%) we check whether ?num' is divisible by ?divisor' or not. If we get the remainder is 0 then ?num' is divisible by ?divisor' absolutely. Check the result.

Step 7: Update the ?xor_value', performing the XOR operation with ?num' using the XOR assignment operator ?^='.

Step 8: Bitwise XOR operation performed with ?xor_value' and ?num' to compute the new XOR value.

Step 9: Create an input array and set the divisor to 10, call the function ?xor_divisible_elements' with the input array and store the calculated value in the ?result' variable.Check the result.

Code For XOR of array elements that are divisible by a given number

Example

def xor_divisible_elements(arr, divisor):
    xor_value = 0

    for num in arr: # use for Loop 
        if num % divisor == 0: # Check remainder is zero
            xor_value ^= num  # XORing with num to get our result

    return xor_value

# Example 
array = [20,25,30,35,40,50] # input array
divisor = 10     # set divisor value

result = xor_divisible_elements(array, divisor) # Store final value in the result variable
print("XOR of divisible elements:", result) # Printing our result

Output

XOR of divisible elements: 16

Example 2: Python Program to find XOR of array elements that are divisible by given number

Code Explanation and Design Steps

Step 1: Open Jupyter Notebook in the Anaconda prompt and start writing the code in its cell.

Step 2: Use this function ?xor_divisible_elements' which takes two parameters ?arr' as an input array and ?divisor' is a number by which elements must be divisible.

Step 3: Initialize ?xor_value' to 0.

Step 4: The XOR value of the divisible number will be stored in ?xor_value' for all elements.

Step 5: Use the ?for' loop to iterate to each element of ?num' in the ?arr'.

Step 6: In the ?for' loop, using the floor division operator (//), calculate the quotient by dividing num by divisor for an individual element. Check if the quotient is odd by checking quotient % 2 == 1.

Step 7: Update the ?xor_value', performing the XOR operation with ?num' using the XOR assignment operator ?^='.

Step 8: Bitwise XOR operation performed with ?xor_value' and ?num' to compute the new XOR value.

Step 9: Create an input array and set the divisor to 10, call the function ?xor_divisible_elements' with the input array and store the calculated value in the ?result' variable. Check the result.

Code For XOR of array elements which are divisible by a given number: (Alternate way)

Example

def xor_divisible_elements(arr, divisor):
    xor_value = 0

    for num in arr: # use for Loop 
        quotient = num // divisor  # Calculate quotient by dividing num by  
                                     # divisor using ?//' operator

        if quotient % 2 == 1:  # Check for quotient odd
            xor_value ^= num  # XORing with num to get our result

    return xor_value

# Example 
array = [20,25,30,35,40,50] # input array
divisor = 10     # set divisor value

result = xor_divisible_elements(array, divisor) # Store final value in the 
                                                 # result variable
print("XOR of divisible elements:", result) # Print our result

Output

XOR of divisible elements: 15

This code follows method 2 to find the XOR of array elements that are divisible by a given number.

Conclusion

This article takes two methods for calculating the XOR of array elements that are divisible for a given number. Both ways are explained clearly for understanding purposes. Compare the binary value of two operands for the XORing operation, the same bit values will return zero as a result otherwise the result would be 1 for different bit values. We can also modify or change the values of the array and divisor as per our requirements.

Updated on: 2023-08-28T14:16:51+05:30

262 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements