Count the occurrence of Nth term in first N terms of Van Eck's sequence - GeeksforGeeks

07 Feb, 2020
Prerequisite: Van Eck’s sequence
Given a positive integer N, the task is to count the occurrences of Nth term in first N terms of Van Eck’s sequence.
Examples:
Input: N = 5Output: 1Explanation:First 5 terms of Van Eck’s Sequence 0, 0, 1, 0, 2Occurrence of 5th term i.e 2 = 1
Input: 11Output: 5Explanation:First 11 terms of Van Eck’s Sequence 0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0,Occurrence of 11th term i.e 0 is 5
Naive Approach:Generate Van Eck’s sequence upto Nth termIterate through the generated sequence and count the occurrence of Nth term.To serve multiple queries we can pre-compute the Van Eck’s sequence.Below is the implementation of above approach:C++JavaPython3C#C++// C++ program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  #include <bits/stdc++.h>using namespace std;  #define MAX 100000int sequence[MAX + 1];  // Utility function to compute// Van Eck's sequencevoid vanEckSequence(){      // Initialize sequence array    for (int i = 0; i < MAX; i++) {        sequence[i] = 0;    }      // Loop to generate sequence    for (int i = 0; i < MAX; i++) {          // Check if sequence[i] has occured        // previously or is new to sequence        for (int j = i - 1; j >= 0; j--) {            if (sequence[j] == sequence[i]) {                  // If occurrence found                // then the next term will be                // how far back this last term                // occured previously                sequence[i + 1] = i - j;                break;            }        }    }}  // Utility function to count// the occurrence of nth term// in first n terms of the sequenceint getCount(int n){      // Get nth term of the sequence    int nthTerm = sequence[n - 1];      int count = 0;      // Count the occurrence of nth term    // in first n terms of the sequence    for (int i = 0; i < n; i++) {          if (sequence[i] == nthTerm)            count++;    }      // Return count    return count;}  // Driver codeint main(){      // Pre-compute Van Eck's sequence    vanEckSequence();      int n = 5;      // Print count of the occurrence of nth term    // in first n terms of the sequence    cout << getCount(n) << endl;      n = 11;      // Print count of the occurrence of nth term    // in first n terms of the sequence    cout << getCount(n) << endl;      return 0;}Java// Java program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  class GFG {      static int MAX = 100000;    static int sequence[] = new int[MAX + 1];      // Utility function to compute    // Van Eck's sequence    static void vanEckSequence()    {          // Initialize sequence array        for (int i = 0; i < MAX; i++) {            sequence[i] = 0;        }          // Loop to generate sequence        for (int i = 0; i < MAX; i++) {              // Check if sequence[i] has occured            // previously or is new to sequence            for (int j = i - 1; j >= 0; j--) {                if (sequence[j] == sequence[i]) {                      // If occurrence found                    // then the next term will be                    // how far back this last term                    // occured previously                    sequence[i + 1] = i - j;                    break;                }            }        }    }      // Utility function to count    // the occurrence of nth term    // in first n terms of the sequence    static int getCount(int n)    {          // Get nth term of the sequence        int nthTerm = sequence[n - 1];          int count = 0;          // Count the occurrence of nth term        // in first n terms of the sequence        for (int i = 0; i < n; i++) {              if (sequence[i] == nthTerm)                count++;        }          // Return count        return count;    }      // Driver code    public static void main(String[] args)    {          // Pre-compute Van Eck's sequence        vanEckSequence();          int n = 5;          // Print count of the occurrence of nth term        // in first n terms of the sequence        System.out.println(getCount(n));          n = 11;          // Print count of the occurrence of nth term        // in first n terms of the sequence        System.out.println(getCount(n));    }}Python3# Python3 program to count the occurrence # of nth term in first n terms # of Van Eck's sequence   MAX = 10000sequence = [0]*(MAX + 1);   # Utility function to compute # Van Eck's sequence def vanEckSequence() :          # Loop to generate sequence     for i in range(MAX) :          # Check if sequence[i] has occured         # previously or is new to sequence         for j in range(i - 1, -1, -1) :            if (sequence[j] == sequence[i]) :                  # If occurrence found                 # then the next term will be                 # how far back this last term                 # occured previously                 sequence[i + 1] = i - j;                 break;   # Utility function to count # the occurrence of nth term # in first n terms of the sequence def getCount(n) :       # Get nth term of the sequence     nthTerm = sequence[n - 1];       count = 0;       # Count the occurrence of nth term     # in first n terms of the sequence     for i in range(n) :          if (sequence[i] == nthTerm) :            count += 1;       # Return count     return count;   # Driver code if __name__ == "__main__" :       # Pre-compute Van Eck's sequence     vanEckSequence();       n = 5;       # Print count of the occurrence of nth term     # in first n terms of the sequence     print(getCount(n));       n = 11;       # Print count of the occurrence of nth term     # in first n terms of the sequence     print(getCount(n));   # This code is contributed by AnkitRai01C#// C# program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  using System;class GFG {      static int MAX = 100000;    static int[] sequence = new int[MAX + 1];      // Utility function to compute    // Van Eck's sequence    static void vanEckSequence()    {          // Initialize sequence array        for (int i = 0; i < MAX; i++) {            sequence[i] = 0;        }          // Loop to generate sequence        for (int i = 0; i < MAX; i++) {              // Check if sequence[i] has occured            // previously or is new to sequence            for (int j = i - 1; j >= 0; j--) {                if (sequence[j] == sequence[i]) {                      // If occurrence found                    // then the next term will be                    // how far back this last term                    // occured previously                    sequence[i + 1] = i - j;                    break;                }            }        }    }      // Utility function to count    // the occurrence of nth term    // in first n terms of the sequence    static int getCount(int n)    {          // Get nth term of the sequence        int nthTerm = sequence[n - 1];          int count = 0;          // Count the occurrence of nth term        // in first n terms of the sequence        for (int i = 0; i < n; i++) {              if (sequence[i] == nthTerm)                count++;        }          // Return count        return count;    }      // Driver code    public static void Main()    {          // Pre-compute Van Eck's sequence        vanEckSequence();          int n = 5;          // Print count of the occurrence of nth term        // in first n terms of the sequence        Console.WriteLine(getCount(n));          n = 11;          // Print count of the occurrence of nth term        // in first n terms of the sequence        Console.WriteLine(getCount(n));    }}Output:1
5
Efficient Approach:For a given term in Van Eck’s sequence, its next term indicates the distance between last occurrence of the given term.So, for ith term, its previous occurrence will be at i – value (i + 1)th term.For example:Also, if the next term in the sequence is 0 then this means that the term has not occurred before.For example:Algorithm:Let us consider Nth term of the sequence as SNIf SN+1 is non-zero then increment countand do the same for (N- SN+1)th termAnd if SN+1 is zero then stop.Below is the implementation of above approach:CPPJavaPython3C#CPP// C++ program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  #include <bits/stdc++.h>using namespace std;  #define MAX 100000int sequence[MAX + 1];  // Utility function to compute// Van Eck's sequencevoid vanEckSequence(){      // Initialize sequence array    for (int i = 0; i < MAX; i++) {        sequence[i] = 0;    }      // Loop to generate sequence    for (int i = 0; i < MAX; i++) {          // Check if sequence[i] has occured        // previously or is new to sequence        for (int j = i - 1; j >= 0; j--) {            if (sequence[j] == sequence[i]) {                  // If occurrence found                // then the next term will be                // how far back this last term                // occured previously                sequence[i + 1] = i - j;                break;            }        }    }}  // Utility function to count// the occurrence of nth term// in first n terms of the sequenceint getCount(int n){      // Initialize count as 1    int count = 1;      int i = n - 1;      while (sequence[i + 1] != 0) {          // Increment count if (i+1)th term        // is non-zero        count++;          // Previous occurrence of sequence[i]        // will be it (i - sequence[i+1])th position        i = i - sequence[i + 1];    }      // Return the count of occurrence    return count;}  // Driver codeint main(){      // Pre-compute Van Eck's sequence    vanEckSequence();      int n = 5;      // Print count of the occurrence of nth term    // in first n terms of the sequence    cout << getCount(n) << endl;      n = 11;      // Print count of the occurrence of nth term    // in first n terms of the sequence    cout << getCount(n) << endl;      return 0;}Java// Java program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  class GFG {      static int MAX = 100000;    static int sequence[] = new int[MAX + 1];      // Utility function to compute    // Van Eck's sequence    static void vanEckSequence()    {          // Initialize sequence array        for (int i = 0; i < MAX; i++) {            sequence[i] = 0;        }          // Loop to generate sequence        for (int i = 0; i < MAX; i++) {              // Check if sequence[i] has occured            // previously or is new to sequence            for (int j = i - 1; j >= 0; j--) {                if (sequence[j] == sequence[i]) {                      // If occurrence found                    // then the next term will be                    // how far back this last term                    // occured previously                    sequence[i + 1] = i - j;                    break;                }            }        }    }      // Utility function to count    // the occurrence of nth term    // in first n terms of the sequence    static int getCount(int n)    {          // Initialize count as 1        int count = 1;          int i = n - 1;        while (sequence[i + 1] != 0) {              // Increment count if (i+1)th term            // is non-zero            count++;              // Previous occurrence of sequence[i]            // will be it (i - sequence[i+1])th position            i = i - sequence[i + 1];        }          // Return the count of occurrence        return count;    }      // Driver code    public static void main(String[] args)    {          // Pre-compute Van Eck's sequence        vanEckSequence();          int n = 5;          // Print count of the occurrence of nth term        // in first n terms of the sequence        System.out.println(getCount(n));          n = 11;          // Print count of the occurrence of nth term        // in first n terms of the sequence        System.out.println(getCount(n));    }}Python3# Python3 program to count the occurrence # of nth term in first n terms # of Van Eck's sequence MAX = 10000sequence = [0] * (MAX + 1);   # Utility function to compute # Van Eck's sequence def vanEckSequence() :      # Loop to generate sequence     for i in range(MAX) :           # Check if sequence[i] has occured         # previously or is new to sequence         for j in range( i - 1, -1, -1) :             if (sequence[j] == sequence[i]) :                                  # If occurrence found                 # then the next term will be                 # how far back this last term                 # occured previously                 sequence[i + 1] = i - j;                 break;   # Utility function to count # the occurrence of nth term # in first n terms of the sequence def getCount(n) :       # Initialize count as 1     count = 1;       i = n - 1;       while (sequence[i + 1] != 0) :          # Increment count if (i+1)th term         # is non-zero         count += 1;           # Previous occurrence of sequence[i]         # will be it (i - sequence[i+1])th position         i = i - sequence[i + 1];       # Return the count of occurrence     return count;   # Driver code if __name__ == "__main__" :       # Pre-compute Van Eck's sequence     vanEckSequence();       n = 5;       # Print count of the occurrence of nth term     # in first n terms of the sequence     print(getCount(n));       n = 11;       # Print count of the occurrence of nth term     # in first n terms of the sequence     print(getCount(n)) ;   # This code is contributed by AnkitRai01C#// C# program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  using System;class GFG {      static int MAX = 100000;    static int[] sequence = new int[MAX + 1];      // Utility function to compute    // Van Eck's sequence    static void vanEckSequence()    {          // Initialize sequence array        for (int i = 0; i < MAX; i++) {            sequence[i] = 0;        }          // Loop to generate sequence        for (int i = 0; i < MAX; i++) {              // Check if sequence[i] has occured            // previously or is new to sequence            for (int j = i - 1; j >= 0; j--) {                if (sequence[j] == sequence[i]) {                      // If occurrence found                    // then the next term will be                    // how far back this last term                    // occured previously                    sequence[i + 1] = i - j;                    break;                }            }        }    }      // Utility function to count    // the occurrence of nth term    // in first n terms of the sequence    static int getCount(int n)    {          // Initialize count as 1        int count = 1;        int i = n - 1;          while (sequence[i + 1] != 0) {              // Increment count if (i+1)th term            // is non-zero            count++;              // Previous occurrence of sequence[i]            // will be it (i - sequence[i+1])th position            i = i - sequence[i + 1];        }          // Return the count of occurrence        return count;    }      // Driver code    public static void Main(string[] args)    {          // Pre-compute Van Eck's sequence        vanEckSequence();          int n = 5;          // Print count of the occurrence of nth term        // in first n terms of the sequence        Console.WriteLine(getCount(n));          n = 11;          // Print count of the occurrence of nth term        // in first n terms of the sequence        Console.WriteLine(getCount(n));    }}Output:1
5

