Count integers up to N which can be represented as sum of two or more consecutive numbers

• Difficulty Level : Medium
• Last Updated : 17 May, 2021

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:

1. 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.
2. 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 using namespace std; // Function to check if the number N// can be expressed as sum of 2 or// more consecutive numbers or notbool 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 numbersvoid countElements(int N){    // Stores the required count    int count = 0;     for (int i = 1; i <= N; i++) {         if (isPossible(i))            count++;    }    cout << count;}// Driver Codeint main(){    int N = 15;    countElements(N);     return 0;}

Java

 // Java program of the above approachimport java.io.*;class GFG{ // Function to check if the number N// can be expressed as sum of 2 or// more consecutive numbers or notstatic 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 numbersstatic 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 notdef 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 numbersdef countElements(N):       # Stores the required count    count = 0    for i in range(1, N + 1):        if (isPossible(i)):            count += 1     print (count)     # Driver Codeif __name__ == '__main__':    N = 15    countElements(N)     # This code is contributed by mohit kumar 29

C#

 // C# program for the above approachusing System;class GFG{ // Function to check if the number N// can be expressed as sum of 2 or// more consecutive numbers or notstatic 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 numbersstatic 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 Codestatic public void Main(){    int N = 15;    countElements(N);}} // This code is contributed by code_hunt.

Javascript


Output:
11

Time Complexity: O(N)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, follow the steps below to solve the problem

1. All numbers except the ones which are powers of 2 can be expressed as a sum of consecutive numbers.
2. Count the number of powers of 2 ≤ N and store it in a variable, k say Count
3. Print (N – Count) as the required answer.

Below is the implementation of the above approach:

C++

 // C++ implementation// of the above approach#include using namespace std; // Function to count integers in the range// [1, N] that can be expressed as sum of// 2 or more consecutive numbersvoid 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 Codeint main(){    int N = 15;    countElements(N);     return 0;}

Java

 // Java implementation// of the above approachimport 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 numbersdef 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 Codeif __name__ == '__main__':    N = 15    countElements(N)

C#

 // C# implementation// of the above approachusing 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


Output:
11

Time Complexity: O(log(N))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up