
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to perform XOR operation in an array using Python
Suppose we have an integer n and another integer start. We have to create an array called nums where nums[i] = start + 2*i (i start 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.
To solve this, we will follow these steps −
count := start
while n-1 > 0, do
count := count XOR 2 + start
n := n - 1
start := start + 2
return count
Example (Python)
Let us see the following implementation to get better understanding −
def solve(n, start): count = start while n-1 > 0: count ^= 2 + start n -= 1 start += 2 return count n = 6 start = 2 print(solve(n, start))
Input
6, 2
Output
14
- Related Articles
- Java Program to perform XOR operation on BigInteger
- How to perform bitwise XOR operation on images in OpenCV Python?
- How to perform Bitwise XOR operation on two images using Java OpenCV?
- Program to perform excel spreadsheet operation in Python?
- Java Menu Driven Program to Perform Array Operation
- Tuple XOR operation in Python
- How to perform bilateral filter operation on an image in OpenCV using Python?
- C++ Program to Perform Addition Operation Using Bitwise Operators
- Program to find maximum XOR with an element from array in Python
- Java Program to perform an XOR on a set of Booleans
- Query in MongoDB to perform an operation similar to LIKE operation
- How to perform an expand operation in PyTorch?
- OpenCV Python – How to perform SQRBox filter operation on an image?
- OpenCV Python – How to perform bitwise NOT operation on an image?
- Java Program to perform AND operation on BigInteger

Advertisements