Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to perform XOR operation in an array using Python
Suppose we have an integer n and another integer start. We need to create an array called nums where nums[i] = start + 2*i (where i starts from 0) and n is the size of nums. Then find the bitwise XOR of all elements of nums.
So, if the input is like n = 6, start = 2, then the output will be 14 because the array will be like [2+2*0, 2+2*1, ... 2+2*5] = [2,4,6,8,10,12], then XOR of each element present in the array is 14.
Understanding the Problem
Let's first understand what array we're creating and how XOR works ?
# Create the array first to understand the pattern
n = 6
start = 2
nums = []
for i in range(n):
nums.append(start + 2*i)
print("Array:", nums)
# Calculate XOR step by step
result = nums[0]
print(f"Starting with: {result}")
for i in range(1, len(nums)):
result ^= nums[i]
print(f"After XOR with {nums[i]}: {result}")
print(f"Final XOR result: {result}")
Array: [2, 4, 6, 8, 10, 12] Starting with: 2 After XOR with 4: 6 After XOR with 6: 0 After XOR with 8: 8 After XOR with 10: 2 After XOR with 12: 14 Final XOR result: 14
Method 1: Direct Array Creation and XOR
The straightforward approach is to create the array and then XOR all elements ?
def solve_direct(n, start):
# Create the array
nums = [start + 2*i for i in range(n)]
# XOR all elements
result = 0
for num in nums:
result ^= num
return result
n = 6
start = 2
print(f"XOR result: {solve_direct(n, start)}")
XOR result: 14
Method 2: Optimized Without Array Creation
We can calculate the XOR directly without creating the array, saving memory ?
def solve_optimized(n, start):
result = 0
for i in range(n):
result ^= (start + 2*i)
return result
n = 6
start = 2
print(f"XOR result: {solve_optimized(n, start)}")
XOR result: 14
Method 3: Using Built-in Functions
Python's reduce function with XOR operator provides a concise solution ?
from functools import reduce
import operator
def solve_builtin(n, start):
nums = [start + 2*i for i in range(n)]
return reduce(operator.xor, nums, 0)
n = 6
start = 2
print(f"XOR result: {solve_builtin(n, start)}")
XOR result: 14
Testing with Different Inputs
def solve(n, start):
result = 0
for i in range(n):
result ^= (start + 2*i)
return result
# Test cases
test_cases = [(6, 2), (4, 3), (5, 0), (1, 10)]
for n, start in test_cases:
array = [start + 2*i for i in range(n)]
result = solve(n, start)
print(f"n={n}, start={start}: Array={array}, XOR={result}")
n=6, start=2: Array=[2, 4, 6, 8, 10, 12], XOR=14 n=4, start=3: Array=[3, 5, 7, 9], XOR=8 n=5, start=0: Array=[0, 2, 4, 6, 8], XOR=2 n=1, start=10: Array=[10], XOR=10
Comparison
| Method | Space Complexity | Time Complexity | Best For |
|---|---|---|---|
| Direct Array | O(n) | O(n) | When you need the array later |
| Optimized Loop | O(1) | O(n) | Memory-efficient solution |
| Built-in Functions | O(n) | O(n) | Functional programming style |
Conclusion
The optimized approach without array creation is most efficient with O(1) space complexity. XOR operation follows the pattern where each element is calculated as start + 2*i and combined using bitwise XOR.
