# Find the light bulb with maximum glowing time

• Difficulty Level : Basic
• Last Updated : 11 Jun, 2021

Given a string S consisting of N unique lowercase alphabets and an array arr[] of length N where character S[i] represents the bulb and arr[i] represents the time up to which the ith bulb glows, starting from the time arr[i – 1]. The task is to find the bulb with maximum glowing time. If there exists more than one bulb with the same maximum glowing time, print the lexicographically greater one.

Examples:

Input: S = “abcd”, arr[] = {9, 29, 49, 50}
Output: c
Explanation:
‘c’ at index 0 has a duration = 9.
‘b’ at index 1 has a duration = arr[1] – arr[0] = 29 – 9 = 20
‘c’ at index 2 has a duration = arr[2] – arr[1]= 49 – 29 = 20
‘d’ at index 3 has a duration = arr[3] – arr[2]= 50 – 49 = 1
Two bulbs, ‘b’ and ‘c’, have the maximum glowing time. Among those two, ‘c’ is lexicographically larger.

Input: S = “spuda”, arr[] = {12, 23, 36, 46, 62}
Output: a
Explanation:
‘s’ at index 0 has a duration = 12.
‘p’ at index 1 has a duration = arr[1] – arr[0] = 23-12 = 11.
‘u’ at index 2 has a duration = arr[2] – arr[1] = 36-23 = 13.
‘d’ at index 3 has a duration = arr[3] – arr[2] = 46-36 = 10.
‘a’ at index 4 has a duration = arr[4] – arr[3] = 62-46 = 16.
Therefore, ‘a’ has maximum glowing time.

Approach: The idea is to traverse the array and for each array element, calculate arr[i] – arr[i – 1]. Then, print the lexicographically larger bulb having maximum glowing time. Follow the steps below to solve the problem:

• Initialize two variables, say maxDur and maxPos, to store the glowing time and the index of the bulb with maximum glowing time, respectively.
• Traverse the given array and perform the following steps:
• If the current time (arr[i] – arr[i – 1]) is smaller than maxCurr, update maxCurr as maxCurr = arr[i] – arr[i – 1].
• Otherwise, if it is equal to maxCurr, maxPos does not contain any valid index and S[maxPos] is lexicographically smaller than S[i], update maxPos as maxPos = i.
• After completing the above steps, print S[maxPos] as the required output.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach`   `#include ` `using` `namespace` `std;`   `// Function to find the bulb` `// having maximum glow` `char` `longestLastingBulb(` `    ``vector<``int``> onTime, string s)` `{` `    ``char` `ans;` `    ``int` `n = onTime.size();`   `    ``// Initialize variables` `    ``int` `maxDur = INT_MIN;` `    ``int` `maxPos = INT_MIN;` `    ``int` `currentDiff = 0;`   `    ``// Traverse the array consisting` `    ``// of glowing time of the bulbs` `    ``for` `(``int` `i = 0; i < n; i++) {`   `        ``// For 1st bulb` `        ``if` `(i == 0) {` `            ``currentDiff = onTime[i];` `            ``maxDur = currentDiff;` `            ``maxPos = i;` `        ``}` `        ``else` `{`   `            ``// Calculate the glowing time` `            ``currentDiff = onTime[i]` `                          ``- onTime[i - 1];`   `            ``// Update the maximum glow` `            ``if` `(maxDur < currentDiff) {` `                ``maxDur = currentDiff;` `                ``maxPos = i;` `            ``}`   `            ``// Find lexicographically` `            ``// largest bulb` `            ``else` `{`   `                ``if` `(maxDur == currentDiff) {` `                    ``char` `one = s[i];` `                    ``char` `two = s[maxPos];`   `                    ``if` `(one > two) {` `                        ``maxDur = currentDiff;` `                        ``maxPos = i;` `                    ``}` `                ``}` `            ``}` `        ``}` `    ``}`   `    ``// Bulb with maximum time` `    ``ans = s[maxPos];`   `    ``// Return the resultant bulb` `    ``return` `ans;` `}`   `// Driver Code` `int` `main()` `{` `    ``string S = ``"spuda"``;` `    ``vector<``int``> arr = { 12, 23, 36, 46, 62 };`   `    ``// Function call` `    ``cout << longestLastingBulb(arr, S);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.util.*;`   `class` `GFG{`   `// Function to find the bulb` `// having maximum glow` `static` `char` `longestLastingBulb(` `    ``int` `[]onTime, ``char` `[]s)` `{` `    ``char` `ans;` `    ``int` `n = onTime.length;`   `    ``// Initialize variables` `    ``int` `maxDur = Integer.MIN_VALUE;` `    ``int` `maxPos = Integer.MIN_VALUE;` `    ``int` `currentDiff = ``0``;`   `    ``// Traverse the array consisting` `    ``// of glowing time of the bulbs` `    ``for` `(``int` `i = ``0``; i < n; i++) {`   `        ``// For 1st bulb` `        ``if` `(i == ``0``) {` `            ``currentDiff = onTime[i];` `            ``maxDur = currentDiff;` `            ``maxPos = i;` `        ``}` `        ``else` `{`   `            ``// Calculate the glowing time` `            ``currentDiff = onTime[i]` `                          ``- onTime[i - ``1``];`   `            ``// Update the maximum glow` `            ``if` `(maxDur < currentDiff) {` `                ``maxDur = currentDiff;` `                ``maxPos = i;` `            ``}`   `            ``// Find lexicographically` `            ``// largest bulb` `            ``else` `{`   `                ``if` `(maxDur == currentDiff) {` `                    ``char` `one = s[i];` `                    ``char` `two = s[maxPos];`   `                    ``if` `(one > two) {` `                        ``maxDur = currentDiff;` `                        ``maxPos = i;` `                    ``}` `                ``}` `            ``}` `        ``}` `    ``}`   `    ``// Bulb with maximum time` `    ``ans = s[maxPos];`   `    ``// Return the resultant bulb` `    ``return` `ans;` `}`   `// Driver Code` `public` `static` `void` `main(String[] args)` `{` `    ``String S = ``"spuda"``;` `    ``int` `[]arr = { ``12``, ``23``, ``36``, ``46``, ``62` `};`   `    ``// Function call` `    ``System.out.print(longestLastingBulb(arr, S.toCharArray()));`   `}` `}`   `// This code is contributed by Amit Katiyar`

