# Golomb Sequence | Set 2

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Initialise the array arr[] with  as the Golomb sequence starts with 1.
2. Store the value of last index in map M.
3. For Golomb sequence till N:
• Intialise 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.
4. Print all the element of Golomb Sequence stored in array arr[].

Below is the implementation of the above approach:

## CPP

 `// C++ program to find the first ` `// N terms of Golomb Sequence ` `#include "bits/stdc++.h" ` `#define MAX 100001 ` `using` `namespace` `std; ` ` `  `// Function to print the Golomb ` `// Sequence ` `void` `printGolombSequence(``int` `N) ` `{ ` ` `  `    ``// Initialise the array ` `    ``int` `arr[MAX]; ` ` `  `    ``// Initialise the cnt to 0 ` `    ``int` `cnt = 0; ` ` `  `    ``// First and second element ` `    ``// of Golomb Sequence is 0, 1 ` `    ``arr = 0; ` `    ``arr = 1; ` ` `  `    ``// Map to store the count of ` `    ``// current element in Golomb ` `    ``// Sequence ` `    ``map<``int``, ``int``> M; ` ` `  `    ``// Store the count of 2 ` `    ``M = 2; ` ` `  `    ``// Iterate over 2 to N ` `    ``for` `(``int` `i = 2; i <= N; i++) { ` ` `  `        ``// If cnt is equals to 0 ` `        ``// then we have new number ` `        ``// for Golomb Sequence ` `        ``// which is 1 + previous ` `        ``// element ` `        ``if` `(cnt == 0) { ` `            ``arr[i] = 1 + arr[i - 1]; ` `            ``cnt = M[arr[i]]; ` `            ``cnt--; ` `        ``} ` ` `  `        ``// Else the current element ` `        ``// is the previous element ` `        ``// in this Sequence ` `        ``else` `{ ` `            ``arr[i] = arr[i - 1]; ` `            ``cnt--; ` `        ``} ` ` `  `        ``// Map the current index to ` `        ``// current value in arr[] ` `        ``M[i] = arr[i]; ` `    ``} ` ` `  `    ``// Print the Golomb Sequence ` `    ``for` `(``int` `i = 1; i <= N; i++) { ` `        ``cout << arr[i] << ``' '``; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `N = 11; ` `    ``printGolombSequence(N); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find the first ` `// N terms of Golomb Sequence ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `static` `int` `MAX = ``1000``; ` ` `  `// Function to print the Golomb ` `// Sequence ` `static` `void` `printGolombSequence(``int` `N) ` `{ ` ` `  `    ``// Initialise the array ` `    ``int` `[]arr = ``new` `int``[MAX]; ` `    ``for``(``int` `i = ``0``; i < MAX; i++) ` `    ``arr[i] = ``0``; ` `     `  `    ``// Initialise the cnt to 0 ` `    ``int` `cnt = ``0``; ` ` `  `    ``// First and second element ` `    ``// of Golomb Sequence is 0, 1 ` `    ``arr[``0``] = ``0``; ` `    ``arr[``1``] = ``1``; ` ` `  `    ``// Map to store the count of ` `    ``// current element in Golomb ` `    ``// Sequence ` `    ``Map M=``new` `HashMap();  ` `     `  `    ``// Store the count of 2 ` `    ``M.put(``2``,``2``); ` ` `  `    ``// Iterate over 2 to N ` `    ``for` `(``int` `i = ``2``; i <= N; i++) { ` ` `  `        ``// If cnt is equals to 0 ` `        ``// then we have new number ` `        ``// for Golomb Sequence 1 2 2 3 3 4 4 4 5 5 5 ` `        ``// which is 1 + previous ` `        ``// element ` `        ``if` `(cnt == ``0``) { ` `            ``arr[i] = ``1` `+ arr[i - ``1``]; ` `            ``cnt = M.get(arr[i]); ` `            ``cnt--; ` `        ``} ` ` `  `        ``// Else the current element ` `        ``// is the previous element ` `        ``// in this Sequence ` `        ``else` `{ ` `            ``arr[i] = arr[i - ``1``]; ` `            ``cnt--; ` `        ``} ` ` `  `        ``// Map the current index to ` `        ``// current value in arr[] ` `            ``M.put(i, arr[i]); ` `    ``} ` ` `  `    ``// Print the Golomb Sequence ` `    ``for` `(``int` `i = ``1``; i <= N; i++) ` `    ``{ ` `        ``System.out.print(arr[i]+``" "``); ` `    ``} ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `N = ``11``; ` `    ``printGolombSequence(N); ` `} ` `} ` ` `  `// This code is contributed by Surendra_Gangwar `

## Python3

 `# Python3 program to find the first ` `# N terms of Golomb Sequence ` `MAX` `=` `100001` ` `  `# Function to print the Golomb ` `# Sequence ` `def` `printGolombSequence(N): ` ` `  `    ``# Initialise the array ` `    ``arr ``=` `[``0``] ``*` `MAX` ` `  `    ``# Initialise the cnt to 0 ` `    ``cnt ``=` `0` ` `  `    ``# First and second element ` `    ``# of Golomb Sequence is 0, 1 ` `    ``arr[``0``] ``=` `0` `    ``arr[``1``] ``=` `1` ` `  `    ``# Map to store the count of ` `    ``# current element in Golomb ` `    ``# Sequence ` `    ``M ``=` `dict``() ` ` `  `    ``# Store the count of 2 ` `    ``M[``2``] ``=` `2` ` `  `    ``# Iterate over 2 to N ` `    ``for` `i ``in` `range``(``2``, N ``+` `1``): ` ` `  `        ``# If cnt is equals to 0 ` `        ``# then we have new number ` `        ``# for Golomb Sequence ` `        ``# which is 1 + previous ` `        ``# element ` `        ``if` `(cnt ``=``=` `0``): ` `            ``arr[i] ``=` `1` `+` `arr[i ``-` `1``] ` `            ``cnt ``=` `M[arr[i]] ` `            ``cnt ``-``=` `1` ` `  `        ``# Else the current element ` `        ``# is the previous element ` `        ``# in this Sequence ` `        ``else``: ` `            ``arr[i] ``=` `arr[i ``-` `1``] ` `            ``cnt ``-``=` `1` ` `  `        ``# Map the current index to ` `        ``# current value in arr[] ` `        ``M[i] ``=` `arr[i] ` ` `  `    ``# Print the Golomb Sequence ` `    ``for` `i ``in` `range``(``1``, N ``+` `1``): ` `        ``print``(arr[i], end``=``" "``) ` ` `  `# Driver Code ` `N ``=` `11` `printGolombSequence(N) ` ` `  `# This code is contributed by mohit kumar 29 `

Output:

```1 2 2 3 3 4 4 4 5 5 5
```

Time Complexity: O(N)
Auxiliary Space: O(N)

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.

My Personal Notes arrow_drop_up

## Recommended Posts: Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.