Given a number N. The task is to print all possible consecutive numbers that add up to N.
Examples :
Input: N = 100
Output:
9 10 11 12 13 14 15 16
18 19 20 21 22
Input: N = 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++
#include<bits/stdc++.h>
using namespace std;
void findConsecutive( int N)
{
int start = 1, end = (N+1)/2;
while (start < end)
{
int sum = 0;
for ( int i = start; i <= end; i++)
{
sum = sum + i;
if (sum == N)
{
for ( int j = start; j <= i; j++)
cout << " " << j;
cout << "\n" ;
break ;
}
if (sum > N)
break ;
}
sum = 0;
start++;
}
}
int main( void )
{
int N = 125;
findConsecutive(N);
return 0;
}
|
C
#include<stdio.h>
void findConsecutive( int N)
{
int start = 1, end = (N+1)/2;
while (start < end)
{
int sum = 0;
for ( int i = start; i <= end; i++)
{
sum = sum + i;
if (sum == N)
{
for ( int j = start; j <= i; j++)
printf ( "%d " , j);
printf ( "\n" );
break ;
}
if (sum > N)
break ;
}
sum = 0;
start++;
}
}
int main( void )
{
int N = 125;
findConsecutive(N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void findConsecutive( int N)
{
int start = 1 ;
int end = (N + 1 ) / 2 ;
while (start < end)
{
int sum = 0 ;
for ( int i = start; i <= end; i++)
{
sum = sum + i;
if (sum == N)
{
for ( int j = start; j <= i; j++)
System.out.print(j + " " );
System.out.println();
break ;
}
if (sum > N)
break ;
}
sum = 0 ;
start++;
}
}
public static void main (String[] args)
{
int N = 125 ;
findConsecutive(N);
}
}
|
Python3
def findConsecutive(N):
start = 1
end = (N + 1 ) / / 2
while (start < end):
Sum = 0
for i in range (start, end + 1 ):
Sum = Sum + i
if ( Sum = = N):
for j in range (start, i + 1 ):
print (j, end = " " )
print ()
break
if ( Sum > N):
break
Sum = 0
start + = 1
N = 125
findConsecutive(N)
|
C#
using System;
class GFG
{
static void findConsecutive( int N)
{
int start = 1;
int end = (N + 1) / 2;
while (start < end)
{
int sum = 0;
for ( int i = start; i <= end; i++)
{
sum = sum + i;
if (sum == N)
{
for ( int j = start; j <= i; j++)
Console.Write(j + " " );
Console.WriteLine();
break ;
}
if (sum > N)
break ;
}
sum = 0;
start++;
}
}
static public void Main ()
{
int N = 125;
findConsecutive(N);
}
}
|
PHP
<?php
function findConsecutive( $N )
{
$start = 1;
$end = ( $N + 1) / 2;
while ( $start < $end )
{
$sum = 0;
for ( $i = $start ; $i <= $end ; $i ++)
{
$sum = $sum + $i ;
if ( $sum == $N )
{
for ( $j = $start ; $j <= $i ; $j ++)
echo $j , " " ;
echo "\n" ;
break ;
}
if ( $sum > $N )
break ;
}
$sum = 0;
$start ++;
}
}
$N = 125;
findConsecutive( $N );
?>
|
Javascript
<script>
function findConsecutive( N)
{
let start = 1, end = Math.trunc((N+1)/2);
while (start < end)
{
let sum = 0;
for (let i = start; i <= end; i++)
{
sum = sum + i;
if (sum == N)
{
for (let j = start; j <= i; j++)
document.write(j+ " " );
document.write( "<br/>" );
break ;
}
if (sum > N)
break ;
}
sum = 0;
start++;
}
}
let N = 125;
findConsecutive(N);
</script>
|
Output :
8 9 10 11 12 13 14 15 16 17
23 24 25 26 27
62 63
Optimized Solution:
In the 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).
C++
#include <bits/stdc++.h>
using namespace std;
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)
cout << " " << i;
cout << "\n" ;
sum -= start;
start += 1;
}
}
}
int main()
{
printSums(125);
return 0;
}
|
C
#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;
}
}
}
int main()
{
printSums(125);
return 0;
}
|
Java
import java.io.*;
class GFG {
static 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)
System.out.print(i
+ " " );
System.out.println();
sum -= start;
start += 1 ;
}
}
}
public static void main (String[] args)
{
printSums( 125 );
}
}
|
Python3
def findConsecutive(N):
start = 1
end = 1
sum = 1
while start < = N / 2 :
if sum < N:
end + = 1
sum + = end
if sum > N:
sum - = start
start + = 1
if sum = = N:
for i in range (start, end + 1 ):
print (i, end = ' ' )
print ( )
sum - = start
start + = 1
N = 125
findConsecutive(N)
|
C#
using System;
class GFG {
static 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)
Console.Write(i
+ " " );
Console.WriteLine();
sum -= start;
start += 1;
}
}
}
public static void Main ()
{
printSums(125);
}
}
|
PHP
<?php
function printSums( $N )
{
$start = 1; $end = 1;
$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 ( $i = $start ; $i <= $end ; ++ $i )
echo $i , " " ;
echo "\n" ;
$sum -= $start ;
$start += 1;
}
}
}
printSums(125);
?>
|
Javascript
<script>
function printSums(N)
{
let start = 1, end = 1;
let 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 (let i = start;
i <= end; ++i)
document.write(i + " " );
document.write( "<br/>" );
sum -= start;
start += 1;
}
}
}
printSums(125);
</script>
|
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 write.geeksforgeeks.org or mail your article to review-team@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.