You are given an array arr[], and you have to re-construct an array arr[].
The values in arr[] are obtained by doing Xor of consecutive elements in the array.
Example 1:
Input : n=5, arr[ ] = {10, 11, 1, 2, 3}
Output : 1 10 3 1 3
Explanation:
At index 0, arr[0] xor arr[1] = 1
At index 1, arr[1] xor arr[2] = 10
At index 2, arr[2] xor arr[3] = 3
...
At index 4, No element is left So, it will remain as
it is.
New Array will be {1, 10, 3, 1, 3}.
Example 2:
Input : n=4, arr[ ] = {5, 9, 7, 6}
Output :  12 14 1 6
Explanation:
At index 0, arr[0] xor arr[1] = 12
At index 1, arr[1] xor arr[2] = 14
At index 2, arr[2] xor arr[3] = 1
At index 3, No element is left So, it will remain as it is.
New Array will be {12, 14, 1, 6}.
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function game_with_number() that takes an array (arr), sizeOfArray (n) and return the array re-constructed array arr. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ arr[i] ≤ 10^{7}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3



def game_with_number (arr,  n) : 

    #Complete the function

Solutions:
def game_with_number(arr, n):
	total = []
	for i in range(n - 1):
		tot_val = arr[i] ^ arr[i + 1]
		total.append(tot_val)
	total.append(arr[n - 1])
	return total


def game_with_number(arr, n):
	arr1 = arr
	for i in range(n - 1):
		arr1[i] = arr1[i] ^ arr1[i + 1]
	return arr1


def game_with_number(arr, n):
	for i in range(n - 1):
		arr.append(arr[0] ^ arr[1])
		arr.pop(0)
	arr.append(arr.pop(0))
	return arr


def game_with_number(arr, n):
	for i in range(len(arr) - 1):
		arr[i] ^= arr[i + 1]
	return arr


def game_with_number(arr, n):
	result = []
	for i in range(n):
		if i + 1 < n:
			element = arr[i] ^ arr[i + 1]
			result.append(element)
	if n < 2:
		result.append(arr[0])
	else:
		result.append(arr[n - 1])
	return result


def game_with_number(arr, n):
	l = []
	for i in range(len(arr) - 1):
		l.append(arr[i] ^ arr[i + 1])
	l.append(arr[-1])
	return l


def game_with_number(arr, n):
	new = [0] * n
	for i in range(n - 1):
		new[i] = arr[i] ^ arr[i + 1]
	new[n - 1] = arr[n - 1]
	return new


def game_with_number(arr, n):
	for i in range(n):
		if i == n - 1:
			pass
		else:
			arr[i] = arr[i] ^ arr[i + 1]
	return arr


def game_with_number(arr, n):
	res = []
	for i in range(n - 1):
		res.append(arr[i] ^ arr[i + 1])
	res.append(arr[-1])
	return res


def game_with_number(arr, n):
	s = []
	for i in range(n - 1):
		s.append(arr[i] ^ arr[i + 1])
	s.append(arr[-1])
	return s


def game_with_number(arr, n):
	arr1 = []
	for i in range(n - 1):
		x = arr[i] ^ arr[i + 1]
		arr1.append(x)
	arr1.append(arr[-1])
	return arr1


def game_with_number(arr, n):
	j = 1
	list_xor = []
	for i in range(0, n - 1):
		list_xor.append(arr[i] ^ arr[j])
		j += 1
	return list_xor + [arr[-1]]


def game_with_number(arr, n):
	y = []
	for i in range(0, len(arr) - 1):
		x = arr[i] ^ arr[i + 1]
		y.append(x)
	y.append(arr[len(arr) - 1])
	return y


def game_with_number(arr, n):
	i = 0
	for i in range(n):
		if i != n - 1:
			arr[i] = arr[i] ^ arr[i + 1]
	return arr


def game_with_number(arr, n):
	res = [0] * n
	res[n - 1] = arr[n - 1]
	for i in range(n - 1):
		res[i] = arr[i] ^ arr[i + 1]
	return res


def game_with_number(arr, n):
	b = []
	for i in range(n - 1):
		b.append(arr[i] ^ arr[i + 1])
	b.append(arr[n - 1])
	return b


def game_with_number(arr, n):
	i = 0
	while i + 1 < n:
		arr[i] = arr[i] ^ arr[i + 1]
		i += 1
	return arr


def game_with_number(arr, n):
	a = []
	for i in range(n - 1):
		a.append(arr[i] ^ arr[i + 1])
	a.append(arr[n - 1])
	return a


def game_with_number(arr, n):
	for ans in range(n - 1):
		arr[ans] ^= arr[ans + 1]
	return arr


def game_with_number(l, n):
	a = int(l[-1])
	r = []
	for i in range(len(l) - 1):
		b = l[i] ^ l[i + 1]
		r.append(b)
	r.append(a)
	return r


def game_with_number(arr, n):
	res = [arr[index] ^ arr[index + 1] for index in range(n - 1)]
	res.append(arr[-1])
	return res


def game_with_number(arr, n):
	answer_array = arr
	for i in range(n - 1):
		answer_array[i] = arr[i] ^ arr[i + 1]
	return answer_array


def game_with_number(arr, n):
	li = []
	for i in range(n - 1):
		li.append(arr[i] ^ arr[i + 1])
	li.append(arr[-1])
	return li


def game_with_number(arr, n):
	l = [arr[n - 1] for _ in range(n)]
	for i in range(n - 1):
		l[i] = arr[i] ^ arr[i + 1]
	return l


def game_with_number(arr, n):
	ans = []
	for i in range(n - 1):
		ele = arr[i] ^ arr[i + 1]
		ans.append(ele)
	ans.append(arr.pop())
	return ans


def game_with_number(arr, n):
	for i in range(n - 1):
		temp = arr[i] ^ arr[i + 1]
		arr[i] = temp
	return arr


def game_with_number(arr, n):
	arr2 = []
	for i in range(1, len(arr)):
		arr2.append(arr[i])
	arr2.append(arr[len(arr) - 1])
	for i in range(0, len(arr) - 1):
		x = arr2[i] ^ arr[i]
		arr.pop(i)
		arr.insert(i, x)
	return arr


def game_with_number(arr, n):
	i = 0
	ans = []
	while i + 1 < n:
		ans.append(arr[i] ^ arr[i + 1])
		i += 1
	ans.append(arr[i])
	return ans


def game_with_number(arr, n):
	l = [arr[i] ^ arr[i + 1] for i in range(0, n - 1)]
	l += [arr[-1]]
	return l


def game_with_number(arr, n):
	count = 0
	while count < n - 1:
		arr[count] ^= arr[count + 1]
		count += 1
	return arr


def game_with_number(arr, n):
	arrf = []
	for i in range(n - 1):
		arrf.append(arr[i] ^ arr[i + 1])
	arrf.append(arr[-1])
	return arrf


def game_with_number(arr, n):
	l = []
	for i in range(n):
		if i != n - 1:
			l.append(arr[i] ^ arr[i + 1])
		else:
			l.append(arr[i])
	return l


def game_with_number(arr, n):
	ans = []
	for i in range(n - 1):
		temp = arr[i] ^ arr[i + 1]
		ans.append(temp)
	ans.append(arr[n - 1])
	return ans


def game_with_number(arr, n):
	e = []
	for i in range(n - 1):
		e.append(arr[i] ^ arr[i + 1])
	e.append(arr[n - 1])
	arr.clear()
	arr.extend(e)
	return arr