Program to find Nth term in the series 0, 0, 2, 1, 4, 2, 6, 3, 8,…
Given a number N. The task is to write a program to find the N-th term in the below series:
0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8,…..
Examples:
Input : N = 10
Output : 4
Input : N = 7
Output : 6
On observing carefully, you will find that the series is a mixture of 2 series:
- Terms at odd positions in the given series form the series of even numbers in increasing order starting from 0. Like, 0,2,4,6,..
- Terms at even positions in the given series are derived from the previous term using the formula (previousTerm/2). That is, the terms at even positions are half of their previous term.
Now, it is known that every odd positioned term forms an even series starting from 0 and every even positioned term is the half of the previous odd positioned term.
Therefore first check whether the input number N is even or odd. If it is odd, set N=(N/2) + 1(since there are Two series running parallelly) and find the Nth term by using formula 2*(N-1) ( N-1 because the series starts with 0).
Similarly, if N is even, set N = N/2, use the previous formula and divide the answer by 2.
Below is the implementation of above approach:
C++
#include <iostream>
#include <math.h>
using namespace std;
void findNthTerm( int n)
{
if (n % 2 == 0) {
n = n / 2;
n = 2 * (n - 1);
cout << n / 2 << endl;
}
else {
n = (n / 2) + 1;
n = 2 * (n - 1);
cout << n << endl;
}
}
int main()
{
int X = 10;
findNthTerm(X);
X = 7;
findNthTerm(X);
return 0;
}
|
Java
class GFG
{
static void findNthTerm( int n)
{
if (n % 2 == 0 )
{
n = n / 2 ;
n = 2 * (n - 1 );
System.out.println(n / 2 );
}
else
{
n = (n / 2 ) + 1 ;
n = 2 * (n - 1 );
System.out.println(n);
}
}
public static void main(String args[])
{
int X = 10 ;
findNthTerm(X);
X = 7 ;
findNthTerm(X);
}
}
|
Python 3
def findNthTerm(n):
if (n % 2 = = 0 ):
n = n / / 2
n = 2 * (n - 1 )
print ( n / / 2 )
else :
n = (n / / 2 ) + 1
n = 2 * (n - 1 )
print (n)
if __name__ = = "__main__" :
X = 10
findNthTerm(X);
X = 7 ;
findNthTerm(X)
|
C#
using System;
class GFG
{
static void findNthTerm( int n)
{
if (n % 2 == 0)
{
n = n / 2;
n = 2 * (n - 1);
Console.Write(n / 2);
}
else
{
n = (n / 2) + 1;
n = 2 * (n - 1);
Console.Write(n);
}
}
public static void Main()
{
int X = 10;
findNthTerm(X);
Console.Write( "\n" );
X = 7;
findNthTerm(X);
}
}
|
PHP
<?php
function findNthTerm( $n )
{
if ( $n % 2 == 0)
{
$n = $n / 2;
$n = 2 * ( $n - 1);
echo $n / 2 . "\n" ;
}
else
{
$n = (int)( $n / 2) + 1;
$n = 2 * ( $n - 1);
echo $n . "\n" ;
}
}
$X = 10;
findNthTerm( $X );
$X = 7;
findNthTerm( $X );
?>
|
Javascript
<script>
function findNthTerm(n)
{
if (n % 2 == 0) {
n = Math.floor(n / 2);
n = 2 * (n - 1);
document.write(Math.floor(n / 2) + "<br>" );
}
else {
n = Math.floor(n / 2) + 1;
n = 2 * (n - 1);
document.write(n + "<br>" );
}
}
let X = 10;
findNthTerm(X);
X = 7;
findNthTerm(X);
</script>
|
Time Complexity: O(1), the code will run in a constant time.
Auxiliary Space: O(1), no extra space is required, so it is a constant.