Related Articles

# Find n-th element from Stern’s Diatomic Series

• Difficulty Level : Easy
• Last Updated : 06 Apr, 2021

Given an integer n. we have to find the nth term of Stern’s Diatomic Series.
Stern’s diatomic series is the sequence which generates the following integer sequence 0, 1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, 4, ……. It arises in the Calkin-Wilf tree. It is sometimes also known as the fusc function.
In mathematical terms, the sequence P(n) of Stern’s diatomic series is defined by the recurrence relation. Examples :

Input : n = 7
Output : 3

Input : n = 15
Output : 4

Approach :
We solve this problem with a very simple concept of Dynamic programming which is used in finding fibonacci numbers. After saving the base case of DP = 0, DP = 1, we have to simply traverse from i = 2 to n and compute DP[i] as per explained definition of Stern’s diatomic series. And finally return the value of DP[n].
Algorithm :

 // SET the Base case
DP = 0;
DP = 1;

// Traversing the array from 2nd Element to nth Element
for (int i=2; i<=n; i++)
{
// Case 1: for even n
if (i%2 == 0)
DP[i] = DP[i/2];

// Case 2: for odd n
else
DP[i] = DP[(i-1)/2] + DP[(i+1)/2];
}
return DP[n];

## C++

 // Program to find the nth element// of Stern's Diatomic Series#include using namespace std; // function to find nth stern'// diatomic seriesint findSDSFunc(int n){    // Initializing the DP array    int DP[n+1];     // SET the Base case    DP = 0;    DP = 1;     // Traversing the array from    // 2nd Element to nth Element    for (int i = 2; i <= n; i++) {                 // Case 1: for even n        if (i % 2 == 0)            DP[i] = DP[i / 2];                 // Case 2: for odd n        else            DP[i] = DP[(i - 1) / 2] +                        DP[(i + 1) / 2];    }    return DP[n];} // Driver programint main(){    int n = 15;       cout << findSDSFunc(n) << endl;       return 0;}

## Java

 // Java program to find the nth element// of Stern's Diatomic Series class GFG {         // function to find nth stern'    // diatomic series    static int findSDSFunc(int n)    {                 // Initializing the DP array        int DP[] = new int[n+1];             // SET the Base case        DP = 0;        DP = 1;             // Traversing the array from        // 2nd Element to nth Element        for (int i = 2; i <= n; i++)        {                         // Case 1: for even n            if (i % 2 == 0)                DP[i] = DP[i / 2];                         // Case 2: for odd n            else                DP[i] = DP[(i - 1) / 2] +                            DP[(i + 1) / 2];        }                 return DP[n];    }         // Driver program    public static void main(String[] args)    {        int n = 15;                 System.out.println(findSDSFunc(n));    }} // This code is contributed by Smita Semwal.

## Python 3

 # Program to find the nth element# of Stern's Diatomic Series # function to find nth stern'# diatomic seriesdef findSDSFunc(n):     # Initializing the DP array    DP =  * (n+1)     # SET the Base case    DP = 0    DP = 1     # Traversing the array from    # 2nd Element to nth Element    for i in range(2, n+1):                 # Case 1: for even n        if (int(i % 2) == 0):            DP[i] = DP[int(i / 2)]                 # Case 2: for odd n        else:            DP[i] = (DP[int((i - 1) / 2)]                  + DP[int((i + 1) / 2)])         return DP[n]  # Driver programn = 15 print(findSDSFunc(n)) # This code is contribute by# Smitha Dinesh Semwal

## C#

 // C# program to find the nth element// of Stern's Diatomic Seriesusing System; class GFG{    // function to find nth    // stern' diatomic series    static int findSDSFunc(int n)    {                 // Initializing the DP array        int []DP = new int[n + 1];             // SET the Base case        DP = 0;        DP = 1;             // Traversing the array from        // 2nd Element to nth Element        for (int i = 2; i <= n; i++)        {                         // Case 1: for even n            if (i % 2 == 0)                DP[i] = DP[i / 2];                         // Case 2: for odd n            else                DP[i] = DP[(i - 1) / 2] +                        DP[(i + 1) / 2];        }                 return DP[n];    }         // Driver Code    static public void Main ()    {        int n = 15;        Console.WriteLine(findSDSFunc(n));    }} // This code is contributed by aj_36

## PHP

 

## Javascript

 
Output:
4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up