private boolean validSeam(int[] a, int max) {
        for (int i = 1; i < a.length; i++) {
            if (Math.abs(a[i - 1] - a[i]) > 1) {
                return false;
            }
            if (a[i] < 0 || a[i] > max) {
                return false;
            }
        }
        return true;
    }