Naive Approach:Generate Van Eck’s sequence upto Nth termIterate through the generated sequence and count the occurrence of Nth term.To serve multiple queries we can pre-compute the Van Eck’s sequence.Below is the implementation of above approach:C++JavaPython3C#C++// C++ program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  #include <bits/stdc++.h>using namespace std;  #define MAX 100000int sequence[MAX + 1];  // Utility function to compute// Van Eck's sequencevoid vanEckSequence(){      // Initialize sequence array    for (int i = 0; i < MAX; i++) {        sequence[i] = 0;    }      // Loop to generate sequence    for (int i = 0; i < MAX; i++) {          // Check if sequence[i] has occured        // previously or is new to sequence        for (int j = i - 1; j >= 0; j--) {            if (sequence[j] == sequence[i]) {                  // If occurrence found                // then the next term will be                // how far back this last term                // occured previously                sequence[i + 1] = i - j;                break;            }        }    }}  // Utility function to count// the occurrence of nth term// in first n terms of the sequenceint getCount(int n){      // Get nth term of the sequence    int nthTerm = sequence[n - 1];      int count = 0;      // Count the occurrence of nth term    // in first n terms of the sequence    for (int i = 0; i < n; i++) {          if (sequence[i] == nthTerm)            count++;    }      // Return count    return count;}  // Driver codeint main(){      // Pre-compute Van Eck's sequence    vanEckSequence();      int n = 5;      // Print count of the occurrence of nth term    // in first n terms of the sequence    cout << getCount(n) << endl;      n = 11;      // Print count of the occurrence of nth term    // in first n terms of the sequence    cout << getCount(n) << endl;      return 0;}Java// Java program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  class GFG {      static int MAX = 100000;    static int sequence[] = new int[MAX + 1];      // Utility function to compute    // Van Eck's sequence    static void vanEckSequence()    {          // Initialize sequence array        for (int i = 0; i < MAX; i++) {            sequence[i] = 0;        }          // Loop to generate sequence        for (int i = 0; i < MAX; i++) {              // Check if sequence[i] has occured            // previously or is new to sequence            for (int j = i - 1; j >= 0; j--) {                if (sequence[j] == sequence[i]) {                      // If occurrence found                    // then the next term will be                    // how far back this last term                    // occured previously                    sequence[i + 1] = i - j;                    break;                }            }        }    }      // Utility function to count    // the occurrence of nth term    // in first n terms of the sequence    static int getCount(int n)    {          // Get nth term of the sequence        int nthTerm = sequence[n - 1];          int count = 0;          // Count the occurrence of nth term        // in first n terms of the sequence        for (int i = 0; i < n; i++) {              if (sequence[i] == nthTerm)                count++;        }          // Return count        return count;    }      // Driver code    public static void main(String[] args)    {          // Pre-compute Van Eck's sequence        vanEckSequence();          int n = 5;          // Print count of the occurrence of nth term        // in first n terms of the sequence        System.out.println(getCount(n));          n = 11;          // Print count of the occurrence of nth term        // in first n terms of the sequence        System.out.println(getCount(n));    }}Python3# Python3 program to count the occurrence # of nth term in first n terms # of Van Eck's sequence   MAX = 10000sequence = [0]*(MAX + 1);   # Utility function to compute # Van Eck's sequence def vanEckSequence() :          # Loop to generate sequence     for i in range(MAX) :          # Check if sequence[i] has occured         # previously or is new to sequence         for j in range(i - 1, -1, -1) :            if (sequence[j] == sequence[i]) :                  # If occurrence found                 # then the next term will be                 # how far back this last term                 # occured previously                 sequence[i + 1] = i - j;                 break;   # Utility function to count # the occurrence of nth term # in first n terms of the sequence def getCount(n) :       # Get nth term of the sequence     nthTerm = sequence[n - 1];       count = 0;       # Count the occurrence of nth term     # in first n terms of the sequence     for i in range(n) :          if (sequence[i] == nthTerm) :            count += 1;       # Return count     return count;   # Driver code if __name__ == "__main__" :       # Pre-compute Van Eck's sequence     vanEckSequence();       n = 5;       # Print count of the occurrence of nth term     # in first n terms of the sequence     print(getCount(n));       n = 11;       # Print count of the occurrence of nth term     # in first n terms of the sequence     print(getCount(n));   # This code is contributed by AnkitRai01C#// C# program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  using System;class GFG {      static int MAX = 100000;    static int[] sequence = new int[MAX + 1];      // Utility function to compute    // Van Eck's sequence    static void vanEckSequence()    {          // Initialize sequence array        for (int i = 0; i < MAX; i++) {            sequence[i] = 0;        }          // Loop to generate sequence        for (int i = 0; i < MAX; i++) {              // Check if sequence[i] has occured            // previously or is new to sequence            for (int j = i - 1; j >= 0; j--) {                if (sequence[j] == sequence[i]) {                      // If occurrence found                    // then the next term will be                    // how far back this last term                    // occured previously                    sequence[i + 1] = i - j;                    break;                }            }        }    }      // Utility function to count    // the occurrence of nth term    // in first n terms of the sequence    static int getCount(int n)    {          // Get nth term of the sequence        int nthTerm = sequence[n - 1];          int count = 0;          // Count the occurrence of nth term        // in first n terms of the sequence        for (int i = 0; i < n; i++) {              if (sequence[i] == nthTerm)                count++;        }          // Return count        return count;    }      // Driver code    public static void Main()    {          // Pre-compute Van Eck's sequence        vanEckSequence();          int n = 5;          // Print count of the occurrence of nth term        // in first n terms of the sequence        Console.WriteLine(getCount(n));          n = 11;          // Print count of the occurrence of nth term        // in first n terms of the sequence        Console.WriteLine(getCount(n));    }}Output:1
5

