Find nth Term of the Series 1 2 2 4 4 4 4 8 8 8 8 8 8 8 8 …
Last Updated :
01 Sep, 2022
Given a number n, the task is to find the nth term of the Series
1 2 2 4 4 4 4 8 8 8 8 8 8 8 8 …
Example:
Input: n = 9
Output: 8
Input: n = 1025
Output: 1024
Naive Approach:
- Run a loop from i = 0 to n
- Inside loop increment i by i+k
- Inside loop increment k by 2*k
- Run above loop while i is less than n
- Return k/2 as the result
Complexity: log(n)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getValue( int n)
{
int i = 0, k = 1;
while (i < n) {
i = i + k;
k = k * 2;
}
return k / 2;
}
int main( void )
{
int n = 9;
cout << getValue(n) << endl;
n = 1025;
cout << getValue(n) << endl;
}
|
Java
class GFG
{
static int getValue( int n)
{
int i = 0 , k = 1 ;
while (i < n) {
i = i + k;
k = k * 2 ;
}
return k / 2 ;
}
public static void main(String []args)
{
int n = 9 ;
System.out.println(getValue(n));
n = 1025 ;
System.out.println(getValue(n));
}
}
|
Python3
def getValue(n):
i = 0 ;
k = 1 ;
while (i < n):
i = i + k;
k = k * 2 ;
return int (k / 2 );
n = 9 ;
print (getValue(n));
n = 1025 ;
print (getValue(n));
|
C#
using System;
class GFG
{
static int getValue( int n)
{
int i = 0, k = 1;
while (i < n) {
i = i + k;
k = k * 2;
}
return k / 2;
}
public static void Main()
{
int n = 9;
Console.WriteLine(getValue(n));
n = 1025;
Console.WriteLine(getValue(n));
}
}
|
PHP
<?php
function getValue( $n )
{
$i = 0; $k = 1;
while ( $i < $n )
{
$i = $i + $k ;
$k = $k * 2;
}
return (int) $k / 2;
}
$n = 9;
echo getValue( $n ), "\n" ;
$n = 1025;
echo getValue( $n ), "\n" ;
?>
|
Javascript
<script>
function getValue(n)
{
let i = 0, k = 1;
while (i < n) {
i = i + k;
k = k * 2;
}
return parseInt(k / 2);
}
let n = 9;
document.write(getValue(n) + "<br>" );
n = 1025;
document.write(getValue(n) + "<br>" );
</script>
|
Time Complexity: O(n / k)
Auxiliary Space: O(1)
Efficient Approach: This Problem can be solved in O(1) time complexity.
Let nth term of the sequence be equal to 2m
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getValue( int n)
{
int result = ( floor )( log (n + 1) / log (2));
return pow (2, result);
}
int main( void )
{
int n = 9;
cout << getValue(n) << endl;
n = 1025;
cout << getValue(n) << endl;
}
|
Java
import java.lang.*;
import java.lang.Math;
import java.io.*;
class GFG {
static double getValue( double n)
{
double result =(Math.floor(Math.log(n + 1 ) / Math.log( 2 )));
return Math.pow( 2 , result);
}
public static void main (String[] args) {
double n = 9 ;
System.out.println (getValue(n));
n = 1025 ;
System.out.println (getValue(n));
}
}
|
Python3
import math
def getValue(n):
result = int (math.floor(math.log(n + 1 ) /
math.log( 2 )))
return int (math. pow ( 2 , result))
n = 9
print (getValue(n))
n = 1025
print (getValue(n))
|
C#
using System;
class GFG
{
static double getValue( double n)
{
double result =(Math.Floor(Math.Log(n + 1) / Math.Log(2)));
return Math.Pow(2, result);
}
public static void Main ()
{
double n = 9;
Console.WriteLine(getValue(n));
n = 1025;
Console.WriteLine (getValue(n));
}
}
|
PHP
<?php
function getValue( $n )
{
$result = (int)(log( $n + 1) / log(2));
return pow(2, $result );
}
$n = 9;
echo getValue( $n ), "\n" ;
$n = 1025;
echo getValue( $n ), "\n" ;
?>
|
Javascript
<script>
function getValue(n)
{
let result = Math.floor(Math.log(n + 1) / Math.log(2));
return Math.pow(2, result);
}
let n = 9;
document.write(getValue(n) + "<br>" );
n = 1025;
document.write(getValue(n));
</script>
|
Time complexity: O(logn) for given number n
Space complexity: O(1) since using constant variables
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...