A PHP Error was encountered

Severity: Warning

Message: fopen(/var/cpanel/php/sessions/ea-php56/PHPSESSID7n3t47mn6h1jdqa286lhoprr97j5t640): failed to open stream: No space left on device

Filename: drivers/Session_files_driver.php

Line Number: 174

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot send session cookie - headers already sent by (output started at /home/nerd/public_html/system/core/Exceptions.php:271)

Filename: Session/Session.php

Line Number: 144

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/nerd/public_html/system/core/Exceptions.php:271)

Filename: Session/Session.php

Line Number: 144

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/nerd/public_html/system/core/Exceptions.php:271)

Filename: controllers/Questions.php

Line Number: 268

For a given n, Print the count and say sequence
Q:

For a given n, Print the count and say sequence

0

Count and Say sequence

The count-and-say sequence is the sequence of integers with the first five terms as following:

  1. 1
  2. 11
  3. 21
  4. 1211
  5. 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.

That means every integer (repeated continuously) is read off with its count value.

For a given n, Print the count and say sequence.

All Answers

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/nerd/public_html/system/core/Exceptions.php:271)

Filename: questions/question_for_visitors.php

Line Number: 568

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <bits/stdc++.h>
using namespace std;

void countAndSay(int n) {
	//base cases
	if(n==0)
		return "";
	if(n==1)
		return "1";
	//for rest of the cases, it must start from 1   
	//initialize result with string "1"
	string result="1";
	cout<<result<<endl;
	string temp;//temporary string to hold levelwise result
	for(int i=1;i<n;i++){ //iterate for n-1 rows
		//iterate upto current string length
		int len=result.length();
		for(int j=0;j<len;j++){
			int count=1;//initialize count as 1
			//find count for repeated number
			while(j+1<len && result[j]==result[j+1] ){
				count++;
				j++;
			}
			//convert the count to string and add to 
			//temprary result, then add original no
			temp+=to_string(count)+result[j];
		}
		//assign temporary result to original result 
		//& print for current level 
		result=temp;
		cout<<result<<endl;
		//clear the temporary result
		temp="";
	}
}

int main(){
	int n;
	
	cout<<"count and Say problem.....\n";
	cout<<"enter n, no of rows\n";
	cin>>n;
	//function to print count and say sequence
	coutAndSay(n);
	
	return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now

A PHP Error was encountered

Severity: Warning

Message: Unknown: Failed to write session data (user). Please verify that the current setting of session.save_path is correct (/var/cpanel/php/sessions/ea-php56)

Filename: Unknown

Line Number: 0