Generating All Increasing Digits

I came across the following interview question.

For a given number of digits, generate all numbers so that the value of a higher order digit is less than a lower order digit.

145 // 1 <4 <5

Is there a better (efficient) way to do this than the one I came up with:

public static void GenerateSpecialNumbers(int noDigits) { int prod = 1; for(int i=0; i < noDigits; i++) { prod = prod * 10; } int minValue = prod/10; int maxValue = prod - 1; for(int i = minValue; i < maxValue; i++) { bool isValid = true; int num = i; int max = int.MaxValue; while(num > 0) { int digit = num % 10; if(digit >= max) { isValid = false; break; } max = digit; num = num/10; } if(isValid) Console.WriteLine(i); } } 

EDIT: Output for 3 digits:

123 124 125 126 127 128 129 134 135 136 137 138 139 145 146 147 148 149 156 157 158 159 167 168 169 178 179 189 234 235 236 237 238 239 245 246 247 248 249 256 257 258 259 267 268 269 278 279 289 345 346 347 348 349 356 357 358 359 367 368 369 378 379 389 456 457 458 459 467 468 469 478 479 489 567 568 569 578 579 589 678 +679 689 789

+4
source share
7 answers

Good puzzle! Here is my trick:

 static void Main() { WriteNumbers(3); } static void WriteNumbers(int digits, int number = 0) { int i = (number % 10) + 1; number *= 10; for (; i <= 9; i++) { if (digits == 1) Console.WriteLine(number + i); else WriteNumbers(digits - 1, number + i); } } 
+3
source

Yes, this problem has a simple recursive description that builds all numbers without checking and throwing away.

For example, the actual digits of the digit n include "1" .append (all valid digits of n-1 using only digits >1 )

Each digit has a lower bound of one plus a digit immediately to the left. Can you find a simple upper bound?

+2
source
 for (i1 from 1 to 9) for (i2 from 1 to i1 - 1) for (i3 from 1 to i2 - 1) print(i1 * 1 + i2 * 10 + i3 * 100); 

No recursion is needed for fixed-length numbers. Easy to code and failsafe.

Note that the upper bounds of the loop are not fixed. That is what makes this work.

+1
source

Since I like things based on tables, I would first generate a table for n = 2 (<100 records, obviously) and just keep it in an initialized array.

Then f (n) = digits in the sequence N [1, 2, 3, 4, 5, 6, 7, 8, 9], composed with f (n-1), where f (n-1) [0]> N

 ie for n = 3: 1, f(2) where f(2)[0] > 1: 123, 124, 125, ... 2, f(2) where f(2)[0] > 2: 234, 235, 236, ... ... 
+1
source

Here is my solution using @ Hand-E-Food and @Ben Voigt. I feel this is easier to understand:

  static void WriteNumbers(int digits, int left=0,int number=0) { for(int i=left+1; i<10; i++) { if(digits==1) { Console.WriteLine(number*10+i); } else { WriteNumbers(digits - 1, i, number*10 + i); } } } 
+1
source

Here's a hint: if there are N possible numbers, and you want to choose M from them, the number of possibilities is "N choose M".

0
source

Here is a solution (albeit a hack-ish) that will work for any number of digits:

 private static void printSpecialNumbers(int noOfDigits){ int max = (int) Math.pow(10, noOfDigits); int min = (int) Math.pow(10, noOfDigits-1); for(int i = min; i < max; i++){ if(isSpecialNumber(i)) System.out.println(i); } } private static boolean isSpecialNumber(int i){ String intString = String.valueOf(i); String[] digits = intString.split(""); for(int k = 1; k < digits.length-1; k++){ if(Integer.valueOf(digits[k]) >= Integer.valueOf(digits[k+1])) return false; } return true; } 
0
source

Source: https://habr.com/ru/post/1395506/


All Articles