Find the light bulb with maximum glowing time | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; char longestLastingBulb ( vector < int > onTime , string s ) { char ans ; int n = onTime . size ( ) ; int maxDur = INT_MIN ; int maxPos = INT_MIN ; int currentDiff = 0 ; for ( int 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 ) { char one = s [ i ] ; char two = s [ maxPos ] ; if ( one > two ) { maxDur = currentDiff ; maxPos = i ; } } } } } ans = s [ maxPos ] ; return ans ; } int main ( ) { string S = " spuda " ; vector < int > arr = { 12 , 23 , 36 , 46 , 62 } ; cout << longestLastingBulb ( arr , S ) ; return 0 ; }