Generate Van Eck’s sequence upto Nth term
Iterate through the generated sequence and count the occurrence of Nth term.
To serve multiple queries we can pre-compute the Van Eck’s sequence.
Below is the implementation of above approach:
C++
Java
Python3
C#
// C++ program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  #include <bits/stdc++.h>using namespace std;  #define MAX 100000int sequence[MAX + 1];  // Utility function to compute// Van Eck's sequencevoid vanEckSequence(){      // Initialize sequence array    for (int i = 0; i < MAX; i++) {        sequence[i] = 0;    }      // Loop to generate sequence    for (int i = 0; i < MAX; i++) {          // Check if sequence[i] has occured        // previously or is new to sequence        for (int j = i - 1; j >= 0; j--) {            if (sequence[j] == sequence[i]) {                  // If occurrence found                // then the next term will be                // how far back this last term                // occured previously                sequence[i + 1] = i - j;                break;            }        }    }}  // Utility function to count// the occurrence of nth term// in first n terms of the sequenceint getCount(int n){      // Get nth term of the sequence    int nthTerm = sequence[n - 1];      int count = 0;      // Count the occurrence of nth term    // in first n terms of the sequence    for (int i = 0; i < n; i++) {          if (sequence[i] == nthTerm)            count++;    }      // Return count    return count;}  // Driver codeint main(){      // Pre-compute Van Eck's sequence    vanEckSequence();      int n = 5;      // Print count of the occurrence of nth term    // in first n terms of the sequence    cout << getCount(n) << endl;      n = 11;      // Print count of the occurrence of nth term    // in first n terms of the sequence    cout << getCount(n) << endl;      return 0;}
// Java program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  class GFG {      static int MAX = 100000;    static int sequence[] = new int[MAX + 1];      // Utility function to compute    // Van Eck's sequence    static void vanEckSequence()    {          // Initialize sequence array        for (int i = 0; i < MAX; i++) {            sequence[i] = 0;        }          // Loop to generate sequence        for (int i = 0; i < MAX; i++) {              // Check if sequence[i] has occured            // previously or is new to sequence            for (int j = i - 1; j >= 0; j--) {                if (sequence[j] == sequence[i]) {                      // If occurrence found                    // then the next term will be                    // how far back this last term                    // occured previously                    sequence[i + 1] = i - j;                    break;                }            }        }    }      // Utility function to count    // the occurrence of nth term    // in first n terms of the sequence    static int getCount(int n)    {          // Get nth term of the sequence        int nthTerm = sequence[n - 1];          int count = 0;          // Count the occurrence of nth term        // in first n terms of the sequence        for (int i = 0; i < n; i++) {              if (sequence[i] == nthTerm)                count++;        }          // Return count        return count;    }      // Driver code    public static void main(String[] args)    {          // Pre-compute Van Eck's sequence        vanEckSequence();          int n = 5;          // Print count of the occurrence of nth term        // in first n terms of the sequence        System.out.println(getCount(n));          n = 11;          // Print count of the occurrence of nth term        // in first n terms of the sequence        System.out.println(getCount(n));    }}
# Python3 program to count the occurrence # of nth term in first n terms # of Van Eck's sequence   MAX = 10000sequence = [0]*(MAX + 1);   # Utility function to compute # Van Eck's sequence def vanEckSequence() :          # Loop to generate sequence     for i in range(MAX) :          # Check if sequence[i] has occured         # previously or is new to sequence         for j in range(i - 1, -1, -1) :            if (sequence[j] == sequence[i]) :                  # If occurrence found                 # then the next term will be                 # how far back this last term                 # occured previously                 sequence[i + 1] = i - j;                 break;   # Utility function to count # the occurrence of nth term # in first n terms of the sequence def getCount(n) :       # Get nth term of the sequence     nthTerm = sequence[n - 1];       count = 0;       # Count the occurrence of nth term     # in first n terms of the sequence     for i in range(n) :          if (sequence[i] == nthTerm) :            count += 1;       # Return count     return count;   # Driver code if __name__ == "__main__" :       # Pre-compute Van Eck's sequence     vanEckSequence();       n = 5;       # Print count of the occurrence of nth term     # in first n terms of the sequence     print(getCount(n));       n = 11;       # Print count of the occurrence of nth term     # in first n terms of the sequence     print(getCount(n));   # This code is contributed by AnkitRai01
// C# program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  using System;class GFG {      static int MAX = 100000;    static int[] sequence = new int[MAX + 1];      // Utility function to compute    // Van Eck's sequence    static void vanEckSequence()    {          // Initialize sequence array        for (int i = 0; i < MAX; i++) {            sequence[i] = 0;        }          // Loop to generate sequence        for (int i = 0; i < MAX; i++) {              // Check if sequence[i] has occured            // previously or is new to sequence            for (int j = i - 1; j >= 0; j--) {                if (sequence[j] == sequence[i]) {                      // If occurrence found                    // then the next term will be                    // how far back this last term                    // occured previously                    sequence[i + 1] = i - j;                    break;                }            }        }    }      // Utility function to count    // the occurrence of nth term    // in first n terms of the sequence    static int getCount(int n)    {          // Get nth term of the sequence        int nthTerm = sequence[n - 1];          int count = 0;          // Count the occurrence of nth term        // in first n terms of the sequence        for (int i = 0; i < n; i++) {              if (sequence[i] == nthTerm)                count++;        }          // Return count        return count;    }      // Driver code    public static void Main()    {          // Pre-compute Van Eck's sequence        vanEckSequence();          int n = 5;          // Print count of the occurrence of nth term        // in first n terms of the sequence        Console.WriteLine(getCount(n));          n = 11;          // Print count of the occurrence of nth term        // in first n terms of the sequence        Console.WriteLine(getCount(n));    }}
1
5

