Python program to find XOR of array elements which are divisible by given number

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 returns 1, whereas it returns 0 if the bits are the same.

Understanding XOR Operation

Let's understand XOR with a simple example using array [1, 2, 3, 4, 5] ?

  • 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 4. Update xor_value = 4.

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

  • The final xor_value is 1.

Method 1: Using Modulo Operator

Check if each element is divisible by the given number using the modulo operator (%) ?

def xor_divisible_elements(arr, divisor):
    xor_value = 0
    
    for num in arr:
        if num % divisor == 0:  # Check if remainder is zero
            xor_value ^= num    # XOR with the divisible element
    
    return xor_value

# Example usage
array = [20, 25, 30, 35, 40, 50]
divisor = 10

result = xor_divisible_elements(array, divisor)
print("XOR of elements divisible by", divisor, ":", result)

# Show which elements are divisible
divisible_elements = [num for num in array if num % divisor == 0]
print("Divisible elements:", divisible_elements)
XOR of elements divisible by 10 : 16
Divisible elements: [20, 30, 40, 50]

Method 2: Using Floor Division

An alternative approach using floor division to check divisibility ?

def xor_divisible_elements_alt(arr, divisor):
    xor_value = 0
    
    for num in arr:
        quotient = num // divisor
        if quotient * divisor == num:  # Check if perfectly divisible
            xor_value ^= num
    
    return xor_value

# Example usage
array = [15, 20, 25, 30, 35, 40]
divisor = 5

result = xor_divisible_elements_alt(array, divisor)
print("XOR of elements divisible by", divisor, ":", result)

# Show the XOR calculation step by step
divisible_elements = [num for num in array if num % divisor == 0]
print("Divisible elements:", divisible_elements)

xor_step = 0
for elem in divisible_elements:
    xor_step ^= elem
    print(f"After XOR with {elem}: {xor_step}")
XOR of elements divisible by 5 : 10
Divisible elements: [15, 20, 25, 30, 35, 40]
After XOR with 15: 15
After XOR with 20: 3
After XOR with 25: 22
After XOR with 30: 8
After XOR with 35: 35
After XOR with 40: 10

Comparison

Method Approach Time Complexity Best For
Modulo Operator Check remainder O(n) Simple and readable
Floor Division Check perfect division O(n) Alternative validation

Conclusion

Both methods efficiently find the XOR of array elements divisible by a given number. The modulo operator approach is more straightforward and commonly used for divisibility checks.

Updated on: 2026-03-27T13:49:04+05:30

415 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements