I'm not a math-guy really (a programmer actually) and this is my first question. And excuse me if I can not explain my problem good.
The problem is I have a sequence of integers and I want to detect if they are consecutive or not. e.g.
a1 = {1, 2, 3, 4} (* consecutive *)
a2 = {0, 1} (* consecutive *)
a3 = {0, 1, 2, 4} (* non-consecutive *)
a4 = {3, 4, 5, 6} (* consecutive *)
a5 = {3, 5, 6, 7} (* non-consecutive *)
a6 = {2, 3, 4} (* consecutive *)
The numbers come from a list's indexes that a user selected in UI. Actually I want to see if he selects a consecutive rows or not. Is there any way (sure there is :)) to check this without walking through all items in the list? I mean for example by using the first and last number in the list etc?
Update:
Note:
The numbers:
- they are integers,
- they are ascending,
- no repeats
PermutationQAlt[e_List] := Sort[e - Min[e] + 1] === Range@Length@e
(see here).PermutationQAlt /@ {a1, a2, a3, a4, a5, a6, {1, 3, 3, 3, 5}, {1, 1, 4, 4, 5}}
gives {True, True, False, True, False, True, False, False} $\endgroup$