Find the missing number in unordered Arithmetic Progression | Function to get the missing element ; For maximum Element in the array ; For minimum Element in the array ; For xor of all elements ; Common difference of AP series ; find maximum and minimum element ; Calculating common difference ; Calculate the XOR of all elements ; Perform XOR with actual AP series resultant x will be the ans ; Return the missing element ; Given array ; Function Call ; Print the missing element
function missingElement ( arr , n ) { let max_ele = arr [ 0 ] ; let min_ele = arr [ 0 ] ; let x = 0 ; let d ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > max_ele ) max_ele = arr [ i ] ; if ( arr [ i ] < min_ele ) min_ele = arr [ i ] ; } d = parseInt ( ( max_ele - min_ele ) / n , 10 ) ; for ( let i = 0 ; i < n ; i ++ ) { x = x ^ arr [ i ] ; } for ( let i = 0 ; i <= n ; i ++ ) { x = x ^ ( min_ele + ( i * d ) ) ; } return x ; } let arr = [ 12 , 3 , 6 , 15 , 18 ] ; let n = arr . length ; let element = missingElement ( arr , n ) ; document . write ( element ) ;