Print all possible sums of consecutive numbers with sum N

2.3

Given a number N. The task is to print all possible sums of consecutive numbers that add up to N.

Examples:

Input : 100
Output :
9 10 11 12 13 14 15 16 
18 19 20 21 22 

Input :125
Output :
8 9 10 11 12 13 14 15 16 17 
23 24 25 26 27 
62 63 

One important fact is we can not find consecutive numbers above N/2 that adds up to N, because N/2 + (N/2 + 1) would be more than N. So we start from start = 1 till end = N/2 and check for every consecutive sequence whether it adds up to N or not. If it is then we print that sequence and start looking for the next sequence by incrementing start point.

// C++ program to print consecutive sequences
// that add to a given value
#include<bits/stdc++.h>
using namespace std;

void findConsecutive(int N)
{
    // Note that we don't ever have to sum
    // numbers > ceil(N/2)
    int start = 1, end = (N+1)/2;

    // Repeat the loop from bottom to half
    while (start < end)
    {
        // Check if there exist any sequence
        // from bottom to half which adds up to N
        int sum = 0;
        for (int i = start; i <= end; i++)
        {
            sum = sum + i;

            // If sum = N, this means consecutive
            // sequence exists
            if (sum == N)
            {
                // found consecutive numbers! print them
                for (int j = start; j <= i; j++)
                    printf("%d ", j);

                printf("\n");
                break;
            }

            // if sum increases N then it can not exist
            // in the consecutive sequence starting from
            // bottom
            if (sum > N)
                break;
        }
        sum = 0;
        start++;
    }
}

// Driver code
int main(void)
{
    int N = 125;
    findConsecutive(N);
    return 0;
}

Output:

8 9 10 11 12 13 14 15 16 17 
23 24 25 26 27 
62 63 

Optimized Solution:
In above solution, we keep recalculating sums from start to end, which results in O(N^2) worst case time complexity. This can be avoided by using a precomputed array of sums, or better yet – just keeping track of the sum you have so far and adjusting it depending on how it compares to the desired sum.

Time complexity of below code is O(N).

// Optimized C++ program to find sequences of all consecutive
// numbers with sum equal to N
#include <stdio.h>

void printSums(int N)
{
    int start = 1, end = 1;
    int sum = 1;

    while (start <= N/2)
    {
        if (sum < N)
        {
            end += 1;
            sum += end;
        }
        else if (sum > N)
        {
            sum -= start;
            start += 1;
        }
        else if (sum == N)
        {
            for (int i = start; i <= end; ++i)
                printf("%d ", i);

            printf("\n");
            sum -= start;
            start += 1;
        }
    }
}

// Driver Code
int main()
{
    printSums(125);
    return 0;
}

Output:

8 9 10 11 12 13 14 15 16 17 
23 24 25 26 27 
62 63 

Reference :
https://www.careercup.com/page?pid=microsoft-interview-questions&n=2

This article is contributed by Niteesh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2.3 Average Difficulty : 2.3/5.0
Based on 6 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.