@Override
    public boolean hasNext() {
        // check if hasNext was called before to follow idempotency
        if (hasNextCalled)
            return hasNext;
        
        hasNextCalled = true;
        if (n == 0)
            return false;
        if (!started){
            // if not started, generate first subset
            for (int i = 0; (subset[i] = i) < k - 1; i++);
            started = true;
            hasNext = true;
            return true;
        } else {
            // check if more subsets can be generated
            int i;
            for (i = k - 1; i >= 0 && subset[i] == n - k + i; i--);
            if (i < 0){
                hasNext = false;
                return false;
            } else {
                subset[i]++;
                for (++i; i < k; i++)
                    subset[i] = subset[i - 1] + 1;
                hasNext = true;
                return true;
            }
        }
    }