Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs | C # program to print the Leftover element after performing alternate Bitwise OR and Bitwise XOR operations to the pairs . ; count the step number ; if one element is there , it will be the answer ; at first step we do a bitwise OR ; keep on doing bitwise operations till the last element is left ; perform operations ; if step is the odd step ; else even step ; answer when one element is left ; Driver Code ; 1 st query ; 2 nd query
using System ; using System . Collections . Generic ; class GFG { static int N = 1000 ; static int lastElement ( int [ ] a , int n ) { int steps = 1 ; List < int > [ ] v = new List < int > [ N ] ; for ( int i = 0 ; i < N ; i ++ ) v [ i ] = new List < int > ( ) ; if ( n == 1 ) return a [ 0 ] ; for ( int i = 0 ; i < n ; i += 2 ) v [ steps ] . Add ( a [ i ] a [ i + 1 ] ) ; while ( v [ steps ] . Count > 1 ) { steps += 1 ; for ( int i = 0 ; i < v [ steps - 1 ] . Count ; i += 2 ) { if ( steps % 2 == 1 ) v [ steps ] . Add ( v [ steps - 1 ] [ i ] v [ steps - 1 ] [ i + 1 ] ) ; v [ steps ] . Add ( v [ steps - 1 ] [ i ] ^ v [ steps - 1 ] [ i + 1 ] ) ; } } return v [ steps ] [ 0 ] ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 4 , 5 , 6 } ; int n = a . Length ; int index = 0 ; int value = 2 ; a [ 0 ] = 2 ; Console . WriteLine ( lastElement ( a , n ) ) ; index = 3 ; value = 5 ; a [ index ] = value ; Console . WriteLine ( lastElement ( a , n ) ) ; } }