Gijswijt’s sequence is a self-describing sequence where the value of each term is equal to the maximum number of repeated blocks of numbers in the sequence preceding the number.
Let us take the ith term of the sequence a(i), Then for this Gijswijt’s Sequence:
where k is the largest natural number such that the word a(1)a(2)..a(n) can be represented as x*(y^k) where the length of y is non zero.
The Gijswijt’s sequence is as follows:
1, 1, 2, 1, 1, 2, 2, 2, 3, 1, ...
It can be proved that every natural number occurs in this sequence at least once but the sequence has very slow growth. The 220th term is 4 whereas the term 5 occurs in the sequence near 10^10^23th term.
Examples:
Input: n = 10
Output: 1, 1, 2, 1, 1, 2, 2, 2, 3, 1
Input: n = 220
Output: 1, 1, 2, 1, 1, 2, 2, 2, 3, 1, …, 3, 2, 1, 1, 2, 1, 1, 2, 2, 2, 3, 1, 1, 2, 1, 1, 2, 2, 2, 3, 2, 2, 2, 3, 2, 2, 2, 3, 3, 2, 2, 2, 3, 2, 2, 2, 3, 2, 2, 2, 3, 3, 2, 2, 2, 3, 2, 2, 2, 3, 2, 2, 2, 3, 3, 3, 3, 4,
Approach: We have to print the first n terms of the series. We will keep a vector which will store all the previous element of the sequence the next term will be maximum count of repeated blocks of any length. So we will vary the length from 1 to n-1 and then find out the count using a loop.
Example:
- let the sequence be 1, 1, 2, 1, 1, 2
- Take values of i from 0 to 5 first pattern contains “2”.
- 2 is repeated only once from the end of array, next pattern is “1, 2”.
- 1, 2 is repeated only once from the end of array, the next pattern is “1, 1, 2”.
- 1, 1, 2 is repeated twice from the end of array so the count is 2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int find_count(vector< int > ele)
{
int count = 0;
for ( int i = 0; i < ele.size(); i++) {
vector< int > p;
int c = 0;
for ( int j = ele.size() - 1;
j >= (ele.size() - 1 - i) && j >= 0;
j--)
p.push_back(ele[j]);
int j = ele.size() - 1, k = 0;
while (j >= 0) {
if (ele[j] != p[k])
break ;
j--;
k++;
if (k == p.size()) {
c++;
k = 0;
}
}
count = max(count, c);
}
return count;
}
void solve( int n)
{
int count = 1;
vector< int > ele;
for ( int i = 0; i < n; i++) {
cout << count << ", " ;
ele.push_back(count);
count = find_count(ele);
}
}
int main()
{
int n = 10;
solve(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int find_count(Vector<Integer> ele)
{
int count = 0 ;
for ( int i = 0 ; i < ele.size(); i++)
{
Vector<Integer> p = new Vector<Integer>();
int c = 0 ;
for ( int j = ele.size() - 1 ;
j >= (ele.size() - 1 - i) && j >= 0 ;
j--)
{
p.add(ele.get(j));
}
int j = ele.size() - 1 , k = 0 ;
while (j >= 0 )
{
if (ele.get(j) != p.get(k))
{
break ;
}
j--;
k++;
if (k == p.size())
{
c++;
k = 0 ;
}
}
count = Math.max(count, c);
}
return count;
}
static void solve( int n)
{
int count = 1 ;
Vector<Integer> ele = new Vector<Integer>();
for ( int i = 0 ; i < n; i++)
{
System.out.print(count + ", " );
ele.add(count);
count = find_count(ele);
}
}
public static void main(String[] args)
{
int n = 10 ;
solve(n);
}
}
|
Python3
def find_count(ele):
count = 0
for i in range ( len (ele)):
p = []
c = 0
j = len (ele) - 1
while j > = ( len (ele) - 1 - i) and j > = 0 :
p.append(ele[j])
j - = 1
j = len (ele) - 1
k = 0
while j > = 0 :
if ele[j] ! = p[k]:
break
j - = 1
k + = 1
if k = = len (p):
c + = 1
k = 0
count = max (count, c)
return count
def solve(n):
count = 1
ele = []
for i in range (n):
print (count, end = " " )
ele.append(count)
count = find_count(ele)
if __name__ = = "__main__" :
n = 10
solve(n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int find_count(List< int > ele)
{
int count = 0;
for ( int i = 0; i < ele.Count; i++)
{
List< int > p = new List< int >();
int c = 0, j;
for (j = ele.Count - 1;
j >= (ele.Count - 1 - i) && j >= 0;
j--)
{
p.Add(ele[j]);
}
j = ele.Count - 1;
int k = 0;
while (j >= 0)
{
if (ele[j] != p[k])
{
break ;
}
j--;
k++;
if (k == p.Count)
{
c++;
k = 0;
}
}
count = Math.Max(count, c);
}
return count;
}
static void solve( int n)
{
int count = 1;
List< int > ele = new List< int >();
for ( int i = 0; i < n; i++)
{
Console.Write(count + ", " );
ele.Add(count);
count = find_count(ele);
}
}
public static void Main(String[] args)
{
int n = 10;
solve(n);
}
}
|
Javascript
<script>
function find_count(ele)
{
let count = 0;
for (let i = 0; i < ele.length; i++) {
let p = [];
let c = 0;
for (let j = ele.length - 1;
j >= (ele.length - 1 - i) && j >= 0;
j--)
p.push(ele[j]);
let j = ele.length - 1, k = 0;
while (j >= 0) {
if (ele[j] != p[k])
break ;
j--;
k++;
if (k == p.length) {
c++;
k = 0;
}
}
count = Math.max(count, c);
}
return count;
}
function solve(n)
{
let count = 1;
let ele = [];
for (let i = 0; i < n; i++) {
document.write(count + ", " );
ele.push(count);
count = find_count(ele);
}
}
let n = 10;
solve(n);
</script>
|
Output:
1, 1, 2, 1, 1, 2, 2, 2, 3, 1,
Last Updated :
11 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...