Program to find N-th term of series 1, 2, 11, 12, 21….
Last Updated :
27 Aug, 2022
Given a number N, the task is to find the Nth term of the series:
1, 2, 11, 12, 21…
Examples:
Input : N = 2
Output : 2
Input : N = 5
Output : 21
Approach:
The idea is based on the fact that the value of the last digit alternates in the series. For example, if the last digit of ith number is 1, then the last digit of (i-1)th and (i+1)th numbers must be 2.
Therefore, Create an array of size (n+1) and push 1 and 2(These two are always first two elements of series) to it.
Therefore the ith term of the array is:
1) If i is odd,
arr[i] = arr[i/2]*10 + 1;
2) If i is even,
arr[i] = arr[(i/2)-1]*10 + 2;
At last return arr[n].
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
int printNthElement( int N)
{
int arr[N + 1];
arr[1] = 1;
arr[2] = 2;
for ( int i = 3; i <= N; i++) {
if (i % 2 != 0) {
arr[i] = arr[i / 2] * 10 + 1;
}
else {
arr[i] = arr[(i / 2) - 1] * 10 + 2;
}
}
return arr[N];
}
int main()
{
int N = 5;
cout << printNthElement(N);
return 0;
}
|
C
#include <stdio.h>
int printNthElement( int N)
{
int arr[N + 1];
arr[1] = 1;
arr[2] = 2;
for ( int i = 3; i <= N; i++) {
if (i % 2 != 0) {
arr[i] = arr[i / 2] * 10 + 1;
}
else {
arr[i] = arr[(i / 2) - 1] * 10 + 2;
}
}
return arr[N];
}
int main()
{
int N = 5;
printf ( "%d" ,printNthElement(N));
return 0;
}
|
Java
class FindNth {
static int printNthElement( int n)
{
int arr[] = new int [n + 1 ];
arr[ 1 ] = 1 ;
arr[ 2 ] = 2 ;
for ( int i = 3 ; i <= n; i++) {
if (i % 2 != 0 )
arr[i] = arr[i / 2 ] * 10 + 1 ;
else
arr[i] = arr[(i / 2 ) - 1 ] * 10 + 2 ;
}
return arr[n];
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println(printNthElement(n));
}
}
|
Python3
def printNthElement(n) :
arr = [ 0 ] * (n + 1 );
arr[ 1 ] = 1
arr[ 2 ] = 2
for i in range ( 3 , n + 1 ) :
if (i % 2 ! = 0 ) :
arr[i] = arr[i / / 2 ] * 10 + 1
else :
arr[i] = arr[(i / / 2 ) - 1 ] * 10 + 2
return arr[n]
n = 5
print (printNthElement(n))
|
C#
using System;
class GFG
{
static int printNthElement( int n)
{
int []arr = new int [n + 1];
arr[1] = 1;
arr[2] = 2;
for ( int i = 3; i <= n; i++)
{
if (i % 2 != 0)
arr[i] = arr[i / 2] * 10 + 1;
else
arr[i] = arr[(i / 2) - 1] * 10 + 2;
}
return arr[n];
}
public static void Main()
{
int n = 5;
Console.WriteLine(printNthElement(n));
}
}
|
PHP
<?php
function printNthElement( $N )
{
$arr = array ( $N + 1);
$arr [1] = 1;
$arr [2] = 2;
for ( $i = 3; $i <= $N ; $i ++)
{
if ( $i % 2 != 0)
{
$arr [ $i ] = $arr [ $i / 2] *
10 + 1;
}
else
{
$arr [ $i ] = $arr [( $i / 2) - 1] *
10 + 2;
}
}
return $arr [ $N ];
}
$N = 5;
echo printNthElement( $N );
?>
|
Javascript
<script>
function printNthElement(n)
{
let arr = new Array(n + 1);
arr[1] = 1;
arr[2] = 2;
for (let i = 3; i <= n; i++) {
if (i % 2 != 0)
arr[i] = arr[parseInt(i / 2, 10)] * 10 + 1;
else
arr[i] = arr[parseInt(i / 2, 10) - 1] * 10 + 2;
}
return arr[n];
}
let n = 5;
document.write(printNthElement(n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...