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 = 5
Output: 1
Explanation:
First 5 terms of Van Eck’s Sequence 0, 0, 1, 0, 2
Occurrence of 5th term i.e 2 = 1Input: 11
Output: 5
Explanation:
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 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++
// C++ program to count the occurrence// of nth term in first n terms// of Van Eck's sequence#include <bits/stdc++.h>usingnamespacestd;#define MAX 100000intsequence[MAX + 1];// Utility function to compute// Van Eck's sequencevoidvanEckSequence(){// Initialize sequence arrayfor(inti = 0; i < MAX; i++) {sequence[i] = 0;}// Loop to generate sequencefor(inti = 0; i < MAX; i++) {// Check if sequence[i] has occured// previously or is new to sequencefor(intj = 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 previouslysequence[i + 1] = i - j;break;}}}}// Utility function to count// the occurrence of nth term// in first n terms of the sequenceintgetCount(intn){// Get nth term of the sequenceintnthTerm = sequence[n - 1];intcount = 0;// Count the occurrence of nth term// in first n terms of the sequencefor(inti = 0; i < n; i++) {if(sequence[i] == nthTerm)count++;}// Return countreturncount;}// Driver codeintmain(){// Pre-compute Van Eck's sequencevanEckSequence();intn = 5;// Print count of the occurrence of nth term// in first n terms of the sequencecout << getCount(n) << endl;n = 11;// Print count of the occurrence of nth term// in first n terms of the sequencecout << getCount(n) << endl;return0;}chevron_rightfilter_noneJava
// Java program to count the occurrence// of nth term in first n terms// of Van Eck's sequenceclassGFG {staticintMAX =100000;staticintsequence[] =newint[MAX +1];// Utility function to compute// Van Eck's sequencestaticvoidvanEckSequence(){// Initialize sequence arrayfor(inti =0; i < MAX; i++) {sequence[i] =0;}// Loop to generate sequencefor(inti =0; i < MAX; i++) {// Check if sequence[i] has occured// previously or is new to sequencefor(intj = 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 previouslysequence[i +1] = i - j;break;}}}}// Utility function to count// the occurrence of nth term// in first n terms of the sequencestaticintgetCount(intn){// Get nth term of the sequenceintnthTerm = sequence[n -1];intcount =0;// Count the occurrence of nth term// in first n terms of the sequencefor(inti =0; i < n; i++) {if(sequence[i] == nthTerm)count++;}// Return countreturncount;}// Driver codepublicstaticvoidmain(String[] args){// Pre-compute Van Eck's sequencevanEckSequence();intn =5;// Print count of the occurrence of nth term// in first n terms of the sequenceSystem.out.println(getCount(n));n =11;// Print count of the occurrence of nth term// in first n terms of the sequenceSystem.out.println(getCount(n));}}chevron_rightfilter_nonePython3
# Python3 program to count the occurrence# of nth term in first n terms# of Van Eck's sequenceMAX=10000sequence=[0]*(MAX+1);# Utility function to compute# Van Eck's sequencedefvanEckSequence() :# Loop to generate sequenceforiinrange(MAX) :# Check if sequence[i] has occured# previously or is new to sequenceforjinrange(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 previouslysequence[i+1]=i-j;break;# Utility function to count# the occurrence of nth term# in first n terms of the sequencedefgetCount(n) :# Get nth term of the sequencenthTerm=sequence[n-1];count=0;# Count the occurrence of nth term# in first n terms of the sequenceforiinrange(n) :if(sequence[i]==nthTerm) :count+=1;# Return countreturncount;# Driver codeif__name__=="__main__":# Pre-compute Van Eck's sequencevanEckSequence();n=5;# Print count of the occurrence of nth term# in first n terms of the sequenceprint(getCount(n));n=11;# Print count of the occurrence of nth term# in first n terms of the sequenceprint(getCount(n));# This code is contributed by AnkitRai01chevron_rightfilter_noneC#
// C# program to count the occurrence// of nth term in first n terms// of Van Eck's sequenceusingSystem;classGFG {staticintMAX = 100000;staticint[] sequence =newint[MAX + 1];// Utility function to compute// Van Eck's sequencestaticvoidvanEckSequence(){// Initialize sequence arrayfor(inti = 0; i < MAX; i++) {sequence[i] = 0;}// Loop to generate sequencefor(inti = 0; i < MAX; i++) {// Check if sequence[i] has occured// previously or is new to sequencefor(intj = 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 previouslysequence[i + 1] = i - j;break;}}}}// Utility function to count// the occurrence of nth term// in first n terms of the sequencestaticintgetCount(intn){// Get nth term of the sequenceintnthTerm = sequence[n - 1];intcount = 0;// Count the occurrence of nth term// in first n terms of the sequencefor(inti = 0; i < n; i++) {if(sequence[i] == nthTerm)count++;}// Return countreturncount;}// Driver codepublicstaticvoidMain(){// Pre-compute Van Eck's sequencevanEckSequence();intn = 5;// Print count of the occurrence of nth term// in first n terms of the sequenceConsole.WriteLine(getCount(n));n = 11;// Print count of the occurrence of nth term// in first n terms of the sequenceConsole.WriteLine(getCount(n));}}chevron_rightfilter_noneOutput: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 SN
- If SN+1 is non-zero then increment count
and 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
// C++ program to count the occurrence// of nth term in first n terms// of Van Eck's sequence#include <bits/stdc++.h>usingnamespacestd;#define MAX 100000intsequence[MAX + 1];// Utility function to compute// Van Eck's sequencevoidvanEckSequence(){// Initialize sequence arrayfor(inti = 0; i < MAX; i++) {sequence[i] = 0;}// Loop to generate sequencefor(inti = 0; i < MAX; i++) {// Check if sequence[i] has occured// previously or is new to sequencefor(intj = 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 previouslysequence[i + 1] = i - j;break;}}}}// Utility function to count// the occurrence of nth term// in first n terms of the sequenceintgetCount(intn){// Initialize count as 1intcount = 1;inti = n - 1;while(sequence[i + 1] != 0) {// Increment count if (i+1)th term// is non-zerocount++;// Previous occurrence of sequence[i]// will be it (i - sequence[i+1])th positioni = i - sequence[i + 1];}// Return the count of occurrencereturncount;}// Driver codeintmain(){// Pre-compute Van Eck's sequencevanEckSequence();intn = 5;// Print count of the occurrence of nth term// in first n terms of the sequencecout << getCount(n) << endl;n = 11;// Print count of the occurrence of nth term// in first n terms of the sequencecout << getCount(n) << endl;return0;}chevron_rightfilter_noneJava
// Java program to count the occurrence// of nth term in first n terms// of Van Eck's sequenceclassGFG {staticintMAX =100000;staticintsequence[] =newint[MAX +1];// Utility function to compute// Van Eck's sequencestaticvoidvanEckSequence(){// Initialize sequence arrayfor(inti =0; i < MAX; i++) {sequence[i] =0;}// Loop to generate sequencefor(inti =0; i < MAX; i++) {// Check if sequence[i] has occured// previously or is new to sequencefor(intj = 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 previouslysequence[i +1] = i - j;break;}}}}// Utility function to count// the occurrence of nth term// in first n terms of the sequencestaticintgetCount(intn){// Initialize count as 1intcount =1;inti = n -1;while(sequence[i +1] !=0) {// Increment count if (i+1)th term// is non-zerocount++;// Previous occurrence of sequence[i]// will be it (i - sequence[i+1])th positioni = i - sequence[i +1];}// Return the count of occurrencereturncount;}// Driver codepublicstaticvoidmain(String[] args){// Pre-compute Van Eck's sequencevanEckSequence();intn =5;// Print count of the occurrence of nth term// in first n terms of the sequenceSystem.out.println(getCount(n));n =11;// Print count of the occurrence of nth term// in first n terms of the sequenceSystem.out.println(getCount(n));}}chevron_rightfilter_nonePython3
# Python3 program to count the occurrence# of nth term in first n terms# of Van Eck's sequenceMAX=10000sequence=[0]*(MAX+1);# Utility function to compute# Van Eck's sequencedefvanEckSequence() :# Loop to generate sequenceforiinrange(MAX) :# Check if sequence[i] has occured# previously or is new to sequenceforjinrange( 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 previouslysequence[i+1]=i-j;break;# Utility function to count# the occurrence of nth term# in first n terms of the sequencedefgetCount(n) :# Initialize count as 1count=1;i=n-1;while(sequence[i+1] !=0) :# Increment count if (i+1)th term# is non-zerocount+=1;# Previous occurrence of sequence[i]# will be it (i - sequence[i+1])th positioni=i-sequence[i+1];# Return the count of occurrencereturncount;# Driver codeif__name__=="__main__":# Pre-compute Van Eck's sequencevanEckSequence();n=5;# Print count of the occurrence of nth term# in first n terms of the sequenceprint(getCount(n));n=11;# Print count of the occurrence of nth term# in first n terms of the sequenceprint(getCount(n)) ;# This code is contributed by AnkitRai01chevron_rightfilter_noneC#
// C# program to count the occurrence// of nth term in first n terms// of Van Eck's sequenceusingSystem;classGFG {staticintMAX = 100000;staticint[] sequence =newint[MAX + 1];// Utility function to compute// Van Eck's sequencestaticvoidvanEckSequence(){// Initialize sequence arrayfor(inti = 0; i < MAX; i++) {sequence[i] = 0;}// Loop to generate sequencefor(inti = 0; i < MAX; i++) {// Check if sequence[i] has occured// previously or is new to sequencefor(intj = 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 previouslysequence[i + 1] = i - j;break;}}}}// Utility function to count// the occurrence of nth term// in first n terms of the sequencestaticintgetCount(intn){// Initialize count as 1intcount = 1;inti = n - 1;while(sequence[i + 1] != 0) {// Increment count if (i+1)th term// is non-zerocount++;// Previous occurrence of sequence[i]// will be it (i - sequence[i+1])th positioni = i - sequence[i + 1];}// Return the count of occurrencereturncount;}// Driver codepublicstaticvoidMain(string[] args){// Pre-compute Van Eck's sequencevanEckSequence();intn = 5;// Print count of the occurrence of nth term// in first n terms of the sequenceConsole.WriteLine(getCount(n));n = 11;// Print count of the occurrence of nth term// in first n terms of the sequenceConsole.WriteLine(getCount(n));}}chevron_rightfilter_noneOutput:1 5
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.
Recommended Posts:
- Program to find Nth term of the Van Eck's Sequence
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Nth term of given recurrence relation having each term equal to the product of previous K terms
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Sum of P terms of an AP if Mth and Nth terms are given
- Find Pth term of a GP if Mth and Nth terms are given
- Number of quadruples where the first three terms are in AP and last three terms are in GP
- Print the sequence of size N in which every term is sum of previous K terms
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Finding nth term of any Polynomial Sequence
- Find nth term of the Dragon Curve Sequence
- Sum of first N terms of Quadratic Sequence 3 + 7 + 13 + ...
- Print first N terms of Lower Wythoff sequence
- Count sequences of length K having each term divisible by its preceding term
- Ratio of mth and nth terms of an A. P. with given ratio of sums
- Sum of nth terms of Modified Fibonacci series made by every pair of two arrays
- Sum of series till N-th term whose i-th term is i^k - (i-1)^k
- Print N terms of Withoff Sequence
- Program to print the sum of the given nth term
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.
Improved By : AnkitRai01

