Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8010393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:43:20+00:00 2026-06-04T18:43:20+00:00

Given a problem, the count-and-say sequence is the sequence of integers beginning as follows:

  • 0
Given a problem, the count-and-say sequence is the sequence of integers beginning as     follows:
1, 11, 21, 1211, 111221, ...

1 is read off as “one 1” or 11.
11 is read off as “two 1s” or 21.
21 is read off as “one 2, then one 1” or 1211.
Given an integer n, generate the nth sequence.

I have made the solution as by scanning each string and generating next string accordingly
It takes time as O(nm)
where m is the length of the largest string
and n is the number given

Here it the code

void countnsay(char str[][1000],int n)
{
int i=1,k;
int j=0;
while(i<=(n-1))
{
//scan str[i-1]
j=0;
k=0;        //for building the new string array 
while(j<strlen(str[i-1]) )
    {
    char c=str[i-1][j];
    int cnt=1;

    while(c==str[i-1][++j])
    cnt++;

    str[i][k++]=cnt+48;
    str[i][k++]=c;

    }
str[i][k]='\0';
i++;

}
printf("%s",str[n-1]);  
}




int main()
{
int n=5;
char str[1000][1000];
strcpy(str[0],"1");
countnsay(str,n);
return 0;
}

Could there be a more better solution to this problem?
Please give some suggestions/hints.
Thanx in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:43:22+00:00Added an answer on June 4, 2026 at 6:43 pm

    You can use dynamic programming. After some time, you’ll encounter already existing substrings which were already computed. You can keep a map of already computed sequences:

    1 -> 11
    11 -> 21
    

    Now you need to compute 1211.

    You first go for 1, which you already know is 11.

    The you encounter 2. You don’t have this, so compute 12 and add it to the map:

    2 -> 12
    

    The you encounter 11, which, again, you have in your map as 21.

    Concatenate these and you get

    111221
    

    and the new map

    1 -> 11
    11 -> 21
    2 -> 12
    

    EDIT: You only look for consecutive identical numbers, so only keep those in the map.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem Given two integers a, b, a < b. Display its decimal expansion. You
Problem Given the following two tables, I'd like to select all Ids for Posts
My given problem is follow: I have an object with x columns and every
Problem Given a boolean expression consisting of the symbols 0, 1, &, |, ^
First the practical application that led me to the problem: Given a set of
Please help me out with an algorithm for the following problem - Given a
Given the following problem , I'd appreciate for any corrections since I have no
The problem is : Given a number of programming libraries with similar or equal
if given a graph problem how do we know whether we need to use
I've come across a tuples problem where given a list of pair tuples it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.