Solution For Brightest Position On Street
Problem Statement:
Given an array representing the brightness of each street light on a street, find the brightest position on the street.
Example:
Input: [4, 2, 8, 3, 6, 9]
Output: 5
Explanation: The 5th position has the brightest light with brightness level 9.
Solution:
To solve this problem, we need to find the maximum brightness level and then return its index in the array.
We can do this by iterating over the input array and keeping track of the maximum brightness level and its index. We can initialize the maximum brightness level and its index to the first element in the array. Then, we can loop through the rest of the elements in the array and check if the current element has a greater brightness level than the maximum brightness level we have seen so far. If it does, we update the maximum brightness level and its index.
Once we have iterated over all the elements in the array, we should have the index of the brightest position. We can then return this index.
Here is the Python code for the solution:
def brightest_position_on_street(lights):
max_brightness = lights[0]
max_brightness_index = 0
for i in range(1, len(lights)):
if lights[i] > max_brightness:
max_brightness = lights[i]
max_brightness_index = i
return max_brightness_index
The time complexity of this solution is O(n) because we are iterating over the input array once.
Overall, this problem is not difficult and can be solved with a simple linear scan of the input array.
Step by Step Implementation For Brightest Position On Street
class Solution { public int solution(int[] A) { // write your code in Java SE 8 int max = 0; int index = 0; for (int i = 0; i < A.length; i++) { if (A[i] > max) { max = A[i]; index = i; } } return index; } }
def brightest_position_on_street(street): # we can keep track of the current max brightness and the position # of that max brightness as we iterate through the street max_brightness = 0 position = 0 for i in range(len(street)): # if the current element is greater than the max brightness, # update the max and position if street[i] > max_brightness: max_brightness = street[i] position = i return position
Given an array of street light intensity, find the brightest position on the street. // Solution: function brightestPosition(arr) { let max = -Infinity; let index = -1; for (let i = 0; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; index = i; } } return index; }
The problem is as follows: Given an array of street light intensity readings, find the position of the brightest light. One solution is to simply iterate through the array and keep track of the maximum value seen so far. The position of the maximum value is then the position of the brightest light. int findBrightest(vector&readings) { int maxVal = readings[0]; int maxPos = 0; for(int i = 1; i < readings.size(); i++) { if(readings[i] > maxVal) { maxVal = readings[i]; maxPos = i; } } return maxPos; }
using System; public class Solution { // Function to find the position of the brightest light public static int findPosition(int[] streetLights) { // Initialize max_so_far int max_so_far = streetLights[0]; // Initialize position of the maximum element int pos = 0; // Traverse the given array from left to right for (int i = 1; i < streetLights.Length; i++) { // Compare current maximum with the maximum so far // and update the maximum if needed if (streetLights[i] > max_so_far) { max_so_far = streetLights[i]; pos = i; } } // Print the position of the maximum element return pos; } // Driver code public static void Main() { int[] streetLights = {5, 10, 20, 15}; Console.WriteLine(findPosition(streetLights)); } }