# 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

**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**:- 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.

- 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] = 0; ` ` ` `arr[1] = 1; ` ` ` ` ` `// Map to store the count of ` ` ` `// current element in Golomb ` ` ` `// Sequence ` ` ` `map<` `int` `, ` `int` `> M; ` ` ` ` ` `// Store the count of 2 ` ` ` `M[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 ` ` ` `// 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; ` `} ` |

*chevron_right*

*filter_none*

## 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<Integer,Integer> M=` `new` `HashMap<Integer,Integer>(); ` ` ` ` ` `// 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

1 2 2 3 3 4 4 4 5 5 5

**Time Complexity:** O(N)

**Auxillary Space:** O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count Possible Decodings of a given Digit Sequence
- Find maximum length Snake sequence
- Construction of Longest Increasing Subsequence(LIS) and printing LIS sequence
- G-Fact 21 | Collatz Sequence
- Padovan Sequence
- Maximum sum Bi-tonic Sub-sequence
- Largest sum Zigzag sequence in a matrix
- Minimum number of deletions to make a sorted sequence
- Golomb sequence
- Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays
- Newman-Conway Sequence
- Print n terms of Newman-Conway Sequence
- Find minimum length sub-array which has given sub-sequence in it
- Check if any valid sequence is divisible by M
- Moser-de Bruijn Sequence
- Print Fibonacci sequence using 2 variables
- Sequence Alignment problem
- Gould's Sequence
- Minimum sum possible of any bracket sequence of length N
- Minimum changes required to make a Catalan Sequence

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.