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 ; Driver code
MAX = 10000 NEW_LINE import sys NEW_LINE from math import ceil , floor NEW_LINE def xorOnSortedArray ( arr , n , k , x ) : NEW_LINE INDENT arr1 = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE arr2 = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE xor_val = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr1 [ arr [ i ] ] += 1 NEW_LINE DEDENT for i in range ( MAX + 1 ) : NEW_LINE INDENT xor_val [ i ] = i ^ x NEW_LINE DEDENT while ( k > 0 ) : NEW_LINE INDENT k -= 1 NEW_LINE count = 0 NEW_LINE for i in range ( MAX + 1 ) : NEW_LINE INDENT store = arr1 [ i ] NEW_LINE if ( arr1 [ i ] > 0 ) : NEW_LINE INDENT if ( count % 2 == 0 ) : NEW_LINE INDENT div = arr1 [ i ] // 2 + 1 NEW_LINE arr1 [ i ] = arr1 [ i ] - div NEW_LINE arr2 [ xor_val [ i ] ] += div NEW_LINE DEDENT elif ( count % 2 != 0 ) : NEW_LINE INDENT div = arr1 [ i ] // 2 NEW_LINE arr1 [ i ] = arr1 [ i ] - div NEW_LINE arr2 [ xor_val [ i ] ] += div NEW_LINE DEDENT DEDENT count = count + store NEW_LINE DEDENT for i in range ( MAX + 1 ) : NEW_LINE INDENT arr1 [ i ] = arr1 [ i ] + arr2 [ i ] NEW_LINE arr2 [ i ] = 0 NEW_LINE DEDENT DEDENT mn = sys . maxsize NEW_LINE mx = - sys . maxsize - 1 NEW_LINE for i in range ( MAX + 1 ) : NEW_LINE INDENT if ( arr1 [ i ] > 0 ) : NEW_LINE INDENT if ( mn > i ) : NEW_LINE INDENT mn = i NEW_LINE DEDENT if ( mx < i ) : NEW_LINE INDENT mx = i NEW_LINE DEDENT DEDENT DEDENT print ( mn , mx ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 605 , 986 ] NEW_LINE n = len ( arr ) NEW_LINE k = 548 NEW_LINE x = 569 NEW_LINE xorOnSortedArray ( arr , n , k , x ) NEW_LINE DEDENT