# Service Lane – HackerRank Solution

In this post, we will solve Service Lane HackerRank Solution. This problem (Service Lane) is a part of HackerRank Problem Solving series.

A driver is driving on the freeway. The check engine light of his vehicle is on, and the driver wants to get service immediately. Luckily, a service lane runs parallel to the highway. It varies in width along its length.

You will be given an array of widths at points along the road (indices), then a list of the indices of entry and exit points. Considering each entry and exit point pair, calculate the maximum size vehicle that can travel that segment of the service lane safely.

Example

n = 4
width = [2, 3, 2, 1]
cases = [[1, 2], [2, 4]]

If the entry index,Â i = 1Â and the exit,Â j = 2, there are two segment widths ofÂ 2Â andÂ 3Â respectively. The widest vehicle that can fit through both isÂ 2. IfÂ i = 2Â andÂ j = 4, the widths areÂ [3, 2, 1]Â which limits vehicle width toÂ 1.

Function Description

Complete the serviceLane function in the editor below.

serviceLane has the following parameter(s):

• int n:Â the size of theÂ widthÂ array
• int cases[t][2]:Â each element contains the starting and ending indices for a segment to consider, inclusive

Returns

• int[t]:Â the maximum width vehicle that can pass through each segment of the service lane described

## Input Format

The first line of input contains two integers,Â nÂ andÂ t, whereÂ nÂ denotes the number of width measurements andÂ t, the number of test cases. The next line hasÂ nÂ space-separated integers which represent the arrayÂ width.

The nextÂ tÂ lines contain two integers,Â iÂ andÂ j, whereÂ iÂ is the start index andÂ jÂ is the end index of the segment to check.

## Constraints

• 2 <= n <= 100000
• 1 <= t <= 1000
• 0 <= i < j < n
• 2 <= ji + 1 <= min(n, 1000)
• 1 <= width[k] <= 3, where 0 <= k < n

Sample Input

``````STDIN               Function
-----               --------
8 5                 n = 8, t = 5
2 3 1 2 3 2 3 3     width = [2, 3, 1, 2, 3, 2, 3, 3]
0 3                 cases = [[0, 3], [4, 6], [6, 7], [3, 5], [0, 7]]
4 6
6 7
3 5
0 7``````

Sample Output

``````1
2
3
2
1``````

Explanation

Below is the representation of the lane:

``````   |HIGHWAY|Lane|    ->    Width

0: |       |--|            2
1: |       |---|           3
2: |       |-|             1
3: |       |--|            2
4: |       |---|           3
5: |       |--|            2
6: |       |---|           3
7: |       |---|           3``````
1. (0, 3): From indexÂ 0Â throughÂ 3Â we have widthsÂ 2, 3, 1Â andÂ 2. Nothing wider thanÂ 1Â can pass all segments.
2. (4, 6): From indexÂ 4Â throughÂ 6Â we have widthÂ 3, 2Â andÂ 3. Nothing wider thanÂ 2Â can pass all segments.
3. (6, 7):Â Â 3, 3 -> 3.
4. (3, 5):Â 2, 3, 2 ->2
5. (0, 7):Â 2, 3, 1, 2, 3, 2, 3, 3 -> 1.

## Solution – Service Lane – HackerRank Solution

### C++

```#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int my_min(const vector<int>& v, int i, int j) {
int the_min = v[i];

for (int o = i+1; o <= j;o++)
if (v[o] < the_min)
the_min = v[o];

return the_min;
}
int main(){
int n;
int t;
cin >> n >> t;
vector<int> width(n);
for(int width_i = 0;width_i < n;width_i++){
cin >> width[width_i];
}
for(int a0 = 0; a0 < t; a0++){
int i;
int j;
cin >> i >> j;

cout << my_min(width,i,j) << endl;
}
return 0;
}
```

### Python

```import sys

def check_interval(i, j):
res = 3
for inter in range(i, j + 1):
res = min(res, width[inter])

return res

n,t = input().strip().split(' ')
n,t = [int(n),int(t)]
width = [int(width_temp) for width_temp in input().strip().split(' ')]
for a0 in range(t):
i,j = input().strip().split(' ')
i,j = [int(i),int(j)]

print(check_interval(i, j))
```

### Java

```import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
try (Scanner in = new Scanner(System.in)) {
in.nextLine();
int[] widths = getWidthArray(in.nextLine());
while (in.hasNextLine()) {
printMin(widths, in.nextInt(), in.nextInt());
}
}
}

public static int[] getWidthArray(String widthLine) {
String[] stringWidths = widthLine.split(" ");
int[] widths = new int[stringWidths.length];
for (int i = 0; i < stringWidths.length; i++) {
widths[i] = Integer.parseInt(stringWidths[i]);
}
return widths;
}

public static void printMin(int[] width, int l, int u) {
int maxWidth = Integer.MAX_VALUE;
for(int i = l; i <= u; i++) {
if (width[i] < maxWidth)
maxWidth = width[i];
}
System.out.println(maxWidth);
}
}
```

Note: This problem (Service Lane) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.