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);
}