cswork / OAOpen practice workspace

BoxOA #3

Box Fro~yo

EasyJudge-ready

Topics (AI-labelled)In-place Arrays

Given a circularly arranged array of flavors, the current position startIndex, and the target flavor target, find the minimum number of steps needed to move left or right to the target flavor. If the target flavor appears multiple times, choose the nearest one.

Input

A single line containing a JSON object with flavors (a non-empty array of strings), startIndex (starting from 0), and target (a flavor name that appears in the array).

Output

Output the minimum number of left/right moves needed to reach any position holding the target flavor.

Example 1

Input:

{"flavors": ["originalart", "banana", "custard", "mango"], "startIndex": 1, "target": "originalart"}

Output:

1

Example 2

Input:

{"flavors": ["originalart", "banana", "custard", "mango"], "startIndex": 1, "target": "banana"}

Output:

0

Example 3

Input:

{"flavors": ["a", "b", "c", "d", "e"], "startIndex": 4, "target": "b"}

Output:

2

Example 4

Input:

{"flavors": ["a", "b", "c", "d", "e"], "startIndex": 0, "target": "d"}

Output:

2

Example 5

Input:

{"flavors": ["only"], "startIndex": 0, "target": "only"}

Output:

0

Example 6

Input:

{"flavors": ["x", "target", "a", "target", "y"], "startIndex": 0, "target": "target"}

Output:

1

Example notes

Note: the explanation of the source example clearly specifies the target as originalart, so the candidate only corrects target=banana in the example to originalart; if the original input kept banana, the correct answer would be 0.

Editorial

Loading solution…