Thue–Morse sequence, or Prouhet–Thue–Morse sequence, is an infinite binary sequence of 0s and 1s. The sequence is obtained by starting with 0 and successively appending the Boolean complement of the sequence obtained so far.
First few steps :
Start with 0
Append complement of 0, we get 01
Append complement of 01, we get 0110
Append complement of 0110, we get 01101001
Given a whole number n. The task is to find the nth string formed of by Thue–Morse sequence i.e prefix of length 2n-1 of Thue–Morse sequence.
Input : n = 4 Output : 01101001 We get 0, 01, 0110 and 01101001 in fourth iteration. Input : n = 3 Output : 0110
The idea is to initialize the output string with 0, then run a loop n – 1 times and for each iteration find the complement of the string and append it to the string.
Below is implementation of this approach:
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.
- Convert an unbalanced bracket sequence to a balanced sequence
- Find a valid parenthesis sequence of length K from a given valid parenthesis sequence
- Given a sequence of words, print all anagrams together | Set 1
- Given a sequence of words, print all anagrams together | Set 2
- Count Possible Decodings of a given Digit Sequence
- Check if a given sequence of moves for a robot is circular or not
- Look-and-Say Sequence
- Farey Sequence
- Form minimum number from given sequence
- Find Recurring Sequence in a Fraction
- String with additive sequence
- G-Fact 21 | Collatz Sequence
- Complete the sequence generated by a polynomial
- Juggler Sequence
- Padovan Sequence
- Aliquot Sequence
- Recaman's sequence
- Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array | Set-2
- Second most repeated word in a sequence
- Lexicographically smallest rotated sequence | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.