sclass MountainsAndValleys { double[] values; new IntBuffer highs; new IntBuffer lows; run { int n = l(values); if (n == 0) ret; highs.add(values[0]); lows.add(values[0]); int i = 1; while (i < n) { // find plateau var value = values[i]; int j = i+1; while (j < n && values[j] == value) ++j; // are we going up or down? var last = values[i-1]; if (value > last) highs.set(l(highs)-1, i); // replace last high else lows.set(l(lows)-1, i); // replace last low } } }