Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum | Java implementation of the missing number such that maximum absolute difference between adjacent element 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
import java . util . * ; class GFG { static int missingnumber ( int n , int arr [ ] ) { int mn = Integer . MAX_VALUE , mx = Integer . MIN_VALUE ; for ( int 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 ] ) ; } } int res = ( mx + mn ) / 2 ; return res ; } public static void main ( String [ ] args ) { int n = 5 ; int arr [ ] = { - 1 , 10 , - 1 , 12 , - 1 } ; int res = missingnumber ( n , arr ) ; System . out . print ( res ) ; } }