Count the occurrence of Nth term in first N terms of Van Eck’s sequence
Last Updated :
02 Mar, 2023
Prerequisite: Van Eck’s sequence Given a positive integer N, the task is to count the occurrences of Nth term in the 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 = 1 Input: 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.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 100000
int sequence[MAX + 1];
void vanEckSequence()
{
for ( int i = 0; i < MAX; i++) {
sequence[i] = 0;
}
for ( int i = 0; i < MAX; i++) {
for ( int j = i - 1; j >= 0; j--) {
if (sequence[j] == sequence[i]) {
sequence[i + 1] = i - j;
break ;
}
}
}
}
int getCount( int n)
{
int nthTerm = sequence[n - 1];
int count = 0;
for ( int i = 0; i < n; i++) {
if (sequence[i] == nthTerm)
count++;
}
return count;
}
int main()
{
vanEckSequence();
int n = 5;
cout << getCount(n) << endl;
n = 11;
cout << getCount(n) << endl;
return 0;
}
|
Java
class GFG {
static int MAX = 100000 ;
static int sequence[] = new int [MAX + 1 ];
static void vanEckSequence()
{
for ( int i = 0 ; i < MAX; i++) {
sequence[i] = 0 ;
}
for ( int i = 0 ; i < MAX; i++) {
for ( int j = i - 1 ; j >= 0 ; j--) {
if (sequence[j] == sequence[i]) {
sequence[i + 1 ] = i - j;
break ;
}
}
}
}
static int getCount( int n)
{
int nthTerm = sequence[n - 1 ];
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
if (sequence[i] == nthTerm)
count++;
}
return count;
}
public static void main(String[] args)
{
vanEckSequence();
int n = 5 ;
System.out.println(getCount(n));
n = 11 ;
System.out.println(getCount(n));
}
}
|
Python3
MAX = 10000
sequence = [ 0 ] * ( MAX + 1 );
def vanEckSequence() :
for i in range ( MAX ) :
for j in range (i - 1 , - 1 , - 1 ) :
if (sequence[j] = = sequence[i]) :
sequence[i + 1 ] = i - j;
break ;
def getCount(n) :
nthTerm = sequence[n - 1 ];
count = 0 ;
for i in range (n) :
if (sequence[i] = = nthTerm) :
count + = 1 ;
return count;
if __name__ = = "__main__" :
vanEckSequence();
n = 5 ;
print (getCount(n));
n = 11 ;
print (getCount(n));
|
C#
using System;
class GFG {
static int MAX = 100000;
static int [] sequence = new int [MAX + 1];
static void vanEckSequence()
{
for ( int i = 0; i < MAX; i++) {
sequence[i] = 0;
}
for ( int i = 0; i < MAX; i++) {
for ( int j = i - 1; j >= 0; j--) {
if (sequence[j] == sequence[i]) {
sequence[i + 1] = i - j;
break ;
}
}
}
}
static int getCount( int n)
{
int nthTerm = sequence[n - 1];
int count = 0;
for ( int i = 0; i < n; i++) {
if (sequence[i] == nthTerm)
count++;
}
return count;
}
public static void Main()
{
vanEckSequence();
int n = 5;
Console.WriteLine(getCount(n));
n = 11;
Console.WriteLine(getCount(n));
}
}
|
Javascript
const MAX = 10000;
let sequence = new Array(MAX + 1).fill(0);
function vanEckSequence() {
for (let i = 0; i <= MAX; i++) {
for (let j = i - 1; j >= 0; j--) {
if (sequence[j] == sequence[i]) {
sequence[i + 1] = i - j;
break ;
}
}
}
}
function getCount(n) {
let nthTerm = sequence[n - 1];
let count = 0;
for (let i = 0; i < n; i++) {
if (sequence[i] == nthTerm) {
count++;
}
}
return count;
}
vanEckSequence();
let n = 5;
console.log(getCount(n));
n = 11;
console.log(getCount(n));
|
- 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 the count and do the same for (N- SN+1)th term
- And if SN+1 is zero then stop.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 100000
int sequence[MAX + 1];
void vanEckSequence()
{
for ( int i = 0; i < MAX; i++) {
sequence[i] = 0;
}
for ( int i = 0; i < MAX; i++) {
for ( int j = i - 1; j >= 0; j--) {
if (sequence[j] == sequence[i]) {
sequence[i + 1] = i - j;
break ;
}
}
}
}
int getCount( int n)
{
int count = 1;
int i = n - 1;
while (sequence[i + 1] != 0) {
count++;
i = i - sequence[i + 1];
}
return count;
}
int main()
{
vanEckSequence();
int n = 5;
cout << getCount(n) << endl;
n = 11;
cout << getCount(n) << endl;
return 0;
}
|
Java
class GFG {
static int MAX = 100000 ;
static int sequence[] = new int [MAX + 1 ];
static void vanEckSequence()
{
for ( int i = 0 ; i < MAX; i++) {
sequence[i] = 0 ;
}
for ( int i = 0 ; i < MAX; i++) {
for ( int j = i - 1 ; j >= 0 ; j--) {
if (sequence[j] == sequence[i]) {
sequence[i + 1 ] = i - j;
break ;
}
}
}
}
static int getCount( int n)
{
int count = 1 ;
int i = n - 1 ;
while (sequence[i + 1 ] != 0 ) {
count++;
i = i - sequence[i + 1 ];
}
return count;
}
public static void main(String[] args)
{
vanEckSequence();
int n = 5 ;
System.out.println(getCount(n));
n = 11 ;
System.out.println(getCount(n));
}
}
|
Python3
MAX = 10000
sequence = [ 0 ] * ( MAX + 1 );
def vanEckSequence() :
for i in range ( MAX ) :
for j in range ( i - 1 , - 1 , - 1 ) :
if (sequence[j] = = sequence[i]) :
sequence[i + 1 ] = i - j;
break ;
def getCount(n) :
count = 1 ;
i = n - 1 ;
while (sequence[i + 1 ] ! = 0 ) :
count + = 1 ;
i = i - sequence[i + 1 ];
return count;
if __name__ = = "__main__" :
vanEckSequence();
n = 5 ;
print (getCount(n));
n = 11 ;
print (getCount(n)) ;
|
C#
using System;
class GFG {
static int MAX = 100000;
static int [] sequence = new int [MAX + 1];
static void vanEckSequence()
{
for ( int i = 0; i < MAX; i++) {
sequence[i] = 0;
}
for ( int i = 0; i < MAX; i++) {
for ( int j = i - 1; j >= 0; j--) {
if (sequence[j] == sequence[i]) {
sequence[i + 1] = i - j;
break ;
}
}
}
}
static int getCount( int n)
{
int count = 1;
int i = n - 1;
while (sequence[i + 1] != 0) {
count++;
i = i - sequence[i + 1];
}
return count;
}
public static void Main( string [] args)
{
vanEckSequence();
int n = 5;
Console.WriteLine(getCount(n));
n = 11;
Console.WriteLine(getCount(n));
}
}
|
Javascript
let MAX = 100000;
let sequence = new Array(MAX + 1);
function vanEckSequence()
{
for ( var i = 0; i < MAX; i++) {
sequence[i] = 0;
}
for ( var i = 0; i < MAX; i++) {
for ( var j = i - 1; j >= 0; j--) {
if (sequence[j] == sequence[i]) {
sequence[i + 1] = i - j;
break ;
}
}
}
}
function getCount(n)
{
let count = 1;
let i = n - 1;
while (sequence[i + 1] != 0) {
count++;
i = i - sequence[i + 1];
}
return count;
}
vanEckSequence();
let n = 5;
console.log(getCount(n));
n = 11;
console.log(getCount(n));
|
Time Complexity: O(MAX*MAX)
Auxiliary Space: O(MAX)
Share your thoughts in the comments
Please Login to comment...