Saturday, August 5, 2017

Service Lane - Problem from HackerRank

Problem Statement:

Calvin is driving his favorite vehicle on the 101 freeway. He notices that the check engine light of his vehicle is on, and he wants to service it immediately to avoid any risks. Luckily, a service lane runs parallel to the highway. The length of the service lane is  units. The service lane consists of  segments of equal length and different width.
Calvin can enter to and exit from any segment. Let's call the entry segment as index  and the exit segment as index . Assume that the exit segment lies after the entry segment () and . Calvin has to pass through allsegments from index  to index  (both inclusive).
Paradise Highway
Calvin has three types of vehicles - bike, car, and truck - represented by  and , respectively. These numbers also denote the width of the vehicle.
You are given an array  of length , where  represents the width of the th segment of the service lane. It is guaranteed that while servicing he can pass through at most  segments, including the entry and exit segments.
  • If , only the bike can pass through the th segment.
  • If , the bike and the car can pass through the th segment.
  • If , all three vehicles can pass through the th segment.
Given the entry and exit point of Calvin's vehicle in the service lane, output the type of the largest vehicle which can pass through the service lane (including the entry and exit segments).
Input Format
The first line of input contains two integers,  and , where  denotes the length of the freeway and  the number of test cases. The next line has  space-separated integers which represent the  array.
 test cases follow. Each test case contains two integers,  and , where  is the index of the segment through which Calvin enters the service lane and  is the index of the lane segment through which he exits.
Constraints
Output Format
For each test case, print the number that represents the largest vehicle type that can pass through the service lane.
Note: Calvin has to pass through all segments from index  to index  (both inclusive).

Problem Solution using python 2.7.6:

N, T = map(int,raw_input().split(' '))
width = map(int,raw_input().split()[:N])
ans =[]
while T:   
    i,j = map(int,raw_input().split())
    ans.append(min(width[i:j+1]))
    T -=1
for r in ans: print r       

No comments:

Post a Comment