A box office keeps a chronological log of the ticket prices charged for a long-running show, prices[0..n-1]. For an upcoming audit, the finance team wants to check several stretches of that log: for each stretch [l, r] (inclusive), they ask whether the prices charged during that stretch could be re-sorted into a perfect price ladder — a sequence where each step up costs exactly the same fixed amount more than the one before it (a constant difference between consecutive sorted prices; a difference of zero, i.e. every price in the stretch being identical, also counts as a valid ladder). For every query, report whether that stretch's prices can be arranged into such a ladder.
Line 1: two integers n q — the number of logged prices and the number of queries. Line 2: n integers — prices[0], prices[1], ..., prices[n-1]. Next q lines: each line has two integers l r (0-indexed, inclusive) describing one query stretch.
Print q lines. For the i-th query, print "YES" if prices[l..r] can be rearranged into a sequence with a constant difference between consecutive elements, otherwise print "NO".
Example 1
Input
6 2 10 14 12 20 8 16 0 2 0 3
Expected
YES NO
Explanation
prices = [10,14,12,20,8,16]. Query [0,2] covers {10,14,12}; sorted this is [10,12,14], with consecutive differences 2 and 2 — constant, so the answer is "YES". Query [0,3] covers {10,14,12,20}; sorted this is [10,12,14,20], with differences 2, 2, 6 — not constant, so the answer is "NO".
Example 2
Input
4 1 5 5 5 5 1 3
Expected
YES
Explanation
The stretch [1,3] covers three prices, all equal to 5; sorted it stays [5,5,5], with consecutive differences 0 and 0. A constant (zero) difference still satisfies the ladder condition, so the answer is "YES".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →