Given an array **arr[] **of size** N **and two integers **M** and **K**, the task is to find the array element at the **K ^{th}** index after performing following

**M**operations on the given array.

In a single operation, a new array is formed whose elements have the Bitwise XOR values of the adjacent elements of the current array.

If the number of operations **M **or the value of **K** after **M** operations is invalid then print **-1**.

**Examples:**

Input:arr[] = {1, 4, 5, 6, 7}, M = 1, K = 2Output:3Explanation:

Since M = 1, therefore, the operation has to be performed only once on the array.

The array is modified to {1^4, 4^5, 5^6, 6^7} = {5, 1, 3, 1}.

The value of the element at index K = 2 in the updated array is 3.

Input:arr[] = {3, 2, 6}, M = 2, K = 1Output:-1Explanation:

Since M = 2, operation has be performed twice.

On performing 1st operation on the array {3, 2, 6} it is modified as {3^2, 2^6} = {1, 4}.

On performing 2nd operation on the array {1, 4}, it is modified as {1^4} = {5}.

After performing 2 operations, the size of the array is reduced to 1. This implies that the index K = 1 doesn’t exist after 2 operations.

Hence, the given input is invalid. So the output is -1.

**Approach:** The idea is to generate the array after every operation and check-in each iteration whether it is possible to do the operation further or not. If it is not possible, then return **“-1”**. Below are the steps:

- Check if the given number of operations can be performed on the array or not.
- After every iteration, the length of the array is reduced by 1 which means if
**M**is greater than or equal to**N**, then it is not possible to perform the operation. Hence, print**“-1”**. - If the value of
**M**is valid then check if the given index**K**is valid or not. - After
**M**operations,**(N – M)**elements are left in the array. So, if the index value is greater than or equal to the value of**(N – M)**, then print**“-1”**. - If both
**M**and**K**values are valid, then iterate a loop**M**times and do the following:- In each iteration, create a temporary array
**temp[]**which stores the values obtained by performing operation on the adjacent values of the original array. - Traverse the array
**arr[]**and calculate the Bitwise XOR values of adjacent elements and save these calculated values in the temporary array**temp[]**. - Update the current array
**arr[]**with**temp[]**.

