public static boolean next_permutation(int[] vec) {
        int tmp;
        for (int i = vec.length - 2; i >= 0; i--)
            if (vec[i + 1] > vec[i])
                for (int j = vec.length - 1; j >= 0; j--)
                    if (vec[j] > vec[i]) {
                        tmp = vec[i];
                        vec[i] = vec[j];
                        vec[j] = tmp;

                        for (int k = i + 1, l = vec.length - 1; k < l; k++, l--) {
                            tmp = vec[k];
                            vec[k] = vec[l];
                            vec[l] = tmp;
                        }
                        return true;
                    }
        return false;
    }