Smallest number that can replace all | Java program for the above approach ; 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
import java . io . * ; class GFG { public static void findMissingValue ( int arr [ ] , int N ) { int minE = Integer . MAX_VALUE , maxE = Integer . MIN_VALUE ; for ( int 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 == Integer . MAX_VALUE && maxE == Integer . MIN_VALUE ) { System . out . println ( "0" ) ; } else { System . out . println ( ( minE + maxE ) / 2 ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , - 1 , - 1 , - 1 , 5 } ; int N = arr . length ; findMissingValue ( arr , N ) ; } }