VOOZH about

URL: https://www.geeksforgeeks.org/dsa/queries-xor-greatest-odd-divisor-range/

⇱ Queries on XOR of greatest odd divisor of the range - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Queries on XOR of greatest odd divisor of the range

Last Updated : 15 Jul, 2022

Given an array of N positive integers. There are Q queries, each include a range [L, R]. For each query output the xor of greatest odd divisor of each number in that range.

Examples: 

Input : arr[] = { 3, 4, 5 }
query 1: [0, 2]
query 2: [1, 2]
Output : 7 4
Greatest odd divisor are: { 3, 1, 5 }
XOR of 3, 1, 5 is 7
XOR of 1, 5 is 4

Input : arr[] = { 2, 1, 2 }
query 1: [0, 2]
Output : 1

The idea is to precompute the greatest odd divisor of the array and store it in an array, say preXOR[]. Now, precompute and store prefix XOR of the array preXOR[]. To answer each query, return (preXOR[r] xor preXOR[l-1]).

Below is the implementation of this approach: 


Output
7
4

Time Complexity: O(n*log(n))
Auxiliary Space: O(n)

Comment