## Python3

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

## C#

 `// C# program for the above approach ` `using` `System;` `using` `System.Collections.Generic; ` `class` `GFG {` `    `  `    ``// Function to find the bulb ` `    ``// having maximum glow ` `    ``static` `char` `longestLastingBulb( ` `        ``List<``int``> onTime, ``string` `s) ` `    ``{ ` `        ``char` `ans; ` `        ``int` `n = onTime.Count; ` `      `  `        ``// Initialize variables ` `        ``int` `maxDur = Int32.MinValue; ` `        ``int` `maxPos = Int32.MinValue; ` `        ``int` `currentDiff = 0; ` `      `  `        ``// Traverse the array consisting ` `        ``// of glowing time of the bulbs ` `        ``for` `(``int` `i = 0; i < n; i++) { ` `      `  `            ``// For 1st bulb ` `            ``if` `(i == 0) { ` `                ``currentDiff = onTime[i]; ` `                ``maxDur = currentDiff; ` `                ``maxPos = i; ` `            ``} ` `            ``else` `{ ` `      `  `                ``// Calculate the glowing time ` `                ``currentDiff = onTime[i] ` `                              ``- onTime[i - 1]; ` `      `  `                ``// Update the maximum glow ` `                ``if` `(maxDur < currentDiff) { ` `                    ``maxDur = currentDiff; ` `                    ``maxPos = i; ` `                ``} ` `      `  `                ``// Find lexicographically ` `                ``// largest bulb ` `                ``else` `{ ` `      `  `                    ``if` `(maxDur == currentDiff) { ` `                        ``char` `one = s[i]; ` `                        ``char` `two = s[maxPos]; ` `      `  `                        ``if` `(one > two) { ` `                            ``maxDur = currentDiff; ` `                            ``maxPos = i; ` `                        ``} ` `                    ``} ` `                ``} ` `            ``} ` `        ``} ` `      `  `        ``// Bulb with maximum time ` `        ``ans = s[maxPos]; ` `      `  `        ``// Return the resultant bulb ` `        ``return` `ans; ` `    ``} `   `  ``static` `void` `Main() {`   `    ``string` `S = ``"spuda"``; ` `    ``List<``int``> arr = ``new` `List<``int``>(``new` `int``[] {12, 23, 36, 46, 62});` `  `  `    ``// Function call ` `    ``Console.Write(longestLastingBulb(arr, S)); ` `  ``}` `}`   `// This code is contributed by divyeshrabadiya07`

## Javascript

 ``

Output:

`a`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :