Given a number N. The task is to find the first N terms of the Golomb Sequence. Golomb sequence is a non-decreasing integer sequence where the n-th term is equal to the number of times n appears in the sequence.
Input: N = 11
Output: 1 2 2 3 3 4 4 4 5 5 5
Explanation:
The first term is 1. 1 appears only once.
The second term is 2. 2 appears two times.
The third term is 2. 3 appears two times.
The fourth term is 3. 4 appears three times.
the fifth term is 3. 5 appears three times.
Input: N = 6
Output: 1 2 2 3 3 4
Approach:
- Initialise the array arr[] with [1] as the Golomb sequence starts with 1.
- Store the value of last index in map M.
- For Golomb sequence till N:
- Initialise cnt = 0 and map the .
- If cnt is not equals to 0 then, then current element in Golomb sequence is the previous element in the sequence and decrease the cnt.
- Else the current element in Golomb sequence is equals to 1 + previous element in the sequence and cnt is updated as the value of map at current element in Golomb sequence and decrease the cnt.
- Map the current index with the current value at Golomb Sequence.
- Print all the element of Golomb Sequence stored in array arr[].
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
#define MAX 100001
using namespace std;
void printGolombSequence( int N)
{
int arr[MAX];
int cnt = 0;
arr[0] = 0;
arr[1] = 1;
map< int , int > M;
M[2] = 2;
for ( int i = 2; i <= N; i++) {
if (cnt == 0) {
arr[i] = 1 + arr[i - 1];
cnt = M[arr[i]];
cnt--;
}
else {
arr[i] = arr[i - 1];
cnt--;
}
M[i] = arr[i];
}
for ( int i = 1; i <= N; i++) {
cout << arr[i] << ' ' ;
}
}
int main()
{
int N = 11;
printGolombSequence(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int MAX = 1000 ;
static void printGolombSequence( int N)
{
int []arr = new int [MAX];
for ( int i = 0 ; i < MAX; i++)
arr[i] = 0 ;
int cnt = 0 ;
arr[ 0 ] = 0 ;
arr[ 1 ] = 1 ;
Map<Integer,Integer> M= new HashMap<Integer,Integer>();
M.put( 2 , 2 );
for ( int i = 2 ; i <= N; i++) {
if (cnt == 0 ) {
arr[i] = 1 + arr[i - 1 ];
cnt = M.get(arr[i]);
cnt--;
}
else {
arr[i] = arr[i - 1 ];
cnt--;
}
M.put(i, arr[i]);
}
for ( int i = 1 ; i <= N; i++)
{
System.out.print(arr[i]+ " " );
}
}
public static void main(String args[])
{
int N = 11 ;
printGolombSequence(N);
}
}
|
Python3
MAX = 100001
def printGolombSequence(N):
arr = [ 0 ] * MAX
cnt = 0
arr[ 0 ] = 0
arr[ 1 ] = 1
M = dict ()
M[ 2 ] = 2
for i in range ( 2 , N + 1 ):
if (cnt = = 0 ):
arr[i] = 1 + arr[i - 1 ]
cnt = M[arr[i]]
cnt - = 1
else :
arr[i] = arr[i - 1 ]
cnt - = 1
M[i] = arr[i]
for i in range ( 1 , N + 1 ):
print (arr[i], end = " " )
N = 11
printGolombSequence(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int MAX = 1000;
static void printGolombSequence( int N)
{
int [] arr = new int [MAX];
for ( int i = 0; i < MAX; i++)
arr[i] = 0;
int cnt = 0;
arr[0] = 0;
arr[1] = 1;
Dictionary< int ,
int > M = new Dictionary< int ,
int >();
M.Add(2, 2);
for ( int i = 2; i <= N; i++)
{
if (cnt == 0)
{
arr[i] = 1 + arr[i - 1];
cnt = M[arr[i]];
cnt--;
}
else
{
arr[i] = arr[i - 1];
cnt--;
}
if (M.ContainsKey(i))
{
M[i] = arr[i];
}
else
{
M.Add(i, arr[i]);
}
}
for ( int i = 1; i <= N; i++)
{
Console.Write(arr[i] + " " );
}
}
static void Main()
{
int N = 11;
printGolombSequence(N);
}
}
|
Javascript
<script>
var MAX = 100001;
function printGolombSequence(N)
{
var arr = Array(MAX);
var cnt = 0;
arr[0] = 0;
arr[1] = 1;
var M = new Map();
M.set(2, 2);
for ( var i = 2; i <= N; i++) {
if (cnt == 0) {
arr[i] = 1 + arr[i - 1];
cnt = M.get(arr[i]);
cnt--;
}
else {
arr[i] = arr[i - 1];
cnt--;
}
M.set(i, arr[i]);
}
for ( var i = 1; i <= N; i++) {
document.write( arr[i] + ' ' );
}
}
var N = 11;
printGolombSequence(N);
</script>
|
Output:
1 2 2 3 3 4 4 4 5 5 5
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
22 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...