Count arrays having at least K elements exceeding XOR of all given array elements by X given operations | Java 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 ; Convert the input as ArrayList ; Given value of X & K
import java . util . ArrayList ; class GFG { static int ans = 0 ; public static void countArraysUtil ( ArrayList < Integer > arr , int X , int K , int xorVal ) { if ( X == 0 ) { int cnt = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { if ( arr . get ( i ) > xorVal ) cnt ++ ; } if ( cnt >= K ) ans ++ ; return ; } int temp = arr . get ( 0 ) ; arr . remove ( 0 ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . add ( 0 , temp ) ; temp = arr . get ( arr . size ( ) - 1 ) ; arr . remove ( arr . size ( ) - 1 ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . add ( temp ) ; arr . set ( 0 , arr . get ( 0 ) + 1 ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . set ( 0 , arr . get ( 0 ) - 1 ) ; arr . set ( arr . size ( ) - 1 , arr . get ( arr . size ( ) - 1 ) + 1 ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . set ( arr . size ( ) - 1 , arr . get ( arr . size ( ) - 1 ) - 1 ) ; } public static void countArrays ( ArrayList < Integer > arr , int X , int K ) { int xorVal = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) xorVal = xorVal ^ arr . get ( i ) ; countArraysUtil ( arr , X , K , xorVal ) ; System . out . println ( ans ) ; } public static void main ( String arg [ ] ) { int [ ] input = { 10 , 2 , 10 , 5 } ; ArrayList < Integer > arr = new ArrayList < Integer > ( ) ; for ( int i : input ) { arr . add ( i ) ; } int X = 3 , K = 3 ; countArrays ( arr , X , K ) ; } }