# alternate xor operations on sorted array | python 3 implementation of the approach ; function to find the maximum and the minimum elements from the array after performing the given operation k times ; to store the current sequence of elements ; to store the next sequence of elements after xoring with current elements ; store the frequency of elements of arr [ ] in arr1 [ ] ; storing all precomputed xor values so that we don 't have to do it  again and again as xor is a costly operation ; perform the operations k times ; the value of count decide on how many elements we have to apply xor operation ; if current element is present in the array to be modified ; 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 0 th 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 ; decrease the frequency of ' m ' from arr1 [ ] ; increase the frequency of ' m ^ x ' in arr2 [ ] ; if the count is odd we have to perform xor operation on alternate ' m ' starting from the 1 st index because count is odd and we have to leave the 0 th ' m ' hence there will be ( num / 2 ) xor operations on ' m ' that will change ' m ' to xor_val [ m ] i . e . m ^ x ; updating the count by frequency of the current elements as we have processed that many elements ; 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 ; resetting arr2 [ ] for next iteration ; finding the maximum and the minimum element from the modified array after the operations ; printing the max and the min element ; 

from math import ceil, floor
import sys
MAX = 10000


def xorOnSortedArray(arr, n, k, x):
    arr1 = [0 for i in range(MAX + 1)]
    arr2 = [0 for i in range(MAX + 1)]
    xor_val = [0 for i in range(MAX + 1)]
    for i in range(n):
        arr1[arr[i]] += 1
    for i in range(MAX + 1):
        xor_val[i] = i ^ x
    while (k > 0):
        k -= 1
        count = 0
        for i in range(MAX + 1):
            store = arr1[i]
            if (arr1[i] > 0):
                if (count % 2 == 0):
                    div = arr1[i] // 2 + 1
                    arr1[i] = arr1[i] - div
                    arr2[xor_val[i]] += div
                elif (count % 2 != 0):
                    div = arr1[i] // 2
                    arr1[i] = arr1[i] - div
                    arr2[xor_val[i]] += div
            count = count + store
        for i in range(MAX + 1):
            arr1[i] = arr1[i] + arr2[i]
            arr2[i] = 0
    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
    print(mn, mx)


if __name__ == '__main__':
    arr = [605, 986]
    n = len(arr)
    k = 548
    x = 569
    xorOnSortedArray(arr, n, k, x)

# Expected output:
605 986