Join Regular Classroom : Visit ClassroomTech

TCS Digital Coding Solve | Code 3 | Codewindow.in

Question:

A motorsports car race is held from source A to destination B. There are ‘N’ number of halt en route A to B. The racer can skip a halt or stop at a halt. Each time he stops or skips a halt i, it costs him some amount of times, as per the array(a[i]), 0<i<=N-1. The task here to find the minimum amount of time it takes for the racer to reach the destination based on the following conditions:
1. Each racer can skip a maximum of two consecutive halts.
2. Each time a racer stops at a halt i, it cost him time equal to Minimum(a[i],Y) for given Y.
3. The racer cannot skip the first and the last halts.

Example 1:

Input:
5->Value of N
6->Value of Y
{5,1,4,7,3}->a[], Elements a[0] to a[N-1]. Where each input elements is
separeted by a newline.

Output:
19

Example 2:

Input:
6->Value of N
4->Value of Y
{1,2,4,6,5,3}->a[], Elements a[0] to a[N-1]. Where each input elements is
separeted by a newline.

Output:
18

Constrains:
0<N<=1000
0<Y<=1000
0<a[i]<=1000

Solution: In Python3

n=int(input())
y=int(input())
a=list(map(int,input().split()))

#print(n,y,a)

res=a[0]+a[-1]
a=a[1:-1]
#print(a)

count=0
for i in range(len(a)):
    if count==0 and a[i]>y:
        res+=y 
        count = 1 
    elif count == 1 and a[i]>y: 
        res+=y 
        count =2 
    elif count==2:
        res+=a[i]
    else:
        res+=a[i]
        count = 0
print(res)

Also Checkout

Recent Posts
Categories
Pages