Anti-Arithmetic Permutation
InterviewTime limit1sMemory limit128 MB
For each permutation of 0 to n-1, decide whether any three terms in order form an arithmetic progression.
- Level
Easy2 of 10
- Topics
- Brute force, Math
- Solved
- No attempts yet
Problem
A permutation of the integers is anti-arithmetic when no three of its terms form an arithmetic series. That is, there are no three indices for which is an arithmetic series, which happens exactly when , or equivalently .
For example, 3, 1, 0, 4, 2 is an anti-arithmetic permutation of . The sequence 0, 5, 4, 3, 1, 2 is not anti-arithmetic. Its first, fifth and sixth terms 0, 1, 2 form an arithmetic series, and so do its second, fourth and fifth terms 5, 3, 1 and its second, third and fourth terms 5, 4, 3.
Given a permutation of length , decide whether it is anti-arithmetic.
Input
The first line contains an integer , the number of test cases.
Each test case consists of two lines. The first line contains an integer . The next line contains integers separated by a single space, a permutation of . is between 3 and 50 inclusive.
Output
For each test case, print one line in the format Case #x: M, where is the case number starting from 1 and is YES when the given permutation is anti-arithmetic and NO otherwise.