This page is still under construction.

Parts of this page are still being built. What you see may change.

Leap Frog

Time limit1sMemory limit128 MB

Summary
Given sorted positions, Jack and Jill alternate hopping over each other within distance 10; find the minimum total jumps until one lands on the last position.
Level

Medium6 of 10

Topics
Dynamic programming, Greedy, Two pointers
Solved
No attempts yet

Problem

Jack and Jill play a game called “Leap Frog” in which they take alternating turns jumping over each other. In a single jump, each of them can move a maximum horizontal distance of 1010 units.

You are given a list of valid positions x1,x2,…,xnx_1, x_2, \ldots, x_n on which a player may stand. Jill starts at position x1x_1, Jack starts at position x2x_2, and their goal is to reach position xnx_n.

On every turn the player who is behind must hop over the player who is in front, landing on some valid position that lies strictly ahead of the front player. A jump is allowed only if the horizontal distance from the jumping player's current position to its landing position is at most 1010. The two players may never occupy the same position at the same time.

Determine the minimum total number of jumps, counting the jumps of both players, needed until either Jack or Jill reaches position xnx_n.

Input

The input contains multiple test cases. Each test case begins with a line containing a single integer nn (2≤n≤1000002 \le n \le 100000). The next line contains the integers x1 x2 … xnx_1\ x_2\ \ldots\ x_n where 0≤x1<x2<⋯<xn≤10000000 \le x_1 < x_2 < \cdots < x_n \le 1000000. The end of the input is marked by a line containing a single 00.

Output

For each test case, print on its own line the minimum total number of jumps needed for either Jack or Jill to reach position xnx_n, or −1-1 if neither of them can reach it.

Examples1

  1. Example 1

    Input
    6
    3 5 9 12 15 17
    6
    3 5 9 12 30 40
    0
    
    Expected output
    3
    -1