Print n numbers that are not the sum of consecutive integers

Updated

Given an integer n, the task is to print N numbers such that N is not the sum of two or more consecutive integers.

Examples :

Input : n = 3
Output : 1 2 4
Explanation :
1, 2 and 4 can't be represented as sum of two or more consecutive
integers.
3 can be represented, 3 = 1 + 2.

Input : n = 4
Output : 1 2 4 8

Approach :

The powers of 2 can never be the sum of two or more consecutive integers. So, we will print the first n powers of 2.

Implementation :

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;

// Function to print n numbers such that
// N is not the sum of two or more consecutive integers
void findNumbers(int n)
{
    int i = 0;
    while (i < n) {

        // Print ith power of 2
        cout << pow(2, i) << " ";
        i++;
    }
}

// Driver code
int main()
{
    int n = 3;
    findNumbers(n);
}

Output :

1 2 4
Comment
Mithlesh Upadhyay Published 1 Dec 2026 · 1 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials