(yes I’ve searched as much as I can..)
Maybe I shouldn’t use sequence but there’s no better way to describe it..
[revision: As stated in the answer section, subsequence is not the right description.
A pair of numbers is the right words!]
Given a sequence of numbers and define the size of a pair of numbers from the sequence as the distance between the two numbers. Obviously the largest pair is [the first number, the last number]. The question is to find the largest pair of numbers with order opposite to the [first,last] pair.
For example, if the sequence is {1,6,3,5,2,8} then the answer should be
[6,2] because the order of the [1,8] is increasing, and the largest pair with decreasing order is [6,2].
A side question is, can this be solved in a declarative way using SQL like statements? Specifically I am thinking of using LINQ to do it.
Thanks.
It is not the sort of problem that is suitable for SQL. It is also poorly phrased. You are really looking for pairs of numbers rather than subsequences, and the size is the distance between the two.
It can be solved O(n) by scanning the sequence backwards, for the smallest number after a given point, and its position. This gives
{1,2,2,2,2,8)and{0,4,4,4,4,5}. Then scanning this in parallel with the original sequences gives sizes of{0,3,2,1,0,0}, so the largest size is for the pair(6,2), which has a size of 3.