Given an array arr[] of size N, the task is to count the number of arrays having at least K elements greater than the XOR of all array elements, generated by performing the following operations X times.
- Select either first or last element from the given array.
- Either increment the selected element by 1 or delete the selected element.
Examples:
Input: arr[] = {10, 2, 10, 5}, X = 3, K = 3
Output: 1
Explanation:
XOR of the given array = 7. The only possible array satisfying the condition is {10, 2, 10, 8}, obtained by incrementing the last array element thrice.Input: arr[] = {3, 3, 4}, X = 3, K = 2
Output: 3
Approach: The idea is to use the backtracking approach to recursively try out all the possible moves and increment the count when the required array is obtained. Possible moves are:
- Initialize a variable, say xorValue, to calculate the XOR of the original array.
- Initialize a variable, say count, to store the final count of the required arrays.
- Recursively try all the following four possibilities and increment the count when the required array is obtained:
- Delete the first element of the array.
- Delete the last element of the array.
- Increment the first array element by one.
- Increment last array element by one.
- Print the final count as the answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Stores the final answer int ans = 0; // Utility function to count arrays // having at least K elements exceeding // XOR of all given array elements void countArraysUtil(vector<int>& arr, int X, int K, int xorVal) { // If no operations are left if (X == 0) { // Stores the count of // possible arrays int cnt = 0; // Count array elements are // greater than XOR for (int i = 0; i < arr.size(); i++) { if (arr[i] > xorVal) cnt++; } if (cnt >= K) ans++; return; } // Stores first element int temp = arr[0]; // Delete first element arr.erase(arr.begin()); // Recursive call countArraysUtil(arr, X - 1, K, xorVal); // Insert first element into vector arr.insert(arr.begin(), temp); // Stores the last element temp = arr.back(); // Remove last element from vector arr.pop_back(); // Recursive call countArraysUtil(arr, X - 1, K, xorVal); // Push last element into vector arr.push_back(temp); // Increment first element arr[0]++; // Recursive call countArraysUtil(arr, X - 1, K, xorVal); // Decrement first element arr[0]--; // Increment last element arr[arr.size() - 1]++; // Recursive call countArraysUtil(arr, X - 1, K, xorVal); // Decrement last element arr[arr.size() - 1]--; } // Function to find the count of // arrays having atleast K elements // greater than XOR of array void countArrays(vector<int>& arr, int X, int K) { // Stores the XOR value // of original array int xorVal = 0; // Traverse the vector for (int i = 0; i < arr.size(); i++) xorVal = xorVal ^ arr[i]; countArraysUtil(arr, X, K, xorVal); // Print the answer cout << ans; } // Driver Code int main() { // Given vector vector<int> arr = { 10, 2, 10, 5 }; // Given value of X & K int X = 3, K = 3; countArrays(arr, X, K); return 0; } |
1
Time Complexity: O(4K * N)
Auxiliary Space: O(1)
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.

