Check if a string has variable elements

Hi there!
I need some help here.
How can i see if this string (LE: ARRAY) (1 -5 -3 2 -1 7 -2 5 1 7 -9 0 -1 6 -1 -8) has a subARRAY that alternates (+ - + -)or vice versa (- + - +) and how can i cout it?
For exemple there are 2 subARRAY with 6 elements:
(3 2 -1 7 -2 5) and (7 -9 0 -1 6 -1) and i have to cout the last subARRAY.


Thx
Last edited on
Could you find a subarray from array of numbers?
regx may work, but it may also be overkill.
otherwise, just loop and detect/count. A little finite state machine, or just brute force multi variable boolean trackers in a loop.
do you care for sub-subs? eg if you had a string of 6, that contains 2 or 3 internal strings that also match (first 4, skip 2, next 4, and in the middle of that, the inverse string as well)
Last edited on
Just work along the array from left to right, noting the length of an alternating sequence that ends at each point. For your example it goes:
1,2,1,2,3,4,5,6,1,1,2,3,4,5,6,1
You can keep track of the largest number as you go along, replacing when greater than or equal to previous. Thus you end up with the six positions ending at the last 6.
Topic archived. No new replies allowed.