Efficient Approach:For a given term in Van Eck’s sequence, its next term indicates the distance between last occurrence of the given term.So, for ith term, its previous occurrence will be at i – value (i + 1)th term.For example:Also, if the next term in the sequence is 0 then this means that the term has not occurred before.For example:Algorithm:Let us consider Nth term of the sequence as SNIf SN+1 is non-zero then increment countand do the same for (N- SN+1)th termAnd if SN+1 is zero then stop.Below is the implementation of above approach:CPPJavaPython3C#CPP// C++ program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  #include <bits/stdc++.h>using namespace std;  #define MAX 100000int sequence[MAX + 1];  // Utility function to compute// Van Eck's sequencevoid vanEckSequence(){      // Initialize sequence array    for (int i = 0; i < MAX; i++) {        sequence[i] = 0;    }      // Loop to generate sequence    for (int i = 0; i < MAX; i++) {          // Check if sequence[i] has occured        // previously or is new to sequence        for (int j = i - 1; j >= 0; j--) {            if (sequence[j] == sequence[i]) {                  // If occurrence found                // then the next term will be                // how far back this last term                // occured previously                sequence[i + 1] = i - j;                break;            }        }    }}  // Utility function to count// the occurrence of nth term// in first n terms of the sequenceint getCount(int n){      // Initialize count as 1    int count = 1;      int i = n - 1;      while (sequence[i + 1] != 0) {          // Increment count if (i+1)th term        // is non-zero        count++;          // Previous occurrence of sequence[i]        // will be it (i - sequence[i+1])th position        i = i - sequence[i + 1];    }      // Return the count of occurrence    return count;}  // Driver codeint main(){      // Pre-compute Van Eck's sequence    vanEckSequence();      int n = 5;      // Print count of the occurrence of nth term    // in first n terms of the sequence    cout << getCount(n) << endl;      n = 11;      // Print count of the occurrence of nth term    // in first n terms of the sequence    cout << getCount(n) << endl;      return 0;}Java// Java program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  class GFG {      static int MAX = 100000;    static int sequence[] = new int[MAX + 1];      // Utility function to compute    // Van Eck's sequence    static void vanEckSequence()    {          // Initialize sequence array        for (int i = 0; i < MAX; i++) {            sequence[i] = 0;        }          // Loop to generate sequence        for (int i = 0; i < MAX; i++) {              // Check if sequence[i] has occured            // previously or is new to sequence            for (int j = i - 1; j >= 0; j--) {                if (sequence[j] == sequence[i]) {                      // If occurrence found                    // then the next term will be                    // how far back this last term                    // occured previously                    sequence[i + 1] = i - j;                    break;                }            }        }    }      // Utility function to count    // the occurrence of nth term    // in first n terms of the sequence    static int getCount(int n)    {          // Initialize count as 1        int count = 1;          int i = n - 1;        while (sequence[i + 1] != 0) {              // Increment count if (i+1)th term            // is non-zero            count++;              // Previous occurrence of sequence[i]            // will be it (i - sequence[i+1])th position            i = i - sequence[i + 1];        }          // Return the count of occurrence        return count;    }      // Driver code    public static void main(String[] args)    {          // Pre-compute Van Eck's sequence        vanEckSequence();          int n = 5;          // Print count of the occurrence of nth term        // in first n terms of the sequence        System.out.println(getCount(n));          n = 11;          // Print count of the occurrence of nth term        // in first n terms of the sequence        System.out.println(getCount(n));    }}Python3# Python3 program to count the occurrence # of nth term in first n terms # of Van Eck's sequence MAX = 10000sequence = [0] * (MAX + 1);   # Utility function to compute # Van Eck's sequence def vanEckSequence() :      # Loop to generate sequence     for i in range(MAX) :           # Check if sequence[i] has occured         # previously or is new to sequence         for j in range( i - 1, -1, -1) :             if (sequence[j] == sequence[i]) :                                  # If occurrence found                 # then the next term will be                 # how far back this last term                 # occured previously                 sequence[i + 1] = i - j;                 break;   # Utility function to count # the occurrence of nth term # in first n terms of the sequence def getCount(n) :       # Initialize count as 1     count = 1;       i = n - 1;       while (sequence[i + 1] != 0) :          # Increment count if (i+1)th term         # is non-zero         count += 1;           # Previous occurrence of sequence[i]         # will be it (i - sequence[i+1])th position         i = i - sequence[i + 1];       # Return the count of occurrence     return count;   # Driver code if __name__ == "__main__" :       # Pre-compute Van Eck's sequence     vanEckSequence();       n = 5;       # Print count of the occurrence of nth term     # in first n terms of the sequence     print(getCount(n));       n = 11;       # Print count of the occurrence of nth term     # in first n terms of the sequence     print(getCount(n)) ;   # This code is contributed by AnkitRai01C#// C# program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  using System;class GFG {      static int MAX = 100000;    static int[] sequence = new int[MAX + 1];      // Utility function to compute    // Van Eck's sequence    static void vanEckSequence()    {          // Initialize sequence array        for (int i = 0; i < MAX; i++) {            sequence[i] = 0;        }          // Loop to generate sequence        for (int i = 0; i < MAX; i++) {              // Check if sequence[i] has occured            // previously or is new to sequence            for (int j = i - 1; j >= 0; j--) {                if (sequence[j] == sequence[i]) {                      // If occurrence found                    // then the next term will be                    // how far back this last term                    // occured previously                    sequence[i + 1] = i - j;                    break;                }            }        }    }      // Utility function to count    // the occurrence of nth term    // in first n terms of the sequence    static int getCount(int n)    {          // Initialize count as 1        int count = 1;        int i = n - 1;          while (sequence[i + 1] != 0) {              // Increment count if (i+1)th term            // is non-zero            count++;              // Previous occurrence of sequence[i]            // will be it (i - sequence[i+1])th position            i = i - sequence[i + 1];        }          // Return the count of occurrence        return count;    }      // Driver code    public static void Main(string[] args)    {          // Pre-compute Van Eck's sequence        vanEckSequence();          int n = 5;          // Print count of the occurrence of nth term        // in first n terms of the sequence        Console.WriteLine(getCount(n));          n = 11;          // Print count of the occurrence of nth term        // in first n terms of the sequence        Console.WriteLine(getCount(n));    }}Output:1
5