- In each iteration, create a temporary array
- After the
**M**iterations, return the value of**arr[K]**, which is the required output.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include<bits/stdc++.h>` `using` `namespace` `std;` `// Method that returns the` `// corresponding output by ` `// taking the given inputs.` `int` `xor_operations(` `int` `N, ` `int` `arr[], ` ` ` `int` `M, ` `int` `K)` `{` ` ` ` ` `// If this condition is satisfied,` ` ` `// value of M is invalid` ` ` `if` `(M < 0 or M >= N)` ` ` `return` `-1;` ` ` ` ` `// Check if index K is valid` ` ` `if` `(K < 0 or K >= N - M)` ` ` `return` `-1;` ` ` ` ` `// Loop to perform M operations` ` ` `for` `(` `int` `p = 0; p < M; p++)` ` ` `{` ` ` ` ` `// Creating a temporary list` ` ` `vector<` `int` `>temp;` ` ` ` ` `// Traversing the array` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `{` ` ` ` ` `// Calculate XOR values ` ` ` `// of adjacent elements` ` ` `int` `value = arr[i] ^ arr[i + 1];` ` ` ` ` `// Adding this value to ` ` ` `// the temporary list` ` ` `temp.push_back(value);` ` ` ` ` `// Update the original array` ` ` `arr[i] = temp[i];` ` ` `}` ` ` `}` ` ` ` ` `// Getting value at index K` ` ` `int` `ans = arr[K];` ` ` ` ` `return` `ans;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Number of elements` ` ` `int` `N = 5;` ` ` ` ` `// Given array arr[]` ` ` `int` `arr[] = {1, 4, 5, 6, 7};` ` ` `int` `M = 1, K = 2;` ` ` ` ` `// Function call` ` ` `cout << xor_operations(N, arr, M, K);` ` ` `return` `0;` `}` `// This code is contributed by gauravrajput1` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG{` `// Method that returns the` `// corresponding output by ` `// taking the given inputs.` `static` `int` `xor_operations(` `int` `N, ` `int` `arr[], ` ` ` `int` `M, ` `int` `K)` `{` ` ` ` ` `// If this condition is satisfied,` ` ` `// value of M is invalid` ` ` `if` `(M < ` `0` `|| M >= N)` ` ` `return` `-` `1` `;` ` ` ` ` `// Check if index K is valid` ` ` `if` `(K < ` `0` `|| K >= N - M)` ` ` `return` `-` `1` `;` ` ` ` ` `// Loop to perform M operations` ` ` `for` `(` `int` `p = ` `0` `; p < M; p++)` ` ` `{` ` ` ` ` `// Creating a temporary list` ` ` `Vector<Integer>temp = ` `new` `Vector<Integer>();` ` ` ` ` `// Traversing the array` ` ` `for` `(` `int` `i = ` `0` `; i < N - ` `1` `; i++)` ` ` `{` ` ` ` ` `// Calculate XOR values ` ` ` `// of adjacent elements` ` ` `int` `value = arr[i] ^ arr[i + ` `1` `];` ` ` ` ` `// Adding this value to ` ` ` `// the temporary list` ` ` `temp.add(value);` ` ` ` ` `// Update the original array` ` ` `arr[i] = temp.get(i);` ` ` `}` ` ` `}` ` ` ` ` `// Getting value at index K` ` ` `int` `ans = arr[K];` ` ` ` ` `return` `ans;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` ` ` `// Number of elements` ` ` `int` `N = ` `5` `;` ` ` ` ` `// Given array arr[]` ` ` `int` `arr[] = { ` `1` `, ` `4` `, ` `5` `, ` `6` `, ` `7` `};` ` ` `int` `M = ` `1` `, K = ` `2` `;` ` ` ` ` `// Function call` ` ` `System.out.print(xor_operations(N, arr, M, K));` `}` `}` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `# Method that returns the` `# corresponding output by ` `# taking the given inputs.` `def` `xor_operations(N, arr, M, K):` ` ` ` ` `# If this condition is satisfied,` ` ` `# value of M is invalid` ` ` `if` `M < ` `0` `or` `M >` `=` `N:` ` ` `return` `-` `1` ` ` ` ` `# Check if index K is valid` ` ` `if` `K < ` `0` `or` `K >` `=` `N` `-` `M:` ` ` `return` `-` `1` ` ` ` ` `# Loop to perform M operations` ` ` `for` `_ ` `in` `range` `(M):` ` ` ` ` `# Creating a temporary list` ` ` `temp ` `=` `[]` ` ` ` ` `# Traversing the array` ` ` `for` `i ` `in` `range` `(` `len` `(arr)` `-` `1` `):` ` ` ` ` `# Calculate XOR values ` ` ` `# of adjacent elements` ` ` `value ` `=` `arr[i]^arr[i ` `+` `1` `]` ` ` ` ` `# Adding this value to ` ` ` `# the temporary list` ` ` `temp.append(value)` ` ` ` ` `# Update the original array` ` ` `arr ` `=` `temp[:]` ` ` ` ` `# Getting value at index K` ` ` `ans ` `=` `arr[K]` ` ` `return` `ans` `# Driver Code` `# Number of elements` `N ` `=` `5` `# Given array arr[]` `arr ` `=` `[` `1` `, ` `4` `, ` `5` `, ` `6` `, ` `7` `]` `M ` `=` `1` `K ` `=` `2` `# Function Call` `print` `(xor_operations(N, arr, M, K))` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG{` `// Method that returns the` `// corresponding output by ` `// taking the given inputs.` `static` `int` `xor_operations(` `int` `N, ` `int` `[]arr, ` ` ` `int` `M, ` `int` `K)` `{` ` ` `// If this condition is satisfied,` ` ` `// value of M is invalid` ` ` `if` `(M < 0 || M >= N)` ` ` `return` `-1;` ` ` `// Check if index K is valid` ` ` `if` `(K < 0 || K >= N - M)` ` ` `return` `-1;` ` ` `// Loop to perform M operations` ` ` `for` `(` `int` `p = 0; p < M; p++)` ` ` `{` ` ` `// Creating a temporary list` ` ` `List<` `int` `>temp = ` `new` `List<` `int` `>();` ` ` `// Traversing the array` ` ` `for` `(` `int` `i = 0; i < N - 1; i++)` ` ` `{` ` ` `// Calculate XOR values ` ` ` `// of adjacent elements` ` ` `int` `value = arr[i] ^ arr[i + 1];` ` ` `// Adding this value to ` ` ` `// the temporary list` ` ` `temp.Add(value);` ` ` `// Update the original array` ` ` `arr[i] = temp[i];` ` ` `}` ` ` `}` ` ` `// Getting value at index K` ` ` `int` `ans = arr[K];` ` ` `return` `ans;` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `// Number of elements` ` ` `int` `N = 5;` ` ` `// Given array []arr` ` ` `int` `[]arr = {1, 4, 5, 6, 7};` ` ` `int` `M = 1, K = 2;` ` ` `// Function call` ` ` `Console.Write(xor_operations(N, arr, ` ` ` `M, K));` `}` `}` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(M * N) **Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Count of replacements required to make the sum of all Pairs of given type from the Array equal
- Minimum replacements to make elements of a ternary array same
- Find Kth element in an array containing odd elements first and then even elements
- Lexicographically smallest permutation with distinct elements using minimum replacements
- Kth largest element after every insertion
- Find the original Array using XOR values of all adjacent elements
- Swap Kth node from beginning with Kth node from end in a Linked List
- Multiplication table till N rows where every Kth row is table of K upto Kth term
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Count of even and odd set bit with array element after XOR with K
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
- Check if all elements of a Circular Array can be made equal by increments of adjacent pairs
- Array Queries for multiply, replacements and product
- Minimize difference between maximum and minimum of Array by at most K replacements
- Count of even and odd set bit Array elements after XOR with K for Q queries
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Minimize the maximum difference of adjacent elements after at most K insertions
- Maximum sum in an array such that every element has exactly one adjacent element to it

This article is contributed by **Mohan**. 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.