Count arrays having at least K elements exceeding XOR of all given array elements by X given operations | C ++ program for the above approach ; Stores the final answer ; Utility function to count arrays having at least K elements exceeding XOR of all given array elements ; If no operations are left ; Stores the count of possible arrays ; Count array elements are greater than XOR ; Stores first element ; Delete first element ; Recursive call ; Insert first element into vector ; Stores the last element ; Remove last element from vector ; Recursive call ; Push last element into vector ; Increment first element ; Recursive call ; Decrement first element ; Increment last element ; Recursive call ; Decrement last element ; Function to find the count of arrays having atleast K elements greater than XOR of array ; Stores the XOR value of original array ; Traverse the vector ; Print the answer ; Driver Code ; Given vector ; Given value of X & K
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ans = 0 ; void countArraysUtil ( vector < int > & arr , int X , int K , int xorVal ) { if ( X == 0 ) { int cnt = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { if ( arr [ i ] > xorVal ) cnt ++ ; } if ( cnt >= K ) ans ++ ; return ; } int temp = arr [ 0 ] ; arr . erase ( arr . begin ( ) ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . insert ( arr . begin ( ) , temp ) ; temp = arr . back ( ) ; arr . pop_back ( ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . push_back ( temp ) ; arr [ 0 ] ++ ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr [ 0 ] -- ; arr [ arr . size ( ) - 1 ] ++ ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr [ arr . size ( ) - 1 ] -- ; } void countArrays ( vector < int > & arr , int X , int K ) { int xorVal = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) xorVal = xorVal ^ arr [ i ] ; countArraysUtil ( arr , X , K , xorVal ) ; cout << ans ; } int main ( ) { vector < int > arr = { 10 , 2 , 10 , 5 } ; int X = 3 , K = 3 ; countArrays ( arr , X , K ) ; return 0 ; }