Find the light bulb with maximum glowing time | Python3 program for the above approach ; Function to find the bulb having maximum glow ; Initialize variables ; Traverse the array consisting of glowing time of the bulbs ; For 1 st bulb ; Calculate the glowing time ; Update the maximum glow ; Find lexicographically largest bulb ; Bulb with maximum time ; Return the resultant bulb ; Driver Code ; Function call
import sys NEW_LINE INT_MIN = ( sys . maxsize - 1 ) NEW_LINE def longestLastingBulb ( onTime , s ) : NEW_LINE INDENT n = len ( onTime ) NEW_LINE maxDur = INT_MIN NEW_LINE maxPos = INT_MIN NEW_LINE currentDiff = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT currentDiff = onTime [ i ] NEW_LINE maxDur = currentDiff NEW_LINE maxPos = i NEW_LINE DEDENT else : NEW_LINE INDENT currentDiff = onTime [ i ] - onTime [ i - 1 ] NEW_LINE if ( maxDur < currentDiff ) : NEW_LINE INDENT maxDur = currentDiff NEW_LINE maxPos = i NEW_LINE DEDENT else : NEW_LINE INDENT if ( maxDur == currentDiff ) : NEW_LINE INDENT one = s [ i ] NEW_LINE two = s [ maxPos ] NEW_LINE if ( one > two ) : NEW_LINE INDENT maxDur = currentDiff NEW_LINE maxPos = i NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT ans = s [ maxPos ] NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " spuda " NEW_LINE arr = [ 12 , 23 , 36 , 46 , 62 ] NEW_LINE print ( longestLastingBulb ( arr , S ) ) NEW_LINE DEDENT