Find the longest subsequence where differences alternate between positive and negative. For , the answer is (the whole array works: differences are ). For , answer is . Count peaks and valleys in the sequence. Tricky: the subsequence does not need to be contiguous.
You can skip elements to maintain the alternating pattern. Flat segments (consecutive equal values) do not contribute to the wiggle because their difference is zero. The longest wiggle is the count of direction changes plus one.