Find the light bulb with maximum glowing time | 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
function longestLastingBulb ( onTime , s ) { let ans ; let n = onTime . length ; let maxDur = Number . MIN_VALUE ; let maxPos = Number . MIN_VALUE ; let currentDiff = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { currentDiff = onTime [ i ] ; maxDur = currentDiff ; maxPos = i ; } else { currentDiff = onTime [ i ] - onTime [ i - 1 ] ; if ( maxDur < currentDiff ) { maxDur = currentDiff ; maxPos = i ; } else { if ( maxDur == currentDiff ) { let one = s [ i ] ; let two = s [ maxPos ] ; if ( one > two ) { maxDur = currentDiff ; maxPos = i ; } } } } } ans = s [ maxPos ] ; return ans ; } let S = " " ; let arr = [ 12 , 23 , 36 , 46 , 62 ] ; document . write ( longestLastingBulb ( arr , S ) ) ;