Count integers up to N which can be represented as sum of two or more consecutive numbers
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++
#include <bits/stdc++.h>
using namespace std;
bool isPossible( int N)
{
return ((N & (N - 1)) && N);
}
void countElements( int N)
{
int count = 0;
for ( int i = 1; i <= N; i++) {
if (isPossible(i))
count++;
}
cout << count;
}
int main()
{
int N = 15;
countElements(N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int isPossible( int N)
{
return (((N & (N - 1 )) & N));
}
static void countElements( int N)
{
int count = 0 ;
for ( int i = 1 ; i <= N; i++)
{
if (isPossible(i) != 0 )
count++;
}
System.out.println(count);
}
public static void main(String[] args)
{
int N = 15 ;
countElements(N);
}
}
|
Python3
def isPossible(N):
return ((N & (N - 1 )) and N)
def countElements(N):
count = 0
for i in range ( 1 , N + 1 ):
if (isPossible(i)):
count + = 1
print (count)
if __name__ = = '__main__' :
N = 15
countElements(N)
|
C#
using System;
class GFG
{
static int isPossible( int N)
{
return (((N & (N - 1)) & N));
}
static void countElements( int N)
{
int count = 0;
for ( int i = 1; i <= N; i++)
{
if (isPossible(i) != 0)
count++;
}
Console.Write(count);
}
static public void Main()
{
int N = 15;
countElements(N);
}
}
|
Javascript
<script>
function isPossible(N) {
return (((N & (N - 1)) & N));
}
function countElements(N) {
var count = 0;
for (i = 1; i <= N; i++) {
if (isPossible(i) != 0)
count++;
}
document.write(count);
}
var N = 15;
countElements(N);
</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++
#include <bits/stdc++.h>
using namespace std;
void countElements( int N)
{
int Cur_Ele = 1;
int Count = 0;
while (Cur_Ele <= N) {
Count++;
Cur_Ele = Cur_Ele * 2;
}
cout << N - Count;
}
int main()
{
int N = 15;
countElements(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void countElements( int N)
{
int Cur_Ele = 1 ;
int Count = 0 ;
while (Cur_Ele <= N)
{
Count++;
Cur_Ele = Cur_Ele * 2 ;
}
System.out.print(N - Count);
}
public static void main(String[] args)
{
int N = 15 ;
countElements(N);
}
}
|
Python3
def countElements(N):
Cur_Ele = 1
Count = 0
while (Cur_Ele < = N):
Count + = 1
Cur_Ele = Cur_Ele * 2
print (N - Count)
if __name__ = = '__main__' :
N = 15
countElements(N)
|
C#
using System;
public class GFG
{
static void countElements( int N)
{
int Cur_Ele = 1;
int Count = 0;
while (Cur_Ele <= N)
{
Count++;
Cur_Ele = Cur_Ele * 2;
}
Console.Write(N - Count);
}
public static void Main(String[] args)
{
int N = 15;
countElements(N);
}
}
|
Javascript
<script>
function countElements(N)
{
var Cur_Ele = 1;
var Count = 0;
while (Cur_Ele <= N)
{
Count++;
Cur_Ele = Cur_Ele * 2;
}
document.write(N - Count);
}
var N = 15;
countElements(N);
</script>
|
Time Complexity: O(log(N))
Auxiliary Space: O(1)
Last Updated :
17 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...