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++
#include <bits/stdc++.h>
using namespace std;
bool isPowerof2(unsigned int n)
{
return ((n & (n - 1)) && n);
}
void countNum( int N)
{
int count = 0;
for ( int i = 1; i <= N; i++) {
bool flag = isPowerof2(i);
if (!flag) {
count++;
}
}
cout << count << "\n" ;
}
int main()
{
int N = 100;
countNum(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isPowerof2( int n)
{
return ((n & (n - 1 )) > 0 && n > 0 );
}
static void countNum( int N)
{
int count = 0 ;
for ( int i = 1 ; i <= N; i++)
{
boolean flag = isPowerof2(i);
if (!flag)
{
count++;
}
}
System.out.print(count + "\n" );
}
public static void main(String[] args)
{
int N = 100 ;
countNum(N);
}
}
|
Python3
def isPowerof2(n):
return ((n & (n - 1 )) and n)
def countNum(N):
count = 0
for i in range ( 1 , N + 1 ):
flag = isPowerof2(i)
if ( not flag):
count + = 1
print (count)
if __name__ = = '__main__' :
N = 100
countNum(N)
|
C#
using System;
class GFG{
static bool isPowerof2( int n)
{
return ((n & (n - 1)) > 0 && n > 0);
}
static void countNum( int N)
{
int count = 0;
for ( int i = 1; i <= N; i++)
{
bool flag = isPowerof2(i);
if (!flag)
{
count++;
}
}
Console.Write(count + "\n" );
}
public static void Main(String[] args)
{
int N = 100;
countNum(N);
}
}
|
Javascript
<script>
function isPowerof2(n)
{
return ((n & (n - 1)) && n);
}
function countNum(N)
{
let count = 0;
for (let i = 1; i <= N; i++) {
let flag = isPowerof2(i);
if (!flag) {
count++;
}
}
document.write(count + "\n" );
}
let N = 100;
countNum(N);
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
void countNum( int N)
{
int ans = log2(N) + 1;
cout << ans << "\n" ;
}
int main()
{
int N = 100;
countNum(N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
static void countNum( int N)
{
int ans = ( int )(Math.log(N) / Math.log( 2 )) + 1 ;
System.out.println(ans);
}
public static void main(String[] args)
{
int N = 100 ;
countNum(N);
}
}
|
Python3
import math
def countNum(N):
ans = int (math.log2(N)) + 1
print (ans)
if __name__ = = "__main__" :
N = 100
countNum(N)
|
C#
using System;
class GFG{
static void countNum( int N)
{
int ans = ( int )(Math.Log(N) /
Math.Log(2)) + 1;
Console.WriteLine(ans);
}
static void Main()
{
int N = 100;
countNum(N);
}
}
|
Javascript
<script>
function countNum(N)
{
var ans = parseInt(Math.log(N) / Math.log(2)) + 1;
document.write(ans);
}
var N = 100;
countNum(N);
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)