Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmCount numbers up to N that cannot be expressed as sum of...

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 = 10
Output: 4
Explanation: The integers that cannot be expressed as sum of two or more consecutive integers are {1, 2, 4, 8}. Therefore, the count of integers is 4.

Input: N = 100
Output: 7

Naive Approach: The given problem can be solved based on the observation that if a number is a power of two, then it cannot be expressed as a sum of consecutive numbers. Follow the steps below to solve the given problem:

  • Initialize a variable, say count that stores the count of numbers over the range [1, N] that cannot be expressed as a sum of two or more consecutive integers.
  • Iterate over the range [1, N], and if the number i is a perfect power of 2, then increment the value of count by 1.
  • After completing the above steps, print the value of count as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a number can
// be expressed as a power of 2
bool isPowerof2(unsigned int n)
{
    // f N is power of
    // two
    return ((n & (n - 1)) && n);
}
 
// Function to count numbers that
// cannot be expressed as sum of
// two or more consecutive +ve integers
void countNum(int N)
{
    // Stores the resultant
    // count of integers
    int count = 0;
 
    // Iterate over the range [1, N]
    for (int i = 1; i <= N; i++) {
 
        // Check if i is power of 2
        bool flag = isPowerof2(i);
 
        // Increment the count if i
        // is not power of 2
        if (!flag) {
            count++;
        }
    }
 
    // Print the value of count
    cout << count << "\n";
}
 
// Driver Code
int main()
{
    int N = 100;
    countNum(N);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if a number can
// be expressed as a power of 2
static boolean isPowerof2(int n)
{
     
    // f N is power of
    // two
    return ((n & (n - 1)) > 0 && n > 0);
}
 
// Function to count numbers that
// cannot be expressed as sum of
// two or more consecutive +ve integers
static void countNum(int N)
{
     
    // Stores the resultant
    // count of integers
    int count = 0;
 
    // Iterate over the range [1, N]
    for(int i = 1; i <= N; i++)
    {
         
        // Check if i is power of 2
        boolean flag = isPowerof2(i);
 
        // Increment the count if i
        // is not power of 2
        if (!flag)
        {
            count++;
        }
    }
 
    // Print the value of count
    System.out.print(count + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 100;
     
    countNum(N);
}
}
 
// This code is contributed by shikhasingrajput


Python3




# Python3 program for the above approach
 
# Function to check if a number can
# be expressed as a power of 2
def isPowerof2(n):
    # if N is power of
    # two
    return ((n & (n - 1)) and n)
 
# Function to count numbers that
# cannot be expressed as sum of
# two or more consecutive +ve integers
def countNum(N):
    # Stores the resultant
    # count of integers
    count = 0
 
    # Iterate over the range [1, N]
    for i in range(1, N + 1):
       
        # Check if i is power of 2
        flag = isPowerof2(i)
 
        # Increment the count if i
        # is not power of 2
        if (not flag):
            count += 1
 
    # Print the value of count
    print(count)
 
# Driver Code
if __name__ == '__main__':
 
    N = 100
    countNum(N)
 
# This code is contributed by mohit kumar 29.


C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if a number can
// be expressed as a power of 2
static bool isPowerof2(int n)
{
     
    // f N is power of
    // two
    return ((n & (n - 1)) > 0 && n > 0);
}
 
// Function to count numbers that
// cannot be expressed as sum of
// two or more consecutive +ve integers
static void countNum(int N)
{
     
    // Stores the resultant
    // count of integers
    int count = 0;
 
    // Iterate over the range [1, N]
    for(int i = 1; i <= N; i++)
    {
         
        // Check if i is power of 2
        bool flag = isPowerof2(i);
 
        // Increment the count if i
        // is not power of 2
        if (!flag)
        {
            count++;
        }
    }
 
    // Print the value of count
    Console.Write(count + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 100;
     
    countNum(N);
}
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
 
// JavaScript program for the above approach
 
// Function to check if a number can
// be expressed as a power of 2
function isPowerof2(n)
{
    // f N is power of
    // two
    return ((n & (n - 1)) && n);
}
  
// Function to count numbers that
// cannot be expressed as sum of
// two or more consecutive +ve integers
function countNum(N)
{
    // Stores the resultant
    // count of integers
    let count = 0;
  
    // Iterate over the range [1, N]
    for (let i = 1; i <= N; i++) {
  
        // Check if i is power of 2
        let flag = isPowerof2(i);
  
        // Increment the count if i
        // is not power of 2
        if (!flag) {
            count++;
        }
    }
  
    // Print the value of count
    document.write(count + "\n");
}
      
// Driver code
 
    let N = 100;
    countNum(N);
 
// This code is contributed by souravghosh0416.
</script>


Output: 

7

 

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

Efficient Approach: The above approach can also be optimized based on the observation that the integers which are not powers of 2, except for 2, can be expressed as the sum of two or more consecutive positive integers. Therefore, the count of such integers over the range [1, N] is given by (log2 N + 1).

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count numbers that
// cannot be expressed as sum of
// two or more consecutive +ve integers
void countNum(int N)
{
    // Stores the count
// of such numbers
    int ans = log2(N) + 1;
 
    cout << ans << "\n";
}
 
// Driver Code
int main()
{
    int N = 100;
    countNum(N);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class GFG {
 
    // Function to count numbers that
    // cannot be expressed as sum of
    // two or more consecutive +ve integers
    static void countNum(int N)
    {
       
        // Stores the count
        // of such numbers
        int ans = (int)(Math.log(N) / Math.log(2)) + 1;
 
        System.out.println(ans);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 100;
        countNum(N);
    }
}
 
// This code is contributed by Kingash.


Python3




# Python 3 program for the above approach
import math
 
# Function to count numbers that
# cannot be expressed as sum of
# two or more consecutive +ve integers
def countNum(N):
 
    # Stores the count
        # of such numbers
    ans = int(math.log2(N)) + 1
 
    print(ans)
 
# Driver Code
if __name__ == "__main__":
 
    N = 100
    countNum(N)
 
    # This code is contributed by ukasp.


C#




// C# program for the above approach
using System;
 
class GFG{
     
// Function to count numbers that
// cannot be expressed as sum of
// two or more consecutive +ve integers
static void countNum(int N)
{
     
    // Stores the count
    // of such numbers
    int ans = (int)(Math.Log(N) /
                    Math.Log(2)) + 1;
 
   Console.WriteLine(ans);
}
 
// Driver Code
static void Main()
{
    int N = 100;
    countNum(N);
}
}
 
// This code is contributed by SoumikMondal.


Javascript




<script>
 
// Javascript program for the above approach
 
 
// Function to count numbers that
// cannot be expressed as sum of
// two or more consecutive +ve integers
function countNum(N)
{
   
    // Stores the count
    // of such numbers
    var ans = parseInt(Math.log(N) / Math.log(2)) + 1;
 
    document.write(ans);
}
 
// Driver Code
var N = 100;
countNum(N);
 
// This code contributed by shikhasingrajput
 
</script>


Output: 

7

 

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments