Count integers up to N which can be represented as sum of two or more consecutive numbers
Last Updated :
23 Jul, 2025
Given a positive integer N, the task is to count the number of integers upto N which can be represented as the sum of two or more consecutive numbers.
Examples:
Input: N = 7
Output: 4
Explanation: In the range [1, 7]: {3, 5, 6, 7} can be represented as sum of consecutive numbers.
- 3 = 1 + 2
- 5 = 2 + 3
- 6 = 1 + 2 + 3
- 7 = 3 + 4
Input: N = 15
Output: 11
Naive Approach: Follow the steps below to solve the problem:
- Iterate over all integers in the range [1, N] and for each integer, check if it can be represented as the sum of two or more consecutive integers or not.
- To check whether a number can be expressed as the sum of two or more consecutive numbers or not, refer to this article.
Below is the implementation of the above approach:
C++
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if the number N
// can be expressed as sum of 2 or
// more consecutive numbers or not
bool isPossible(int N)
{
return ((N & (N - 1)) && N);
}
// Function to count integers in the range
// [1, N] that can be expressed as sum of
// 2 or more consecutive numbers
void countElements(int N)
{
// Stores the required count
int count = 0;
for (int i = 1; i <= N; i++) {
if (isPossible(i))
count++;
}
cout << count;
}
// Driver Code
int main()
{
int N = 15;
countElements(N);
return 0;
}
Java
// Java program of the above approach
import java.io.*;
class GFG
{
// Function to check if the number N
// can be expressed as sum of 2 or
// more consecutive numbers or not
static int isPossible(int N)
{
return (((N & (N - 1)) & N));
}
// Function to count integers in the range
// [1, N] that can be expressed as sum of
// 2 or more consecutive numbers
static void countElements(int N)
{
// Stores the required count
int count = 0;
for (int i = 1; i <= N; i++)
{
if (isPossible(i) != 0)
count++;
}
System.out.println(count);
}
// Driver Code
public static void main(String[] args)
{
int N = 15;
countElements(N);
}
}
// This code is contributed by jana_sayantan.
Python3
# Python3 Program to implement
# the above approach
# Function to check if the number N
# can be expressed as sum of 2 or
# more consecutive numbers or not
def isPossible(N):
return ((N & (N - 1)) and N)
# Function to count integers in the range
# [1, N] that can be expressed as sum of
# 2 or more consecutive numbers
def countElements(N):
# Stores the required count
count = 0
for i in range(1, N + 1):
if (isPossible(i)):
count += 1
print (count)
# Driver Code
if __name__ == '__main__':
N = 15
countElements(N)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG
{
// Function to check if the number N
// can be expressed as sum of 2 or
// more consecutive numbers or not
static int isPossible(int N)
{
return (((N & (N - 1)) & N));
}
// Function to count integers in the range
// [1, N] that can be expressed as sum of
// 2 or more consecutive numbers
static void countElements(int N)
{
// Stores the required count
int count = 0;
for (int i = 1; i <= N; i++)
{
if (isPossible(i) != 0)
count++;
}
Console.Write(count);
}
// Driver Code
static public void Main()
{
int N = 15;
countElements(N);
}
}
// This code is contributed by code_hunt.
JavaScript
<script>
// JavaScript program of the above approach
// Function to check if the number N
// can be expressed as sum of 2 or
// more consecutive numbers or not
function isPossible(N) {
return (((N & (N - 1)) & N));
}
// Function to count integers in the range
// [1, N] that can be expressed as sum of
// 2 or more consecutive numbers
function countElements(N) {
// Stores the required count
var count = 0;
for (i = 1; i <= N; i++) {
if (isPossible(i) != 0)
count++;
}
document.write(count);
}
// Driver Code
var N = 15;
countElements(N);
// This code contributed by Rajput-Ji
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, follow the steps below to solve the problem
- All numbers except the ones which are powers of 2 can be expressed as a sum of consecutive numbers.
- Count the number of powers of 2 ? N and store it in a variable, k say Count
- Print (N - Count) as the required answer.
Below is the implementation of the above approach:
C++
// C++ implementation
// of the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count integers in the range
// [1, N] that can be expressed as sum of
// 2 or more consecutive numbers
void countElements(int N)
{
int Cur_Ele = 1;
int Count = 0;
// Count powers of 2 up to N
while (Cur_Ele <= N) {
// Increment count
Count++;
// Update current power of 2
Cur_Ele = Cur_Ele * 2;
}
cout << N - Count;
}
// Driver Code
int main()
{
int N = 15;
countElements(N);
return 0;
}
Java
// Java implementation
// of the above approach
import java.util.*;
class GFG
{
// Function to count integers in the range
// [1, N] that can be expressed as sum of
// 2 or more consecutive numbers
static void countElements(int N)
{
int Cur_Ele = 1;
int Count = 0;
// Count powers of 2 up to N
while (Cur_Ele <= N)
{
// Increment count
Count++;
// Update current power of 2
Cur_Ele = Cur_Ele * 2;
}
System.out.print(N - Count);
}
// Driver Code
public static void main(String[] args)
{
int N = 15;
countElements(N);
}
}
// This code is contributed by shikhasingrajput
Python3
# python 3 implementation
# of the above approach
# Function to count integers in the range
# [1, N] that can be expressed as sum of
# 2 or more consecutive numbers
def countElements(N):
Cur_Ele = 1
Count = 0
# Count powers of 2 up to N
while (Cur_Ele <= N):
# Increment count
Count += 1
# Update current power of 2
Cur_Ele = Cur_Ele * 2
print(N - Count)
# Driver Code
if __name__ == '__main__':
N = 15
countElements(N)
C#
// C# implementation
// of the above approach
using System;
public class GFG
{
// Function to count integers in the range
// [1, N] that can be expressed as sum of
// 2 or more consecutive numbers
static void countElements(int N)
{
int Cur_Ele = 1;
int Count = 0;
// Count powers of 2 up to N
while (Cur_Ele <= N)
{
// Increment count
Count++;
// Update current power of 2
Cur_Ele = Cur_Ele * 2;
}
Console.Write(N - Count);
}
// Driver Code
public static void Main(String[] args)
{
int N = 15;
countElements(N);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript implementation
// of the above approach
// Function to count integers in the range
// [1, N] that can be expressed as sum of
// 2 or more consecutive numbers
function countElements(N)
{
var Cur_Ele = 1;
var Count = 0;
// Count powers of 2 up to N
while (Cur_Ele <= N)
{
// Increment count
Count++;
// Update current power of 2
Cur_Ele = Cur_Ele * 2;
}
document.write(N - Count);
}
// Driver Code
var N = 15;
countElements(N);
// This code is contributed by todaysgaurav
</script>
Time Complexity: O(log(N))
Auxiliary Space: O(1)
Similar Reads
Number of ways in which N can be represented as the sum of two positive integers Given a number N, the task is to find the number of unique ways in which N can be represented as a sum of two positive integers.Examples: Input: N = 7 Output: 3 (1 + 6), (2 + 5) and (3 + 4).Input: N = 200 Output: 100 Approach: The number of ways in which the number can be expressed as the sum of two
3 min read
Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers Given a positive integer N, the task is to find the count of integers from the range [1, N] such that the integer cannot be expressed as sum of two or more consecutive positive integers. Examples: Input: N = 10Output: 4Explanation: The integers that cannot be expressed as sum of two or more consecut
8 min read
Count prime numbers up to N that can be represented as a sum of two prime numbers Given a positive integer N, the task is to find the count of prime numbers less than or equal to N that can be represented as a sum of two prime numbers. Examples: Input: N = 6Output: 1Explanation:5 is the only prime number over the range [1, 6] that can be represented as sum of 2 prime numbers i.e.
15+ min read
Count array elements that can be represented as sum of at least two consecutive array elements Given an array A[] consisting of N integers from a range [1, N], the task is to calculate the count of array elements (non-distinct) that can be represented as the sum of two or more consecutive array elements. Examples: Input: a[] = {3, 1, 4, 1, 5, 9, 2, 6, 5}Output: 5Explanation:The array elements
11 min read
Count array elements that can be represented as sum of at least two consecutive array elements Given an array A[] consisting of N integers from a range [1, N], the task is to calculate the count of array elements (non-distinct) that can be represented as the sum of two or more consecutive array elements. Examples: Input: a[] = {3, 1, 4, 1, 5, 9, 2, 6, 5}Output: 5Explanation:The array elements
11 min read
Count of ways in which N can be represented as sum of Fibonacci numbers without repetition Given a number N, the task is to find the number of ways in which the integer N can be represented as a sum of Fibonacci numbers without repetition of any Fibonacci number. Examples: Input: N = 13Output: 3Explanation: The possible ways to select N as 13 are: {13} {8, 5} {8, 3, 2}. Note that it is no
10 min read