Find the last positive element remaining after repeated subtractions of smallest positive element from all Array elements | Function to calculate last positive element of the array ; Return the first element if N = 1 ; Stores the greatest and the second greatest element ; Traverse the array A [ ] ; If current element is greater than the greatest element ; If current element is greater than second greatest element ; Return the final answer ; Driver Code
def lastPositiveElement ( arr ) : NEW_LINE INDENT N = len ( arr ) ; NEW_LINE if ( N == 1 ) : NEW_LINE INDENT return arr [ 0 ] ; NEW_LINE DEDENT greatest = - 1 ; secondGreatest = - 1 ; NEW_LINE for x in arr : NEW_LINE INDENT if ( x >= greatest ) : NEW_LINE INDENT secondGreatest = greatest ; NEW_LINE greatest = x ; NEW_LINE DEDENT elif ( x >= secondGreatest ) : NEW_LINE INDENT secondGreatest = x ; NEW_LINE DEDENT DEDENT return greatest - secondGreatest ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 5 , 4 , 7 ] ; NEW_LINE print ( lastPositiveElement ( arr ) ) ; NEW_LINE DEDENT