Smallest number that can replace all | Function to find the value of K to minimize the value of maximum absolute difference between adjacent elements ; Stores the maximum and minimum among array elements that are adjacent to " - 1" ; Traverse the given array arr [ ] ; If arr [ i ] is - 1 & arr [ i + 1 ] is not - 1 ; If arr [ i + 1 ] is - 1 & arr [ i ] is not - 1 ; If all array element is - 1 ; Otherwise ; Driver Code
function findMissingValue ( arr , N ) { let minE = Number . MAX_VALUE , maxE = Number . MIN_VALUE ; for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == - 1 && arr [ i + 1 ] != - 1 ) { minE = Math . min ( minE , arr [ i + 1 ] ) ; maxE = Math . max ( maxE , arr [ i + 1 ] ) ; } if ( arr [ i ] != - 1 && arr [ i + 1 ] == - 1 ) { minE = Math . min ( minE , arr [ i ] ) ; maxE = Math . max ( maxE , arr [ i ] ) ; } } if ( minE == Number . MAX_VALUE && maxE == Number . MIN_VALUE ) { document . write ( " " ) ; } else { document . write ( ( minE + maxE ) / 2 ) ; } } let arr = [ 1 , - 1 , - 1 , - 1 , 5 ] ; let N = arr . length ; findMissingValue ( arr , N ) ;