FunctionRacerProblemsRace

Change Over k Days

BeginnerTwo pointersPython, JavaScript, TypeScript, Java, C++

prices[i] is the price of a ticket on day i. For every day that has a day k days before it in the list, work out how much the price changed in between: the price on that day minus the price k days earlier.

Return these changes in the order of the days, starting with day k. If the list has k days or fewer, there is nothing to compare, so return an empty list.

Example 1

prices [10,12,9,15]
k 1
returns [2,-3,6]

Each day is compared with the day before: 12 - 10 = 2, 9 - 12 = -3 and 15 - 9 = 6.

Example 2

prices [10,12,9,15]
k 2
returns [-1,3]

With k = 2, day 2 is compared with day 0 and day 3 with day 1: 9 - 10 = -1 and 15 - 12 = 3.

Example 3

prices [5,8]
k 3
returns []

Two days are not enough to look 3 days back.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Two pointers problems