text: Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum | Function to find the missing number such that maximum absolute difference is minimum ; Loop to find the maximum and minimum adjacent element to missing number ; Driver Code ; Function Call
code: function missingnumber ( n , arr ) { let mn = 10000 ; let mx = - 10000 ; for ( let i = 0 ; i < n ; i ++ ) { if ( i > 0 && arr [ i ] == - 1 && arr [ i - 1 ] != - 1 ) { mn = Math . min ( mn , arr [ i - 1 ] ) ; mx = Math . max ( mx , arr [ i - 1 ] ) ; } if ( i < ( n - 1 ) && arr [ i ] == - 1 && arr [ i + 1 ] != - 1 ) { mn = Math . min ( mn , arr [ i + 1 ] ) ; mx = Math . max ( mx , arr [ i + 1 ] ) ; } } let res = ( mx + mn ) / 2 ; return res ; } let n = 5 ; let arr = [ - 1 , 10 , - 1 , 12 , - 1 ] ; let res = missingnumber ( n , arr ) ; document . write ( res ) ;