Smallest number that can replace all | Python 3 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 sys NEW_LINE def findMissingValue ( arr , N ) : NEW_LINE INDENT minE = sys . maxsize NEW_LINE maxE = - sys . maxsize - 1 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( arr [ i ] == - 1 and arr [ i + 1 ] != - 1 ) : NEW_LINE INDENT minE = min ( minE , arr [ i + 1 ] ) NEW_LINE maxE = max ( maxE , arr [ i + 1 ] ) NEW_LINE DEDENT if ( arr [ i ] != - 1 and arr [ i + 1 ] == - 1 ) : NEW_LINE INDENT minE = min ( minE , arr [ i ] ) NEW_LINE maxE = max ( maxE , arr [ i ] ) NEW_LINE DEDENT DEDENT if ( minE == sys . maxsize and maxE == - sys . maxsize - 1 ) : NEW_LINE INDENT print ( "0" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ( minE + maxE ) // 2 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , - 1 , - 1 , - 1 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE findMissingValue ( arr , N ) NEW_LINE DEDENT