Untitled

mail@pastecode.io avatar
unknown
java
2 months ago
1.0 kB
3
Indexable
Never
    public static int[] findMaxMonotonicSubsequence(int[] source) {
        if(source.length == 1) return new int[] {0,0};
        var previous = source[0];
        var current = 0;
        var max = 0;
        var isReduced = source[0] > source[1];
        var result = new int[2];
        for(var i = 1; i < source.length; i++) {
            var el = source[i];
            if(previous == el) {
                current = 0;
                continue;
            }
            if(previous > el) {
                if(!isReduced) {
                    current = 1;
                    isReduced = true;
                } else current++;
            } else  {
                if(isReduced) {
                    current = 1;
                    isReduced = false;
                } else current ++;
            }
            previous = el;
            if(current > max) {
                max = current;
                result[0] = (i - current);
                result[1] = i ;
            }
        }
        return result;
    }
Leave a Comment