VOOZH about

URL: https://www.geeksforgeeks.org/dsa/prefix-xor-array/

⇱ Prefix Xor Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Prefix Xor Array

Last Updated : 24 Jul, 2025

Given an array arr[] of size N, find the Prefix Xor of the array. A prefix xor array is another array prefixXor[] of the same size, such that the value of prefixXor[i] is arr[0] ^ arr[1] ^ arr[2] . . . arr[i].

Examples:

Input: arr[] = {10, 20, 10, 5, 15}
Output:prefixXor[] = {10, 30, 20, 17, 30 }
Explanation: While traversing the array, update the element by xoring it with its previous element.
prefixXor[0] = 10, 
prefixXor[1] = prefixXor[0] ^ arr[1] = 30, 
prefixXor[2] = prefixXor[1] ^ arr[2] = 20 and so on.

Input: arr[]={1,2,1,2,5}
Output: prefixXor[] = {1, 3, 2, 0, 5 }

Approach: To solve the problem follow the given steps:

  • Declare a new array prefixXor[] of the same size as the input array
  • Run a for loop to traverse the input array
  • For each index add the value of the current element and the previous value of the prefix sum array

Below is the implementation of the approach:


Output
Given Array: 10 20 10 5 15 
Prefix Xor: 10 30 20 17 30 

Time Complexity: O(N), where N is the size of input array
Auxiliary Space: O(N)

Problem

Practice Link

Find XOR of numbers from the range [L, R]

solve

XOR of a given range

solve

XOR of all elements

solve

XOR counts of 0s and 1s in binary representation

solve

Count number of subsets having a particular XOR value

solve

Find number of pairs in an array such that their XOR is 0

solve

Game of xor

solve

Maximum XOR of Two Numbers in an Array

solve

Find the maximum subset XOR of a given set

solve


Comment
Article Tags: