Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs | Javascript 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
var N = 1000 function lastElement ( a , n ) { var steps = 1 ; var v = Array . from ( Array ( N ) , ( ) => Array ( 0 ) ) ; if ( n == 1 ) return a [ 0 ] ; for ( var i = 0 ; i < n ; i += 2 ) v [ steps ] . push ( a [ i ] a [ i + 1 ] ) ; while ( v [ steps ] . length > 1 ) { steps += 1 ; for ( var i = 0 ; i < v [ steps - 1 ] . length ; i += 2 ) { if ( steps & 1 ) v [ steps ] . push ( v [ steps - 1 ] [ i ] v [ steps - 1 ] [ i + 1 ] ) ; v [ steps ] . push ( v [ steps - 1 ] [ i ] ^ v [ steps - 1 ] [ i + 1 ] ) ; } } return v [ steps ] [ 0 ] ; } var a = [ 1 , 4 , 5 , 6 ] ; var n = a . length ; var index = 0 ; var value = 2 ; a [ 0 ] = 2 ; document . write ( lastElement ( a , n ) + " " ) ; index = 3 ; value = 5 ; a [ index ] = value ; document . write ( lastElement ( a , n ) ) ;