Find first K characters in Nth term of the Thue-Morse sequence
Given two integers N and K, the task is to print the first K bits of the Nth term of the Thue-Morse sequence. The Thue-Morse sequence is a binary sequence. It starts with a “0” as its first term. And then after the next term is generated by replacing “0” with “01” and “1” with “10”.
Examples:
Input: N = 3, K = 2
Output: 01
Explanation: The 1st term is “0”.
The 2nd term is obtained by replacing “0” with “01” i.e. 2nd term is “01”.
The 3rd term in the sequence is obtained by replacing “0” with “01” and “1” with “10”.
So the 3rd term becomes “0110”. Hence, the 1st 2 characters of the 3rd term is “01”.
Input: N = 4, K = 7
Output: 0110100
Approach: The basic approach to solve this problem is to generate the Nth term of the sequence and print the first K characters of that term. This can be done using the algorithm discussed here.
Time Complexity: O(N * 2N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by observing that the ith term is the concatenation of (i – 1)th term and the inverse of (i – 1)th term where inverse means changing the polarity of all bits in a binary integer. Hence, xth term, Ai[x] = Ai-1[x – 1] if (x < 2i-1), otherwise Ai[x] = !Ai-1[x – 2i-1]. Therefore, using this relation, a recursive function can be created to calculate the value of each bit in the Nth term.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findDig( int N, long K, int curr)
{
if (N == 0) {
return curr;
}
long middle = ( long ) pow (2, N) / 2;
if (K <= middle) {
return findDig(N - 1, K, curr);
}
else {
if (curr == 0) {
curr = 1;
}
else {
curr = 0;
}
return findDig(N - 1,
K - middle, curr);
}
}
void firstKTerms( int N, int K)
{
for ( int i = 1; i <= K; ++i)
{
cout << (findDig(N, i, 0));
}
}
int main() {
int N = 4;
int K = 7;
firstKTerms(N, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int findDig( int N, long K,
int curr)
{
if (N == 0 ) {
return curr;
}
long middle = ( long )Math.pow( 2 , N) / 2 ;
if (K <= middle) {
return findDig(N - 1 , K, curr);
}
else {
if (curr == 0 ) {
curr = 1 ;
}
else {
curr = 0 ;
}
return findDig(N - 1 ,
K - middle, curr);
}
}
public static void firstKTerms( int N, int K)
{
for ( int i = 1 ; i <= K; ++i) {
System.out.print(findDig(N, i, 0 ));
}
}
public static void main(String args[])
{
int N = 4 ;
int K = 7 ;
firstKTerms(N, K);
}
}
|
Python3
def findDig(N, K, curr):
if (N = = 0 ):
return curr
middle = pow ( 2 , N) / / 2
if (K < = middle):
return findDig(N - 1 , K, curr)
else :
if (curr = = 0 ):
curr = 1
else :
curr = 0
return findDig(N - 1 , K - middle, curr)
def firstKTerms(N, K):
for i in range ( 1 , K + 1 ):
print (findDig(N, i, 0 ), end = "")
if __name__ = = "__main__" :
N = 4
K = 7
firstKTerms(N, K)
|
C#
using System;
class GFG
{
public static int findDig( int N, long K, int curr)
{
if (N == 0)
{
return curr;
}
long middle = ( long )Math.Pow(2, N) / 2;
if (K <= middle)
{
return findDig(N - 1, K, curr);
}
else
{
if (curr == 0)
{
curr = 1;
}
else
{
curr = 0;
}
return findDig(N - 1,
K - middle, curr);
}
}
public static void firstKTerms( int N, int K)
{
for ( int i = 1; i <= K; ++i)
{
Console.Write(findDig(N, i, 0));
}
}
public static void Main()
{
int N = 4;
int K = 7;
firstKTerms(N, K);
}
}
|
Javascript
<script>
function findDig(N, K, curr)
{
if (N == 0) {
return curr;
}
let middle = Math.floor(Math.pow(2, N) / 2);
if (K <= middle) {
return findDig(N - 1, K, curr);
}
else {
if (curr == 0) {
curr = 1;
}
else {
curr = 0;
}
return findDig(N - 1,
K - middle, curr);
}
}
function firstKTerms(N, K) {
for (let i = 1; i <= K; ++i) {
document.write(findDig(N, i, 0));
}
}
let N = 4;
let K = 7;
firstKTerms(N, K);
</script>
|
Time Complexity: O(K*log N)
Auxiliary Space: O(1)
Last Updated :
23 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...