Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Look-And-Say Sequence

Easy
0/40
Average time to solve is 15m
profile
Contributed by
97 upvotes
Asked in companies
BarclaysMicrosoftMakeMyTrip

Problem statement

The Look-And-Say sequence is a sequence of positive integers. The sequence is as follows:

1, 11, 21, 1211, 111221, 312211, 13112221,...

This sequence is constructed in the following way:

The first number is 1.

This is read as “One 1”. 
Hence, the second number will be 11.

The second number is read as “Two 1s”. 
Hence, the third number will be 21.

The third number is read as “One 2, One 1”. 
Hence, the fourth number will be 1211. And so on.

The fourth term is read as “One 1, One 2, Two 1s”.

Hence, the fifth term will be 111221. And so on.

Given an integer N, find the Nth term of the sequence.

Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <= T <= 30
1 <= N <= 40

Where 'T' is the number of test cases and 'N' is the given sequence index.

Time Limit: 1 sec
Sample Input 1:
3
1
2
3
Sample Output 1:
1
11
21
Explanation for Sample 1:
The first term is 1.

The second term is 11.

The third term is 21.
Sample Input 2:
1
6
Sample Output 2:
312211
Full screen
Console