Java implements a sequence of consecutive positive numbers whose sum is S

Output all sequences of consecutive positive numbers whose sum is S.

The sequence is in ascending order, and the starting numbers are in ascending order among the sequences ( Xiao Ming likes mathematics very much. One day, when he was doing his math homework, he asked to calculate the sum of 9~16, and he immediately wrote it correctly. The answer is 100. But he is not satisfied with this, he is thinking about how many kinds of consecutive positive numbers the sum is 100 (including at least two numbers). Before long, he got another set of consecutive positive numbers that sum to Sequence of 100: 18, 19, 20, 21, 22. Now hand the problem to you, can you also quickly find all consecutive positive sequences whose sum is S? Good Luck!)

code

    public static List<List<Integer>> findContinuesSequence(int target) {
        // Contains at least two numbers, so the minimum must be 3, so there is a combination of {1, 2} to satisfy
        if (target < 3) {
            return null;
        }
        // initialize two pointers
        int small = 1;
        int big = 2;
        // get half of the target value
        int middle = (target + 1) / 2;
        // save the result
        List<List<Integer>> result = Lists.newArrayList();
        // small < middel, because big is larger than small and exceeds middle, small+big > target
        // Equal to also not possible, small+big>target
        // For example, 14, middel=7, if small=7, big > small, small+big > target
        // so the starting point of all results should be to the left of middle
        int sum = small + big;
        while (small < middle) {
            /* This is my own way of writing
            int sum = 0;
            for (int i = small; i <= big; i++) {
                sum += i;
            }
            if (sum == target) {
                result.add(Lists.newArrayList(small, big));
                big++;
            } else if (sum > target) {
                small++;
            } else {
                big++;
            }
            */
            // If sum is greater than target, subtract the smaller value small, and move the pointer of small so that sum <= target
            // When sum <= target, end this while
            // When sum > target, reduce the range and move the small pointer. At this time, the part larger than the target needs to be subtracted
            // When sum = target, record the result and move the pointer of big to expand the search range
            // When sum < target, expand the range and move the pointer of big. At this time, a larger value is needed to make up the gap with target
            // eg target = 15, small=1, big = 2
            //     sum = 1+2 =3,           sum < target, small=1, big = 3
            //     sum = 1+2+3 = 6,        sum < target, small=1, big = 4
            //     sum = 1+2+3+4 = 10,     sum < target, small=1, big = 5
            // sum = 1+2+3+4+5 = 15, sum = target, find, expand search, small=1, big = 6
            //     sum = 1+2+3+4+5+6 = 21, sum > target, small=2, big = 6
            //     sum = 2+3+4+5+6 = 20,   sum > target, small=3, big = 6
            //     sum = 3+4+5+6 = 18,     sum > target, small=4, big = 6
            // sum = 4+5+6 = 15, sum = target, , find, expand search, small=4, big = 7
            //     sum = 4+5+6+7 = 22,     sum > target, small=5, big = 7
            //     sum = 5+6+7 = 18,       sum > target, small=6, big = 7
            //     sum = 6+7 = 13,         sum < target, small=6, big = 8
            //     sum = 6+7+8 = 21,       sum > target, small=7, big = 8
            // sum = 7+8 = 15, sum = target, , find, expand search, small=7, big = 9
            //     sum = 7+8+9 = 24,       sum > target, small=8, big = 9
            // middel = (15 + 1) / 2 = 8, if small < middle is not satisfied, exit
            while (sum > target && small < middle) {
                sum -= small;
                small++;
                if (sum == target) {
                    result.add(Lists.newArrayList(small, big));
                }
            }
            big++;
            sum += big;
        }
        return result;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325939772&siteId=291194637