# Alternate XOR operations on sorted array

Given an array **arr[]** and two integers **X** and **K**. The task is to perform the following operation on array **K** times:

- Sort the array.
- XOR every alternate element of the sorted array with
**X**i.e.**arr[0], arr[2], arr[4], …**

After repeating the above steps **K** times, print the maximum and the minimum element in the modified array.

**Examples:**

Input:arr[] = {9, 7, 11, 15, 5}, K = 1, X = 2

Output:7 13

Since the operations has to be performed only once,

the sorted array will be {5, 7, 9, 11, 15}

Now, apply xor with 2 on alternate elements i.e. 5, 9 and 15.

{5 ^ 2, 7, 9 ^ 2, 11, 15 ^ 2} which is equal to

{7, 7, 11, 11, 13}

Input:arr[] = {605, 986}, K = 548, X = 569

Output:605 986

**Approach:** Instead of sorting the array in every iteration, a frequency array can be maintained that will store the frequency of each of the element in the array. Traversing from 1 to maximum element in the array, the elements can be processed in the sorted order and after every operation the frequency of the same elements can be adjusted when the alternate elements get xored with the given integer. Check the programming implementation for more details.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define MAX 100000 ` ` ` `// Function to find the maximum and the ` `// minimum elements from the array after ` `// performing the given operation k times ` `void` `xorOnSortedArray(` `int` `arr[], ` `int` `n, ` `int` `k, ` `int` `x) ` `{ ` ` ` ` ` `// To store the current sequence of elements ` ` ` `int` `arr1[MAX + 1] = { 0 }; ` ` ` ` ` `// To store the next sequence of elements ` ` ` `// after xoring with current elements ` ` ` `int` `arr2[MAX + 1] = { 0 }; ` ` ` `int` `xor_val[MAX + 1]; ` ` ` ` ` `// Store the frequency of elements of arr[] in arr1[] ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `arr1[arr[i]]++; ` ` ` ` ` `// Storing all precomputed XOR values so that ` ` ` `// we don't have to do it again and again ` ` ` `// as XOR is a costly operation ` ` ` `for` `(` `int` `i = 0; i <= MAX; i++) ` ` ` `xor_val[i] = i ^ x; ` ` ` ` ` `// Perform the operations k times ` ` ` `while` `(k--) { ` ` ` ` ` `// The value of count decide on how many elements ` ` ` `// we have to apply XOR operation ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i <= MAX; i++) { ` ` ` `int` `store = arr1[i]; ` ` ` ` ` `// If current element is present in ` ` ` `// the array to be modified ` ` ` `if` `(arr1[i] > 0) { ` ` ` ` ` `// Suppose i = m and arr1[i] = num, it means ` ` ` `// 'm' appears 'num' times ` ` ` `// If the count is even we have to perform ` ` ` `// XOR operation on alternate 'm' starting ` ` ` `// from the 0th index because count is even ` ` ` `// and we have to perform XOR operations ` ` ` `// starting with initial 'm' ` ` ` `// Hence there will be ceil(num/2) operations on ` ` ` `// 'm' that will change 'm' to xor_val[m] i.e. m^x ` ` ` `if` `(count % 2 == 0) { ` ` ` `int` `div` `= ` `ceil` `((` `float` `)arr1[i] / 2); ` ` ` ` ` `// Decrease the frequency of 'm' from arr1[] ` ` ` `arr1[i] = arr1[i] - ` `div` `; ` ` ` ` ` `// Increase the frequency of 'm^x' in arr2[] ` ` ` `arr2[xor_val[i]] += ` `div` `; ` ` ` `} ` ` ` ` ` `// If the count is odd we have to perform ` ` ` `// XOR operation on alternate 'm' starting ` ` ` `// from the 1st index because count is odd ` ` ` `// and we have to leave the 0th 'm' ` ` ` `// Hence there will be (num/2) XOR operations on ` ` ` `// 'm' that will change 'm' to xor_val[m] i.e. m^x ` ` ` `else` `if` `(count % 2 != 0) { ` ` ` `int` `div` `= arr1[i] / 2; ` ` ` `arr1[i] = arr1[i] - ` `div` `; ` ` ` `arr2[xor_val[i]] += ` `div` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Updating the count by frequency of ` ` ` `// the current elements as we have ` ` ` `// processed that many elements ` ` ` `count = count + store; ` ` ` `} ` ` ` ` ` `// Updating arr1[] which will now store the ` ` ` `// next sequence of elements ` ` ` `// At this time, arr1[] stores the remaining ` ` ` `// 'm' on which XOR was not performed and ` ` ` `// arr2[] stores the frequency of 'm^x' i.e. ` ` ` `// those 'm' on which operation was performed ` ` ` `// Updating arr1[] with frequency of remaining ` ` ` `// 'm' & frequency of 'm^x' from arr2[] ` ` ` `// With help of arr2[], we prevent sorting of ` ` ` `// the array again and again ` ` ` `for` `(` `int` `i = 0; i <= MAX; i++) { ` ` ` `arr1[i] = arr1[i] + arr2[i]; ` ` ` ` ` `// Resetting arr2[] for next iteration ` ` ` `arr2[i] = 0; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Finding the maximum and the minimum element ` ` ` `// from the modified array after the operations ` ` ` `int` `min = INT_MAX; ` ` ` `int` `max = INT_MIN; ` ` ` `for` `(` `int` `i = 0; i <= MAX; i++) { ` ` ` `if` `(arr1[i] > 0) { ` ` ` `if` `(min > i) ` ` ` `min = i; ` ` ` `if` `(max < i) ` ` ` `max = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Printing the max and the min element ` ` ` `cout << min << ` `" "` `<< max << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 605, 986 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `k = 548, x = 569; ` ` ` ` ` `xorOnSortedArray(arr, n, k, x); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `static` `int` `MAX = ` `100000` `; ` ` ` ` ` `// Function to find the maximum and the ` ` ` `// minimum elements from the array after ` ` ` `// performing the given operation k times ` ` ` `public` `static` `void` `xorOnSortedArray(` `int` `[] arr, ` `int` `n, ` ` ` `int` `k, ` `int` `x) ` ` ` `{ ` ` ` ` ` `// To store the current sequence of elements ` ` ` `int` `[] arr1 = ` `new` `int` `[MAX + ` `1` `]; ` ` ` ` ` `// To store the next sequence of elements ` ` ` `// after xoring with current elements ` ` ` `int` `[] arr2 = ` `new` `int` `[MAX + ` `1` `]; ` ` ` `int` `[] xor_val = ` `new` `int` `[MAX + ` `1` `]; ` ` ` ` ` `// Store the frequency of elements ` ` ` `// of arr[] in arr1[] ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `arr1[arr[i]]++; ` ` ` ` ` `// Storing all precomputed XOR values so that ` ` ` `// we don't have to do it again and again ` ` ` `// as XOR is a costly operation ` ` ` `for` `(` `int` `i = ` `0` `; i <= MAX; i++) ` ` ` `xor_val[i] = i ^ x; ` ` ` ` ` `// Perform the operations k times ` ` ` `while` `(k-- > ` `0` `) ` ` ` `{ ` ` ` ` ` `// The value of count decide on how many elements ` ` ` `// we have to apply XOR operation ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i <= MAX; i++) ` ` ` `{ ` ` ` `int` `store = arr1[i]; ` ` ` ` ` `// If current element is present in ` ` ` `// the array to be modified ` ` ` `if` `(arr1[i] > ` `0` `) ` ` ` `{ ` ` ` ` ` `// Suppose i = m and arr1[i] = num, it means ` ` ` `// 'm' appears 'num' times ` ` ` `// If the count is even we have to perform ` ` ` `// XOR operation on alternate 'm' starting ` ` ` `// from the 0th index because count is even ` ` ` `// and we have to perform XOR operations ` ` ` `// starting with initial 'm' ` ` ` `// Hence there will be ceil(num/2) operations on ` ` ` `// 'm' that will change 'm' to xor_val[m] i.e. m^x ` ` ` `if` `(count % ` `2` `== ` `0` `) ` ` ` `{ ` ` ` `int` `div = (` `int` `) Math.ceil(arr1[i] / ` `2` `); ` ` ` ` ` `// Decrease the frequency of 'm' from arr1[] ` ` ` `arr1[i] = arr1[i] - div; ` ` ` ` ` `// Increase the frequency of 'm^x' in arr2[] ` ` ` `arr2[xor_val[i]] += div; ` ` ` `} ` ` ` ` ` `// If the count is odd we have to perform ` ` ` `// XOR operation on alternate 'm' starting ` ` ` `// from the 1st index because count is odd ` ` ` `// and we have to leave the 0th 'm' ` ` ` `// Hence there will be (num/2) XOR operations on ` ` ` `// 'm' that will change 'm' to xor_val[m] i.e. m^x ` ` ` `else` `if` `(count % ` `2` `!= ` `0` `) ` ` ` `{ ` ` ` `int` `div = arr1[i] / ` `2` `; ` ` ` `arr1[i] = arr1[i] - div; ` ` ` `arr2[xor_val[i]] += div; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Updating the count by frequency of ` ` ` `// the current elements as we have ` ` ` `// processed that many elements ` ` ` `count = count + store; ` ` ` `} ` ` ` ` ` `// Updating arr1[] which will now store the ` ` ` `// next sequence of elements ` ` ` `// At this time, arr1[] stores the remaining ` ` ` `// 'm' on which XOR was not performed and ` ` ` `// arr2[] stores the frequency of 'm^x' i.e. ` ` ` `// those 'm' on which operation was performed ` ` ` `// Updating arr1[] with frequency of remaining ` ` ` `// 'm' & frequency of 'm^x' from arr2[] ` ` ` `// With help of arr2[], we prevent sorting of ` ` ` `// the array again and again ` ` ` `for` `(` `int` `i = ` `0` `; i <= MAX; i++) ` ` ` `{ ` ` ` `arr1[i] = arr1[i] + arr2[i]; ` ` ` ` ` `// Resetting arr2[] for next iteration ` ` ` `arr2[i] = ` `0` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Finding the maximum and the minimum element ` ` ` `// from the modified array after the operations ` ` ` `int` `min = Integer.MAX_VALUE; ` ` ` `int` `max = Integer.MIN_VALUE; ` ` ` `for` `(` `int` `i = ` `0` `; i <= MAX; i++) ` ` ` `{ ` ` ` `if` `(arr1[i] > ` `0` `) ` ` ` `{ ` ` ` `if` `(min > i) ` ` ` `min = i; ` ` ` `if` `(max < i) ` ` ` `max = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Printing the max and the min element ` ` ` `System.out.println(min + ` `" "` `+ max); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `[] arr = { ` `605` `, ` `986` `}; ` ` ` `int` `n = arr.length; ` ` ` `int` `k = ` `548` `, x = ` `569` `; ` ` ` `xorOnSortedArray(arr, n, k, x); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// sanjeev2552 ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 implementation of the approach ` `MAX` `=` `10000` `import` `sys ` `from` `math ` `import` `ceil, floor ` ` ` `# Function to find the maximum and the ` `# minimum elements from the array after ` `# performing the given operation k times ` `def` `xorOnSortedArray(arr, n, k, x): ` ` ` ` ` `# To store the current sequence of elements ` ` ` `arr1 ` `=` `[` `0` `for` `i ` `in` `range` `(` `MAX` `+` `1` `)] ` ` ` ` ` `# To store the next sequence of elements ` ` ` `# after xoring with current elements ` ` ` `arr2 ` `=` `[` `0` `for` `i ` `in` `range` `(` `MAX` `+` `1` `)] ` ` ` `xor_val ` `=` `[` `0` `for` `i ` `in` `range` `(` `MAX` `+` `1` `)] ` ` ` ` ` `# Store the frequency of elements ` ` ` `# of arr[] in arr1[] ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `arr1[arr[i]] ` `+` `=` `1` ` ` ` ` `# Storing all precomputed XOR values ` ` ` `# so that we don't have to do it ` ` ` `# again and again as XOR is a costly operation ` ` ` `for` `i ` `in` `range` `(` `MAX` `+` `1` `): ` ` ` `xor_val[i] ` `=` `i ^ x ` ` ` ` ` `# Perform the operations k times ` ` ` `while` `(k > ` `0` `): ` ` ` `k ` `-` `=` `1` ` ` ` ` `# The value of count decide on ` ` ` `# how many elements we have to ` ` ` `# apply XOR operation ` ` ` `count ` `=` `0` ` ` `for` `i ` `in` `range` `(` `MAX` `+` `1` `): ` ` ` `store ` `=` `arr1[i] ` ` ` ` ` `# If current element is present in ` ` ` `# the array to be modified ` ` ` `if` `(arr1[i] > ` `0` `): ` ` ` ` ` `# Suppose i = m and arr1[i] = num, ` ` ` `# it means 'm' appears 'num' times ` ` ` `# If the count is even we have to ` ` ` `# perform XOR operation on alternate ` ` ` `# 'm' starting from the 0th index because ` ` ` `# count is even and we have to perform ` ` ` `# XOR operations starting with initial 'm' ` ` ` `# Hence there will be ceil(num/2) ` ` ` `# operations on 'm' that will change ` ` ` `# 'm' to xor_val[m] i.e. m^x ` ` ` `if` `(count ` `%` `2` `=` `=` `0` `): ` ` ` `div ` `=` `arr1[i] ` `/` `/` `2` `+` `1` ` ` ` ` `# Decrease the frequency of ` ` ` `# 'm' from arr1[] ` ` ` `arr1[i] ` `=` `arr1[i] ` `-` `div ` ` ` ` ` `# Increase the frequency of ` ` ` `# 'm^x' in arr2[] ` ` ` `arr2[xor_val[i]] ` `+` `=` `div ` ` ` ` ` `# If the count is odd we have to perform ` ` ` `# XOR operation on alternate 'm' starting ` ` ` `# from the 1st index because count is odd ` ` ` `# and we have to leave the 0th 'm' ` ` ` `# Hence there will be (num/2) XOR operations on ` ` ` `# 'm' that will change 'm' to xor_val[m] i.e. m^x ` ` ` `elif` `(count ` `%` `2` `!` `=` `0` `): ` ` ` `div ` `=` `arr1[i] ` `/` `/` `2` ` ` `arr1[i] ` `=` `arr1[i] ` `-` `div ` ` ` `arr2[xor_val[i]] ` `+` `=` `div ` ` ` ` ` `# Updating the count by frequency of ` ` ` `# the current elements as we have ` ` ` `# processed that many elements ` ` ` `count ` `=` `count ` `+` `store ` ` ` ` ` `# Updating arr1[] which will now store the ` ` ` `# next sequence of elements ` ` ` `# At this time, arr1[] stores the remaining ` ` ` `# 'm' on which XOR was not performed and ` ` ` `# arr2[] stores the frequency of 'm^x' i.e. ` ` ` `# those 'm' on which operation was performed ` ` ` `# Updating arr1[] with frequency of remaining ` ` ` `# 'm' & frequency of 'm^x' from arr2[] ` ` ` `# With help of arr2[], we prevent sorting of ` ` ` `# the array again and again ` ` ` `for` `i ` `in` `range` `(` `MAX` `+` `1` `): ` ` ` `arr1[i] ` `=` `arr1[i] ` `+` `arr2[i] ` ` ` ` ` `# Resetting arr2[] for next iteration ` ` ` `arr2[i] ` `=` `0` ` ` ` ` `# Finding the maximum and the minimum element ` ` ` `# from the modified array after the operations ` ` ` `mn ` `=` `sys.maxsize ` ` ` `mx ` `=` `-` `sys.maxsize` `-` `1` ` ` `for` `i ` `in` `range` `(` `MAX` `+` `1` `): ` ` ` `if` `(arr1[i] > ` `0` `): ` ` ` `if` `(mn > i): ` ` ` `mn ` `=` `i ` ` ` `if` `(mx < i): ` ` ` `mx ` `=` `i ` ` ` ` ` `# Printing the max and the min element ` ` ` `print` `(mn,mx) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr ` `=` `[` `605` `, ` `986` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` `k ` `=` `548` ` ` `x ` `=` `569` ` ` ` ` `xorOnSortedArray(arr, n, k, x) ` ` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `MAX = 100000; ` ` ` ` ` `// Function to find the maximum and the ` ` ` `// minimum elements from the array after ` ` ` `// performing the given operation k times ` ` ` `public` `static` `void` `xorOnSortedArray(` `int` `[] arr, ` `int` `n, ` ` ` `int` `k, ` `int` `x) ` ` ` `{ ` ` ` ` ` `// To store the current sequence of elements ` ` ` `int` `[] arr1 = ` `new` `int` `[MAX + 1]; ` ` ` ` ` `// To store the next sequence of elements ` ` ` `// after xoring with current elements ` ` ` `int` `[] arr2 = ` `new` `int` `[MAX + 1]; ` ` ` `int` `[] xor_val = ` `new` `int` `[MAX + 1]; ` ` ` ` ` `// Store the frequency of elements ` ` ` `// of arr[] in arr1[] ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `arr1[arr[i]]++; ` ` ` ` ` `// Storing all precomputed XOR values so that ` ` ` `// we don't have to do it again and again ` ` ` `// as XOR is a costly operation ` ` ` `for` `(` `int` `i = 0; i <= MAX; i++) ` ` ` `xor_val[i] = i ^ x; ` ` ` ` ` `// Perform the operations k times ` ` ` `while` `(k-- > 0) ` ` ` `{ ` ` ` ` ` `// The value of count decides on ` ` ` `// how many elements we have to ` ` ` `// apply XOR operation ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i <= MAX; i++) ` ` ` `{ ` ` ` `int` `store = arr1[i]; ` ` ` ` ` `// If current element is present in ` ` ` `// the array to be modified ` ` ` `if` `(arr1[i] > 0) ` ` ` `{ ` ` ` ` ` `// Suppose i = m and arr1[i] = num, ` ` ` `// it means 'm' appears 'num' times ` ` ` `// If the count is even we have to perform ` ` ` `// XOR operation on alternate 'm' starting ` ` ` `// from the 0th index because count is even ` ` ` `// and we have to perform XOR operations ` ` ` `// starting with initial 'm' ` ` ` `// Hence there will be ceil(num/2) operations on ` ` ` `// 'm' that will change 'm' to xor_val[m] i.e. m^x ` ` ` `if` `(count % 2 == 0) ` ` ` `{ ` ` ` `int` `div = (` `int` `) Math.Ceiling((` `double` `)(arr1[i] / 2)); ` ` ` ` ` `// Decrease the frequency of 'm' from arr1[] ` ` ` `arr1[i] = arr1[i] - div; ` ` ` ` ` `// Increase the frequency of 'm^x' in arr2[] ` ` ` `arr2[xor_val[i]] += div; ` ` ` `} ` ` ` ` ` `// If the count is odd we have to perform ` ` ` `// XOR operation on alternate 'm' starting ` ` ` `// from the 1st index because count is odd ` ` ` `// and we have to leave the 0th 'm' ` ` ` `// Hence there will be (num/2) XOR operations on ` ` ` `// 'm' that will change 'm' to xor_val[m] i.e. m^x ` ` ` `else` `if` `(count % 2 != 0) ` ` ` `{ ` ` ` `int` `div = arr1[i] / 2; ` ` ` `arr1[i] = arr1[i] - div; ` ` ` `arr2[xor_val[i]] += div; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Updating the count by frequency of ` ` ` `// the current elements as we have ` ` ` `// processed that many elements ` ` ` `count = count + store; ` ` ` `} ` ` ` ` ` `// Updating arr1[] which will now store the ` ` ` `// next sequence of elements ` ` ` `// At this time, arr1[] stores the remaining ` ` ` `// 'm' on which XOR was not performed and ` ` ` `// arr2[] stores the frequency of 'm^x' i.e. ` ` ` `// those 'm' on which operation was performed ` ` ` `// Updating arr1[] with frequency of remaining ` ` ` `// 'm' & frequency of 'm^x' from arr2[] ` ` ` `// With help of arr2[], we prevent sorting of ` ` ` `// the array again and again ` ` ` `for` `(` `int` `i = 0; i <= MAX; i++) ` ` ` `{ ` ` ` `arr1[i] = arr1[i] + arr2[i]; ` ` ` ` ` `// Resetting arr2[] for next iteration ` ` ` `arr2[i] = 0; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Finding the maximum and the minimum element ` ` ` `// from the modified array after the operations ` ` ` `int` `min = ` `int` `.MaxValue; ` ` ` `int` `max = ` `int` `.MinValue; ` ` ` `for` `(` `int` `i = 0; i <= MAX; i++) ` ` ` `{ ` ` ` `if` `(arr1[i] > 0) ` ` ` `{ ` ` ` `if` `(min > i) ` ` ` `min = i; ` ` ` `if` `(max < i) ` ` ` `max = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Printing the max and the min element ` ` ` `Console.WriteLine(min + ` `" "` `+ max); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `int` `[] arr = { 605, 986 }; ` ` ` `int` `n = arr.Length; ` ` ` `int` `k = 548, x = 569; ` ` ` `xorOnSortedArray(arr, n, k, x); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

605 986

## Recommended Posts:

- Minimum increment or decrement operations required to make the array sorted
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Sort a nearly sorted (or K sorted) array
- Lexicographical smallest alternate Array
- Print modified array after multiple array range increment operations
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Sort an array where a subarray of a sorted array is in reverse order
- Why is it faster to process sorted array than an unsorted array ?
- Check if reversing a sub array make the array sorted
- Maximum in an array that can make another array sorted
- Check if array sum can be made K by three operations on it
- Minimum operations to make XOR of array zero
- Maximum Possible Product in Array after performing given Operations
- Minimum operations required to remove an array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.