#P5944. Elimination Game: Finding the Minimal Counting Number

    ID: 19169 Type: Default 1000ms 256MiB

Elimination Game: Finding the Minimal Counting Number

Elimination Game: Finding the Minimal Counting Number

There are n children numbered from \(1\) to \(n\) playing an elimination game. The child with number \(i+1\) stands to the left of the child with number \(i\) (and the child \(1\) stands to the left of child \(n\)). The game starts with child \(1\) counting, then the child to its left (i.e. increasing order modulo \(n\)) continues the counting. When the count reaches a number \(K\), that child is eliminated from the circle. The counting then resumes with the next child (to the left) from \(1\> again. This process repeats until all children have been eliminated.

Given a sequence representing the order in which children are eliminated, your task is to determine the smallest positive integer \(K\) that produces the given elimination sequence.

Note: All formulas are in \(\LaTeX\) format.

inputFormat

The input consists of two lines:

  1. The first line contains an integer \(n\) representing the number of children.
  2. The second line contains \(n\) space-separated integers representing the elimination order.

outputFormat

Output a single integer which is the smallest value of \(K\) that produces the given elimination order.

sample

4
1 2 3 4
1