Rope Folding

Interview

Time limit1sMemory limit128 MB

Summary
Given knots at integer positions on a rope, count the fold points where every knot in the overlapping interval mirrors onto another knot.
Level

Medium4 of 10

Topics
Array, Brute force, Implementation, Geometry
Solved
No attempts yet

Problem

Farmer John has a rope of length LL (1≤L≤10,0001 \le L \le 10{,}000) that he uses for tasks around his farm. The rope has NN knots (1≤N≤1001 \le N \le 100) tied into it at distinct integer positions, including one knot at each of its two endpoints (positions 00 and LL).

FJ can fold the rope back onto itself at certain points. When he folds at a point, one side of the rope reflects over onto the other side and the two strands overlap. A fold is good if, wherever the two strands lie on top of each other, every knot on one strand lines up exactly with a knot on the other strand.

Formally, a fold at position ff (0<f<L0 < f < L) reflects a knot at position xx to position 2f−x2f - x. Let the shorter side have length d=min⁡(f, L−f)d = \min(f,\, L - f); the two strands then overlap on the interval [f−d, f+d][f - d,\, f + d]. The fold is good if, for every knot xx inside that overlap interval, its mirror position 2f−x2f - x is also a knot.

Folding exactly at a knot is allowed, but folding at either endpoint is not. Extra knots on the longer side of the fold — outside the overlap interval — do not matter. FJ only ever makes a single fold at a time.

Count the number of positions at which FJ can make a good fold.

Input

  • Line 1: Two space-separated integers, NN and LL.
  • Lines 2…N+12 \ldots N+1: Each line contains one integer in the range 0…L0 \ldots L, the position of a single knot. Two of these positions are always 00 and LL.

Output

  • Line 1: A single integer — the number of positions at which a good fold can be made.

Hint

For example, if the rope has length L=10L = 10 with knots at 0,2,4,6,100, 2, 4, 6, 10, the four good fold positions are 1,2,3,1, 2, 3, and 88:

  • Fold at 11: the overlap is [0,2][0, 2]; knots 00 and 22 mirror onto each other.
  • Fold at 22: the overlap is [0,4][0, 4]; knots 0,2,40, 2, 4 are symmetric about 22.
  • Fold at 33: the overlap is [0,6][0, 6]; knots 0,2,4,60, 2, 4, 6 are symmetric about 33.
  • Fold at 88: the overlap is [6,10][6, 10]; knots 66 and 1010 mirror onto each other (the extra knots 0,2,40, 2, 4 sit on the longer side and are ignored).

A fold position may be a half-integer: since the endpoint knot on the shorter side must reflect onto an integer knot, every good fold occurs where 2f2f is an integer.

Examples1

  1. Example 1

    Input
    5 10
    0
    10
    6
    2
    4
    
    Expected output
    4