For a given term in Van Eck’s sequence, its next term indicates the distance between last occurrence of the given term.
So, for ith term, its previous occurrence will be at i – value (i + 1)th term.For example:
Also, if the next term in the sequence is 0 then this means that the term has not occurred before.For example:
Algorithm:Let us consider Nth term of the sequence as SNIf SN+1 is non-zero then increment countand do the same for (N- SN+1)th termAnd if SN+1 is zero then stop.
Let us consider Nth term of the sequence as SN
If SN+1 is non-zero then increment countand do the same for (N- SN+1)th term
And if SN+1 is zero then stop.
Below is the implementation of above approach:
CPP
Java
Python3
C#
// C++ program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  #include <bits/stdc++.h>using namespace std;  #define MAX 100000int sequence[MAX + 1];  // Utility function to compute// Van Eck's sequencevoid vanEckSequence(){      // Initialize sequence array    for (int i = 0; i < MAX; i++) {        sequence[i] = 0;    }      // Loop to generate sequence    for (int i = 0; i < MAX; i++) {          // Check if sequence[i] has occured        // previously or is new to sequence        for (int j = i - 1; j >= 0; j--) {            if (sequence[j] == sequence[i]) {                  // If occurrence found                // then the next term will be                // how far back this last term                // occured previously                sequence[i + 1] = i - j;                break;            }        }    }}  // Utility function to count// the occurrence of nth term// in first n terms of the sequenceint getCount(int n){      // Initialize count as 1    int count = 1;      int i = n - 1;      while (sequence[i + 1] != 0) {          // Increment count if (i+1)th term        // is non-zero        count++;          // Previous occurrence of sequence[i]        // will be it (i - sequence[i+1])th position        i = i - sequence[i + 1];    }      // Return the count of occurrence    return count;}  // Driver codeint main(){      // Pre-compute Van Eck's sequence    vanEckSequence();      int n = 5;      // Print count of the occurrence of nth term    // in first n terms of the sequence    cout << getCount(n) << endl;      n = 11;      // Print count of the occurrence of nth term    // in first n terms of the sequence    cout << getCount(n) << endl;      return 0;}
// Java program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  class GFG {      static int MAX = 100000;    static int sequence[] = new int[MAX + 1];      // Utility function to compute    // Van Eck's sequence    static void vanEckSequence()    {          // Initialize sequence array        for (int i = 0; i < MAX; i++) {            sequence[i] = 0;        }          // Loop to generate sequence        for (int i = 0; i < MAX; i++) {              // Check if sequence[i] has occured            // previously or is new to sequence            for (int j = i - 1; j >= 0; j--) {                if (sequence[j] == sequence[i]) {                      // If occurrence found                    // then the next term will be                    // how far back this last term                    // occured previously                    sequence[i + 1] = i - j;                    break;                }            }        }    }      // Utility function to count    // the occurrence of nth term    // in first n terms of the sequence    static int getCount(int n)    {          // Initialize count as 1        int count = 1;          int i = n - 1;        while (sequence[i + 1] != 0) {              // Increment count if (i+1)th term            // is non-zero            count++;              // Previous occurrence of sequence[i]            // will be it (i - sequence[i+1])th position            i = i - sequence[i + 1];        }          // Return the count of occurrence        return count;    }      // Driver code    public static void main(String[] args)    {          // Pre-compute Van Eck's sequence        vanEckSequence();          int n = 5;          // Print count of the occurrence of nth term        // in first n terms of the sequence        System.out.println(getCount(n));          n = 11;          // Print count of the occurrence of nth term        // in first n terms of the sequence        System.out.println(getCount(n));    }}
# Python3 program to count the occurrence # of nth term in first n terms # of Van Eck's sequence MAX = 10000sequence = [0] * (MAX + 1);   # Utility function to compute # Van Eck's sequence def vanEckSequence() :      # Loop to generate sequence     for i in range(MAX) :           # Check if sequence[i] has occured         # previously or is new to sequence         for j in range( i - 1, -1, -1) :             if (sequence[j] == sequence[i]) :                                  # If occurrence found                 # then the next term will be                 # how far back this last term                 # occured previously                 sequence[i + 1] = i - j;                 break;   # Utility function to count # the occurrence of nth term # in first n terms of the sequence def getCount(n) :       # Initialize count as 1     count = 1;       i = n - 1;       while (sequence[i + 1] != 0) :          # Increment count if (i+1)th term         # is non-zero         count += 1;           # Previous occurrence of sequence[i]         # will be it (i - sequence[i+1])th position         i = i - sequence[i + 1];       # Return the count of occurrence     return count;   # Driver code if __name__ == "__main__" :       # Pre-compute Van Eck's sequence     vanEckSequence();       n = 5;       # Print count of the occurrence of nth term     # in first n terms of the sequence     print(getCount(n));       n = 11;       # Print count of the occurrence of nth term     # in first n terms of the sequence     print(getCount(n)) ;   # This code is contributed by AnkitRai01
// C# program to count the occurrence// of nth term in first n terms// of Van Eck's sequence  using System;class GFG {      static int MAX = 100000;    static int[] sequence = new int[MAX + 1];      // Utility function to compute    // Van Eck's sequence    static void vanEckSequence()    {          // Initialize sequence array        for (int i = 0; i < MAX; i++) {            sequence[i] = 0;        }          // Loop to generate sequence        for (int i = 0; i < MAX; i++) {              // Check if sequence[i] has occured            // previously or is new to sequence            for (int j = i - 1; j >= 0; j--) {                if (sequence[j] == sequence[i]) {                      // If occurrence found                    // then the next term will be                    // how far back this last term                    // occured previously                    sequence[i + 1] = i - j;                    break;                }            }        }    }      // Utility function to count    // the occurrence of nth term    // in first n terms of the sequence    static int getCount(int n)    {          // Initialize count as 1        int count = 1;        int i = n - 1;          while (sequence[i + 1] != 0) {              // Increment count if (i+1)th term            // is non-zero            count++;              // Previous occurrence of sequence[i]            // will be it (i - sequence[i+1])th position            i = i - sequence[i + 1];        }          // Return the count of occurrence        return count;    }      // Driver code    public static void Main(string[] args)    {          // Pre-compute Van Eck's sequence        vanEckSequence();          int n = 5;          // Print count of the occurrence of nth term        // in first n terms of the sequence        Console.WriteLine(getCount(n));          n = 11;          // Print count of the occurrence of nth term        // in first n terms of the sequence        Console.WriteLine(getCount(n));    }}
1
5

ankthon
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Prime Numbers
Modulo Operator (%) in C/C++ with Examples
Program for Decimal to Binary Conversion
Find all factors of a natural number | Set 1
Modulo 10^9+7 (1000000007)
Program to find sum of elements in a given array
The Knight's tour problem | Backtracking-1
Program for factorial of a number
Minimum number of jumps to reach end