# Given input n,find out the largest number with "n" digits

Page 1 of 1

## 2 Replies - 4603 Views - Last Post: 11 March 2008 - 10:05 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=45831&amp;s=035317ff6aaef7a2c4671a27367238b6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 skan_33

Reputation: 0
• Posts: 9
• Joined: 23-February 08

# Given input n,find out the largest number with "n" digits

Posted 11 March 2008 - 05:04 AM

Hi all,
I got one assingment to do,which I did but my code is hardcoded ,but my processor wants generic code.

The requirement for the assingment is-------->
Given input n,find out the largest number with "n" digits such that:
a. All the digits in its are just 1 or 2 or 3.
b. No two adjacent substrings of the given number is same
some of the examples are :3 323 3231 3231323

Examples of the numbers that do not fit the requirements are:33 3232 3223 321321
* 33 as 3 follows 3.
* 3232, because the substring "32" is present adjacent to the substring "32"
* 3223, because the substring "2" follows "2".
* 321321 because the substring "321" is adjacent to substring "321"

321312 is a good number because there are no two adjacent substrings repeating
themselves. But given n = 6,it is not the largest 6- digit number we are looking for.
The largest 6-digit number that fits our requirments would be 323132

Below is my code
```#include<stdio.h>
#include<string.h>

char str[20] = "323132131232132";

main() {
int i,k,n,count = 0;
char *ptr;
ptr = str;
printf("Enter no\n");
scanf("%d",&n);
//printf("%d->", n);
for(k = 1; k <= n; k++) {
printf("%c",*ptr);
ptr++;
if(*ptr == '\0') {
count++;
for(i = 0;i <= strlen(str);i++) {
switch(count) {
case 1: ptr = &str[2];
break;
case 2: ptr = &str[7];
break;
case 3:ptr = &str[8];
count =0;
break;
}
}
}
}
printf("\n");
}

```

So plz help me out in making generic code as soon as possible

This post has been edited by skan_33: 11 March 2008 - 05:33 AM

Is This A Good Question/Topic? 0

## Replies To: Given input n,find out the largest number with "n" digits

### #2 Nykc

• Gentleman of Leisure

Reputation: 737
• Posts: 8,648
• Joined: 14-September 07

## Re: Given input n,find out the largest number with "n" digits

Posted 11 March 2008 - 05:41 AM

Hi skan 33, Welcome to Dream.In.Code! you might want to post this in the appropriate forum whereas this is for introductions only and not alot of people are going to see it.

Best of luck!

### #3 skan_33

Reputation: 0
• Posts: 9
• Joined: 23-February 08

## Re: Given input n,find out the largest number with "n" digits

Posted 11 March 2008 - 10:05 PM

Thanks for info,but I bit confused for where to post the question exactly...
Can you let me know the procedure of how and where to post the questions