Service Lane HackerRank Solution in C, C++, Java, Python

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.

Paradise Highway

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 .

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

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

Service Lane HackerRank 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;
}

 

Service Lane HackerRank Solution in C++

#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;
}

 

Service Lane HackerRank 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();
    }
}

 

Service Lane HackerRank 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

 

Service Lane HackerRank Solution in C#

using System;
using System.Collections.Generic;
namespace Solution
{
    class Solution
    {
        static void Main(string[] args)
        {
            var line1 = System.Console.ReadLine().Trim().Split();
            var N = Int32.Parse(line1[0]);
            var T = Int32.Parse(line1[1]);
            line1 = System.Console.ReadLine().Trim().Split();

            int[] w = new int[N];
            for (var i = 0; i < N; i++)
            {
                w[i] = int.Parse(line1[i]);
            }
            for (int i = 0; i < T; i++)
            {
                line1 = System.Console.ReadLine().Trim().Split();
                var a = int.Parse(line1[0]);
                var b = int.Parse(line1[1]);
                int min = int.MaxValue;
                for (int k = a; k <= b; k++)
                {
                    if (w[k] < min)
                    {
                        min = w[k];
                    }
                }
                System.Console.WriteLine(min);
            }
        }
    }
}

Attempt Service Lane HackerRank Challenge

Link – https://www.hackerrank.com/challenges/service-lane/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/lisas-workbook-hackerrank-solution/

 

Leave a Comment