Maximize the last Array element as per the given conditions | Function to find the maximum possible value that can be placed at the last index ; Sort the array elements in ascending order ; If the first element is is not equal to 1 ; Traverse the array to make difference between adjacent elements <= 1 ; Driver Code
def maximizeFinalElement ( arr , n ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE if ( arr [ 0 ] != 1 ) : NEW_LINE INDENT arr [ 0 ] = 1 ; NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] - arr [ i - 1 ] > 1 ) : NEW_LINE INDENT arr [ i ] = arr [ i - 1 ] + 1 ; NEW_LINE DEDENT DEDENT return arr [ n - 1 ] ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 ; NEW_LINE arr = [ 3 , 1 , 3 , 4 ] ; NEW_LINE max = maximizeFinalElement ( arr , n ) ; NEW_LINE print ( max ) ; NEW_LINE DEDENT