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
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
- Initialise the array arr with  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:
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.
- 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 email@example.com. 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.