Hello Programmers, In this post, you will learn how to solve HackerRank Service Lane Solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

HackerRank Service Lane
Task
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 <= j – i + 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
- (0, 3): From index 0 through 3 we have widths 2, 3, 1 and 2. Nothing wider than 1 can pass all segments.
- (4, 6): From index 4 through 6 we have width 3, 2 and 3. Nothing wider than 2 can pass all segments.
- (6, 7): 3, 3 -> 3.
- (3, 5): 2, 3, 2 –>2
- (0, 7): 2, 3, 1, 2, 3, 2, 3, 3 -> 1.
HackerRank Service Lane Solution
HackerRank Service Lane Solution Solution in C
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int i,n,j,t,min; scanf("%d%d",&n,&t); int* arr=(int*)malloc(n*sizeof(int)); int p,q; for(i=0;i<n;i++) { scanf("%d",arr+i); } min=4; for(i=0;i<t;i++) { scanf("%d%d",&p,&q); min=4; for(j=p;j<=q;j++) { if(arr[j]<min) min=arr[j]; } printf("%d\n",min); } return 0; }
HackerRank Service Lane Solution in Cpp
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int n, t; cin >> n >> t; int width[n]; for(int i = 0; i < n; i++) cin >> width[i]; for(int qq = 0; qq < t; qq++){ int i, j; cin >> i >> j; int m = 3; for(int k = i; k <= j; k++){ m = min(m, width[k]); } cout << m << endl; } return 0; }
HackerRank Service Lane Solution in Java
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] rags) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(br.readLine()); int N = Integer.parseInt(st.nextToken()); int t = Integer.parseInt(st.nextToken()); int[]r = new int[N]; st = new StringTokenizer(br.readLine()); for(int i=0;i<N;i++) { r[i] = Integer.parseInt(st.nextToken()); } while (t-- > 0) { st = new StringTokenizer(br.readLine()); int i = Integer.parseInt(st.nextToken()); int j = Integer.parseInt(st.nextToken()); int mini = 3; for(int k=i;k<=j;k++) { mini = Math.min(mini, r[k]); } pw.println(mini); } pw.flush(); } }
HackerRank Service Lane Solution in Python
import math n, t = map(int, raw_input().split()) width = map(int, raw_input().split()) for i in range(t): x,y = map(int, raw_input().split()) minW = min(width[x:(y+1)]) print minW
HackerRank Service Lane Solution using JavaScript
'use strict'; function processData(input) { var parse_fun = function (s) { return parseInt(s, 10); }; var lines = input.split('\n'); var params = lines.shift().split(' ').splice(0, 2).map(parse_fun); var N = params[0]; var T = params[1]; var data = lines.shift().split(' ').splice(0, N).map(parse_fun); var res = []; for (var t = 0; t < T; t++) { var p = lines[t].split(' ').splice(0, 2).map(parse_fun); var i = p[0]; var j = p[1]; var m = data[i]; for (var k = i + 1; k <= j; k++) { if (data[k] < m) { m = data[k]; } } res.push(m); } console.log(res.join('\n')); } process.stdin.resume(); process.stdin.setEncoding("ascii"); var _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });
HackerRank Service Lane Solution in Scala
object Solution { def main(args: Array[String]) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ val N_T = readLine.split(' ').map(_.toInt) val arr = readLine.split(' ').map(_.toInt) val min = (start: Int, end: Int) => { var minimum = arr(start) for (i <- start to end) { if(arr(i) < minimum) minimum = arr(i) } minimum } for(i <- 0 until N_T(1)){ val S_E = readLine.split(' ').map(_.toInt) println(min(S_E(0) , S_E(1))) } } }
HackerRank Service Lane Solution in Pascal
var N, T : longint; i, j, k, l, m, min : longint; width: array [0..100000] of integer; a: array [1..1000] of integer; begin readln(N,T); for k:=1 to N do read(width[k-1]); for l:=1 to T do begin min:=3; readln(i,j); for m:=i to j do begin if width[m]<min then min:=width[m]; end; a[l]:=min; end; for l:=1 to T do writeln(a[l]); end.
Disclaimer: This problem (Service Lane) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.