Sunday, November 17, 2024
Google search engine
HomeData Modelling & AIFind n-th element from Stern’s Diatomic Series

Find n-th element from Stern’s Diatomic Series

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.
\\ p(n) = p(n/2) \hspace{5.5cm} for \ n \ is \ even\\ p(n) = p((n-1)/2)+p(n+1)/2) \hspace{2cm} for \ n \ is \ odd \\ \\ where \ p(0) = 0 \ and \ p(1) = 1 \\ \\ Stern's \ Diatomic \ Series \ 0, 1, 1, 2, 1, 3, 2, 3, 1, 4, ......

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] = 0, DP[1] = 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] = 0;
    DP[1] = 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];

Below is the implementation of the above approach:

C++




// Program to find the nth element
// of Stern's Diatomic Series
#include <bits/stdc++.h>
using namespace std;
 
// function to find nth stern'
// diatomic series
int findSDSFunc(int n)
{
    // Initializing the DP array
    int DP[n+1];
 
    // SET the Base case
    DP[0] = 0;
    DP[1] = 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
int 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] = 0;
        DP[1] = 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 series
def findSDSFunc(n):
 
    # Initializing the DP array
    DP = [0] * (n+1)
 
    # SET the Base case
    DP[0] = 0
    DP[1] = 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 program
n = 15
 
print(findSDSFunc(n))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#




// C# program to find the nth element
// of Stern's Diatomic Series
using 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] = 0;
        DP[1] = 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




<?php
// PHP Program to find the nth element
// of Stern's Diatomic Series
 
// function to find nth stern'
// diatomic series
function findSDSFunc($n)
{
 
    // SET the Base case
    $DP[0] = 0;
    $DP[1] = 1;
 
    // Traversing the array from
    // 2nd Element to nth Element
    for ($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
$n = 15;
echo(findSDSFunc($n));
 
// This code is contributed by Ajit.
?>


Javascript




<script>
 
// JavaScript program to find the nth element
// of Stern's Diatomic Series
 
    // function to find nth stern'
    // diatomic series
    function findSDSFunc(n)
    {
           
        // Initializing the DP array
        let DP = [];
       
        // SET the Base case
        DP[0] = 0;
        DP[1] = 1;
       
        // Traversing the array from
        // 2nd Element to nth Element
        for (let 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
        let n = 15;     
        document.write(findSDSFunc(n));
           
          // This code is contributed by souravghosh0416.
</script>


Output: 

4

 

Time Complexity: O(N)
Auxiliary Space: O(N)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments