Can anyone help me with Pro Chef And Katana from LoC June 2018 ?

Hello friends can someone tell me how to approach this problem ? I am getting WA.

@vijju123 @taran_1407 @vivek_1998299 @meooow @john_smith_3 @aryanc403 @vbt_95

Here is my latest submission. If it’s too messy, here is the approach I used :

n =readInt()
arr = readInts()
count = 0
for i in range(len(arr)):
    count+=1
    f =0 
    minus = arr[i]
    for j in range(len(arr)):
        arr[j]-=minus
    for j in range(len(arr)):
        if arr[j]>0:
            f =1
    if f==0:
        break
print(count)
1 Like

You’ll need to subtract the MINIMUM element of the segment from each element of the segment. By segment I mean, a contiguous range, surrounded by a 0 at both sides.

Eg. 1, 4, 7, 0, 4, 0, 7, 8 has three segments; 1 is minimum of the first segment, 4 of the second and 7 of the third segment.

Try the simple test:

2
2 1

Your code gives 1 as answer, but you need atleast 2 cuts.

1 Like

At least make sure arr[i] > 0 before subtracting it from the whole array.

n =readInt()

arr = readInts()

count = 0

for i in range(len(arr)):

count+=1

f =0 

minus = arr[i]

if minus <0:

    continue

for j in range(i,len(arr)):

    arr[j]-=minus

for j in range(i,len(arr)):

    if arr[j]>0:

        f =1

if f==0:


    break

print(count)

@sarthakmanna here is the approach where am I missing. Also for 100 points I feel it’s too slow.

I was trying to explain this approach: https://www.codechef.com/viewsolution/19150087

For 100pts, you can use a stack or priority queue like this: https://www.codechef.com/viewsolution/19011529

The 100pt solution is written in Java. I hope you’ll have no problem in understanding it. However, if you have any, feel free to ask.

1 Like

thanks man I got it now. Let me code it out using stack :slight_smile: