Given an array of values, I want to determine if its an increasing or decreasing array. Of course this is trivial if we want to detect only Monotone Increasing/Decreasing. But if we have an array like this:
3, 3.2, 3.4, 3.3, 3.4, 3.7, 4.2, 4.8
It is indeed an increasing array, but not monotone because for i=3 we have 3.3<3.4
Also, a single check between first and last index is not an option for me, as I may be working with angles, and I could complete more than a single circle:
270º, 290º, 315º, 345º, 5º
I would say that a clockwise is increasing too, despite 5º<270º
Also, if I’m defining angles from 0 to 180 and then -180 to 0 instead of 0 to 360, this is again a problem:
170º, 175º, 180º, -175º, -170º is again increasing for me.
I want to write this for C++, but what matters to me is the algorithm to use. Any idea?
Use linear regression. Based on the resulting slope you can decide whether it’s increasing or decreasing.
As for the cyclic case: I would simply move the “negative” elements in front of the list and then do the test…
Note: for special cases like
5 4 3 2 1 1000this will still tell you that it’s increasing, but it’s hard to give an exact answer for your question as the definition of “increasing” is quite fuzzy. (You might want to filter out some crazy values?)Another simple approach would be to count the number of increases and decreases between adjacent elements, but in this case
10 11 12 13 14 0 1 2 3 4would be clearly increasing (which might be the right answer, it’s hard to tell..)Without knowing more of what you’re trying to achieve it’s hard to give a good answer.