Find the missing number in unordered Arithmetic Progression | Java program for the above approach ; 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 ; Driver code ; Given array ; Function Call ; Print the missing element
class GFG { static int missingElement ( int arr [ ] , int n ) { int max_ele = arr [ 0 ] ; int min_ele = arr [ 0 ] ; int x = 0 ; int d ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > max_ele ) max_ele = arr [ i ] ; if ( arr [ i ] < min_ele ) min_ele = arr [ i ] ; } d = ( max_ele - min_ele ) / n ; for ( int i = 0 ; i < n ; i ++ ) { x = x ^ arr [ i ] ; } for ( int i = 0 ; i <= n ; i ++ ) { x = x ^ ( min_ele + ( i * d ) ) ; } return x ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 12 , 3 , 6 , 15 , 18 } ; int n = arr . length ; int element = missingElement ( arr , n ) ; System . out . print ( element ) ; } }