Program to find N-th term of series 1, 2, 11, 12, 21….
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++
// C++ program to find // the N-th term in the series // 1, 2, 11, 12, 21... #include <bits/stdc++.h> using namespace std; // Function to find N-th number in series int printNthElement(int N) { // create an array of size (N+1) int arr[N + 1]; arr[1] = 1; arr[2] = 2; for (int i = 3; i <= N; i++) { // If i is odd if (i % 2 != 0) { arr[i] = arr[i / 2] * 10 + 1; } else { arr[i] = arr[(i / 2) - 1] * 10 + 2; } } return arr[N]; } // Driver code int main() { // Get N int N = 5; // Get Nth term cout << printNthElement(N); return 0; } |
Java
// Java program to find // the N-th term in the series // 1, 2, 11, 12, 21... class FindNth { // Function to find n-th number in series static int printNthElement(int n) { // create an array of size (n+1) int arr[] = new int[n + 1]; arr[1] = 1; arr[2] = 2; for (int i = 3; i <= n; i++) { // If i is odd if (i % 2 != 0) arr[i] = arr[i / 2] * 10 + 1; else arr[i] = arr[(i / 2) - 1] * 10 + 2; } return arr[n]; } // main function public static void main(String[] args) { int n = 5; System.out.println(printNthElement(n)); } } |
Python3
# Python3 program to find # the N-th term in the series # 1, 2, 11, 12, 21... # Return n-th number in series def printNthElement(n) : # create an array of size (n + 1) arr =[0] * (n + 1); arr[1] = 1 arr[2] = 2 for i in range(3, n + 1) : # If i is odd if (i % 2 != 0) : arr[i] = arr[i // 2] * 10 + 1 else : arr[i] = arr[(i // 2) - 1] * 10 + 2 return arr[n] # Driver code n = 5print(printNthElement(n)) |
C#
// C# program to find // the N-th term in the series // 1, 2, 11, 12, 21... using System; class GFG { // Function to find n-th // number in series static int printNthElement(int n) { // create an array of size (n+1) int []arr = new int[n + 1]; arr[1] = 1; arr[2] = 2; for (int i = 3; i <= n; i++) { // If i is odd if (i % 2 != 0) arr[i] = arr[i / 2] * 10 + 1; else arr[i] = arr[(i / 2) - 1] * 10 + 2; } return arr[n]; } // Driver Code public static void Main() { int n = 5; Console.WriteLine(printNthElement(n)); } } // This code is contributed // by inder_verma |
PHP
<?php // PHP program to find // the N-th term in the series // 1, 2, 11, 12, 21... // Function to find N-th // number in series function printNthElement($N) { // create an array of size (N+1) $arr = array($N + 1); $arr[1] = 1; $arr[2] = 2; for ( $i = 3; $i <= $N; $i++) { // If i is odd if ($i % 2 != 0) { $arr[$i] = $arr[$i / 2] * 10 + 1; } else { $arr[$i] = $arr[($i / 2) - 1] * 10 + 2; } } return $arr[$N]; } // Driver code $N = 5; // Get Nth term echo printNthElement($N); // This code is contributed // by Mahadev99 ?> |
21
Recommended Posts:
- Program to find the Nth term of the series 0, 5, 14, 27, 44, ........
- Program to find the Nth term of series 5, 12, 21, 32, 45......
- Program to find nth term of the series 1 4 15 24 45 60 92
- Program to find Nth term in the given Series
- Program to find the Nth term of the series 3, 20, 63, 144, 230, ……
- Program to find the Nth term of series 5, 10, 17, 26, 37, 50, 65, 82, ...
- Program to find the Nth term of series 0, 4, 14, 30, 51, 80, 114, 154, 200, ...
- Program to find Nth term of series 7, 21, 49, 91, 147, 217, ......
- Program to find Nth term of the series 3, 12, 29, 54, 87, ...
- Program to find Nth term of the series 3 , 5 , 21 , 51 , 95 , ...
- Program to find the Nth term of the series 0, 3/1, 8/3, 15/5........
- Program to find the Nth term of the series 0, 14, 40, 78, 124, ...
- Program to find the nth term of the series -2, 4, -6, 8....
- Program to find N-th term of series 3, 5, 33, 35, 53....
- Program to find the N-th term of series 3, 5, 33, 35, 53…. | Set-2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



