In this HackerRank Service Lane problem 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.
Problem solution in Python programming.
value = input() value = value.split(' ') N = int(value[0]) T = int(value[1]) # Get width for each road segment width = [] value = input() value = value.split(' ') for a in value: width.append(int(a)) # Determine smallest width for ea test case for a in range(T): value = input() value = value.split(' ') x = int(value[0]) y = int(value[1]) smallest = 3 for b in range(x,y+1): if width[b] < smallest: smallest = width[b] print (smallest)
Problem solution in Java Programming.
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); } }
Problem solution in C++ programming.
#include<math.h> #include<algorithm> #include<cstdlib> #include<iostream> #include<stdio.h> #include<map> #include<ext/hash_map> #include<ext/hash_set> #include<set> #include<string> #include<vector> #include<time.h> #include<queue> #include<deque> #include<sstream> #include<stack> #include<sstream> #include <string.h> #define INF 1001001001 #define MA(a,b) ((a)>(b)?(a):(b)) #define MI(a,b) ((a)<(b)?(a):(b)) #define AB(a) (-(a)<(a)?(a):-(a)) #define P 1000000007ll #define X first #define Y second #define mp make_pair #define pb push_back #define pob pop_back #define E 0.000000001 #define Pi 3.1415926535897932384626433832795 using namespace std; using namespace __gnu_cxx; const int NN=4100001; int A,n,m,i,k,j,p,a[NN],b[NN],c[NN]; int main() { cin>>n>>m; for (i=0;i<n;i++) scanf("%d",&a[i]); for (j=1;j<=m;j++) { int l,r; cin>>l>>r; k=3; for (i=l;i<=r;i++) k=MI(k,a[i]); cout<<k<<endl; } return 0; }
Problem solution in C programming.
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int n,t,w[100000]; int i,j,a,b,val; scanf("%d %d",&n,&t); //printf("%d %d",n,t); for(i=0;i<n;i++) { scanf("%d",&w[i]); } /*printf("n"); for(i=0;i<n;i++) { printf("%d ",w[i]); }*/ for(i=0;i<t;i++) { scanf("%d %d",&a,&b); //printf("n%d %d",a,b); val=w[a]; for(j=a;j<=b;j++) { if(w[j]<val) val=w[j]; } printf("%dn",val); } /* Enter your code here. Read input from STDIN. Print output to STDOUT */ return 0; }
Problem solution in JavaScript programming.
function processData(input) { //Enter your code here var testCases = input.split("n"); var config = testCases.shift().split(' '); var widths = testCases.shift().split(' '); for(tIdx in testCases){ var vehicleSize=3; var thisCase = testCases[tIdx].split(' '); var caseMin = parseInt(thisCase[0]); var caseMax = parseInt(thisCase[1]); for(var ii=caseMin; ii <= caseMax; ii++){ vehicleSize = Math.min(widths[ii],vehicleSize); if(vehicleSize == 1) break; } console.log(vehicleSize); } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });