Fence

Practice this coding question on Coding Gurukul.

Problem statement

A community garden has a straight fence made from n planks. To bring in a new planter, volunteers must temporarily remove exactly k neighboring planks. Removing a plank costs effort proportional to its height, so the team wants the opening that requires the least total effort. Your task Find the starting position of a block of exactly k consecutive planks whose total height is minimum. Positions are numbered from 1 on the left. If several blocks have the same minimum total, choose the leftmost one. Important details The fence is a straight line, not a circle: a block cannot wrap from the last plank to the first. Planks outside the chosen block remain in place. When k = 1, choose the first shortest plank. When k = n, there is only one possible block, starting at position 1. Worked example For heights 5 2 1 4 1 2 and k = 3, the four possible block totals are 8, 7, 6 and 7. The smallest total is 6, achieved by the block at positions 3 through 5, so print 3. For heights 2 2 2 and k = 2, both blocks cost 4; the leftmost rule requires 1.

Input format

First line: n k. Second line: n plank heights.

Output format

Print the one-based starting position of the leftmost minimum-total block of length k.

Constraints

1 <= n <= 5000; 1 <= k <= n; 1 <= height[i] <= 100.

Sample input

6 3
5 2 1 4 1 2

Sample output

3

Sign in to run or submit a solution in C++, Java or Python.

Loading Coding Gurukul…