Related Articles
Print all numbers up to N in words in lexicographical order
• Last Updated : 24 Nov, 2020

Given an integer N, the task is to print all numbers from 1 to N ( N < 100000) in words in lexicographical order.

Examples :

Input: N = 11
Output: eight, eleven, five, four, nine, one, seven, six, three, two
Explanation:
The numbers from 1 to N is 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11.
Their respective representations in words are {one, two, three, four, five, six, seven, eight, nine, ten, eleven}.
Their correct lexicographical order is {eight, eleven, eleven, five, four, nine, one, seven, six, three, two}.

Input: N = 5
Output: five, four, one, three, two
Explanation:
The numbers from 1 to N is 1, 2, 3, 4, 5.
Their respective representations in words are {one, two, three, four, five}.
Their correct lexicographical order is {five, four, one, three, two}.

Approach: Follow the steps below to solve the problem:

1. Initialize an array arr[] of size N + 1 which stores the strings representations of each index from 1 to N at respective indices.
2. Convert all numbers from 1 to N to words and store them at their corresponding indices.
3. Sort the array arr[] in ascending order.
4. Print the elements present in the array arr[].

Below is the implementation of the above approach:

## Java

 `// Java program for the above approach`   `import` `java.util.*;` `class` `GFG {`   `    ``// Function to convert a number to words` `    ``static` `String convert_to_words(``int` `n)` `    ``{` `        ``// Stores the digits` `        ``char` `num[] = String.valueOf(n)` `                         ``.toCharArray();` `        ``int` `len = num.length;` `        ``String ans = ``""``;`   `        ``// Base cases` `        ``if` `(len == ``0``) {` `            ``ans += ``"Empty String"``;` `            ``return` `ans;` `        ``}`   `        ``// Stores strings of unit place` `        ``String[] single_digits = ``new` `String[] {` `            ``"zero"``, ``"one"``, ``"two"``, ``"three"``, ``"four"``,` `            ``"five"``, ``"six"``, ``"seven"``, ``"eight"``, ``"nine"` `        ``};`   `        ``// Stores strings for corner cases` `        ``String[] two_digits = ``new` `String[] {` `            ``""``, ``"ten"``, ``"eleven"``, ``"twelve"``,` `            ``"thirteen"``, ``"fourteen"``, ``"fifteen"``, ``"sixteen"``,` `            ``"seventeen"``, ``"eighteen"``, ``"nineteen"` `        ``};`   `        ``// Stores strings for ten's place digits` `        ``String[] tens_multiple = ``new` `String[] {` `            ``""``, ``""``, ``"twenty"``, ``"thirty"``, ``"forty"``,` `            ``"fifty"``, ``"sixty"``, ``"seventy"``, ``"eighty"``, ``"ninety"` `        ``};`   `        ``// Stores strings for powers of 10` `        ``String[] tens_power` `            ``= ``new` `String[] { ``"hundred"``, ``"thousand"` `};`   `        ``// If given number contains a single digit` `        ``if` `(len == ``1``) {` `            ``ans += single_digits[num[``0``] - ``'0'``];` `            ``return` `ans;` `        ``}`   `        ``// Iterate over all the digits` `        ``int` `x = ``0``;` `        ``while` `(x < num.length) {`   `            ``// Represent first 2 digits in words` `            ``if` `(len >= ``3``) {` `                ``if` `(num[x] - ``'0'` `!= ``0``) {`   `                    ``ans += single_digits[num[x] - ``'0'``];` `                    ``ans += ``" "``;` `                    ``ans += tens_power[len - ``3``];` `                    ``ans += ``" "``;` `                ``}`   `                ``--len;` `            ``}`   `            ``// Represent last 2 digits in words` `            ``else` `{`   `                ``// Explicitly handle corner cases [10, 19]` `                ``if` `(num[x] - ``'0'` `== ``1``) {` `                    ``int` `sum = num[x] - ``'0'` `+ num[x] - ``'0'``;` `                    ``ans += two_digits[sum];` `                    ``return` `ans;` `                ``}`   `                ``// Explicitly handle corner case 20` `                ``else` `if` `(num[x] - ``'0'` `== ``2` `                         ``&& num[x + ``1``] - ``'0'` `== ``0``) {` `                    ``ans += ``"twenty"``;` `                    ``return` `ans;` `                ``}`   `                ``// For rest of the two digit` `                ``// numbers i.e., 21 to 99` `                ``else` `{` `                    ``int` `i = (num[x] - ``'0'``);` `                    ``if` `(i > ``0``) {` `                        ``ans += tens_multiple[i];` `                        ``ans += ``" "``;` `                    ``}` `                    ``else` `                        ``ans += ``""``;` `                    ``++x;` `                    ``if` `(num[x] - ``'0'` `!= ``0``)` `                        ``ans += single_digits[num[x] - ``'0'``];` `                ``}` `            ``}` `            ``++x;` `        ``}` `        ``return` `""``;` `    ``}`   `    ``// Function to print all the numbers` `    ``// up to n in lexicographical order` `    ``static` `void` `lexNumbers(``int` `n)` `    ``{` `        ``Vector s = ``new` `Vector();`   `        ``// Convert all numbers in words` `        ``for` `(``int` `i = ``1``; i <= n; i++) {` `            ``s.add(convert_to_words(i));` `        ``}`   `        ``// Sort all strings` `        ``Collections.sort(s);` `        ``Vector ans` `            ``= ``new` `Vector();`   `        ``for` `(``int` `i = ``0``; i < n; i++)` `            ``ans.add(s.get(i));`   `        ``// Print answer` `        ``for` `(``int` `i = ``0``; i < n - ``1``; i++)` `            ``System.out.print(` `                ``ans.get(i) + ``", "``);`   `        ``System.out.print(ans.get(n - ``1``));` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `n = ``5``;` `        ``lexNumbers(n);` `    ``}` `}`

## Python3

 `# Python3 program for the ` `# above approach`   `# Function to convert a number ` `# to words` `def` `convert_to_words(n):`   `    ``# Stores the digits` `    ``num ``=` `str``(n)` `    ``length ``=` `len``(num)` `    ``ans ``=` `""`   `    ``# Base cases` `    ``if` `(length ``=``=` `0``):` `        ``ans ``+``=` `"Empty String"` `        ``return` `ans`   `    ``# Stores strings of unit place` `    ``single_digits ``=` `[``"zero"``, ``"one"``, ``"two"``, ` `                     ``"three"``, ``"four"``, ``"five"``,` `                     ``"six"``, ``"seven"``, ``"eight"``, ``"nine"``]`   `    ``# Stores strings for corner cases` `    ``two_digits ``=` `["``", "``ten``", "``eleven", ` `                  ``"twelve"``, ``"thirteen"``, ` `                  ``"fourteen"``, ``"fifteen"``,` `                  ``"sixteen"``, ``"seventeen"``, ` `                  ``"eighteen"``, ``"nineteen"``]`   `    ``# Stores strings for ten's place digits` `    ``tens_multiple ``=` `["``", "``", "``twenty",` `                     ``"thirty"``, ``"forty"``, ` `                     ``"fifty"``, ``"sixty"``,` `                     ``"seventy"``, ``"eighty"``, ` `                     ``"ninety"``]`   `    ``# Stores strings for powers of 10` `    ``tens_power ``=` `[``"hundred"``, ``"thousand"``]`   `    ``# If given number contains a ` `    ``# single digit` `    ``if` `(length ``=``=` `1``):` `        ``ans ``+``=` `single_digits[``ord``(num[``0``]) ``-` `                             ``ord``(``'0'``)]` `        ``return` `ans`   `    ``# Iterate over all the digits` `    ``x ``=` `0` `    `  `    ``while` `(x < ``len``(num)):`   `        ``# Represent first 2 digits ` `        ``# in words` `        ``if` `(length >``=` `3``) :` `            ``if` `(num[x] ``-` `'0'` `!``=` `0``):`   `                ``ans ``+``=` `single_digits[``ord``(num[x]) ``-` `                                     ``ord``(``'0'``)]` `                ``ans ``+``=` `" "` `                ``ans ``+``=` `tens_power[``len` `-` `3``]` `                ``ans ``+``=` `" "`   `            ``length ``-``=` `1`   `        ``# Represent last 2 digits in words` `        ``else` `:`   `            ``# Explicitly handle corner ` `            ``# cases[10, 19]` `            ``if` `(``ord``(num[x]) ``-` `                ``ord``(``'0'``) ``=``=` `1``):` `                ``sum` `=` `(``ord``(num[x]) ``-` `ord``(``'0'` `) ``+` `                       ``ord``(num[x]) ``-` `ord``(``'0'``))` `                ``ans ``+``=` `two_digits[``sum``]` `                ``return` `ans` `            `  `            ``# Explicitly handle corner ` `            ``# case 20` `            ``elif` `(``ord``(num[x]) ``-` `                  ``ord``(``'0'``) ``=``=` `2` `and` `                  ``ord``(num[x ``+` `1``]) ``-` `                  ``ord``(``'0'``) ``=``=` `0``):` `                ``ans ``+``=` `"twenty"` `                ``return` `ans`   `            ``# For rest of the two digit` `            ``# numbers i.e., 21 to 99` `            ``else``:` `                ``i ``=` `(``ord``(num[x]) ``-` `                     ``ord``(``'0'``))` `                ``if` `(i > ``0``) :` `                    ``ans ``+``=` `tens_multiple[i]` `                    ``ans ``+``=` `" "` `                `  `                ``else``:` `                  ``ans ``+``=` `""` `                  ``x ``+``=` `1` `                  ``if` `(``ord``(num[x]) ``-` `                      ``ord``(``'0'``) !``=` `0``):` `                      ``ans ``+``=` `single_digits[``ord``(num[x]) ``-` `                                         ``ord``(``'0'``)]` `        ``x ``+``=` `1` `    ``return` `""`   `# Function to print all the numbers` `# up to n in lexicographical order` `def` `lexNumbers(n):` `  `  `    ``s ``=` `[]`   `    ``# Convert all numbers in ` `    ``# words` `    ``for` `i ``in` `range``(``1``, n ``+` `1``):` `        ``s.append(convert_to_words(i))`   `    ``# Sort all strings` `    ``s.sort()` `    ``ans ``=` `[]`   `    ``for` `i ``in` `range``(n):` `        ``ans.append(s[i])`   `    ``# Print answer` `    ``for` `i ``in` `range``(n ``-` `1``):` `         ``print``(ans[i], end ``=` `", "``)`   `    ``print``(ans[n ``-` `1``], end ``=` `"")`   `# Driver Code` `if` `__name__ ``=``=` `"__main__"``:` `  `  `    ``n ``=` `5` `    ``lexNumbers(n)`   `# This code is contributed by Chitranayal`

## C#

 `// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;`   `class` `GFG{`   `// Function to convert a number to words` `static` `String convert_to_words(``int` `n)` `{` `    `  `    ``// Stores the digits` `    ``char` `[]num = String.Join(``""``, n).ToCharArray();` `    ``int` `len = num.Length;` `    ``String ans = ``""``;`   `    ``// Base cases` `    ``if` `(len == 0)` `    ``{` `        ``ans += ``"Empty String"``;` `        ``return` `ans;` `    ``}`   `    ``// Stores strings of unit place` `    ``String[] single_digits = ``new` `String[]{` `        ``"zero"``, ``"one"``, ``"two"``, ``"three"``, ``"four"``,` `        ``"five"``, ``"six"``, ``"seven"``, ``"eight"``, ``"nine"` `};`   `    ``// Stores strings for corner cases` `    ``String[] two_digits = ``new` `String[]{` `        ``""``, ``"ten"``, ``"eleven"``, ``"twelve"``,` `        ``"thirteen"``, ``"fourteen"``, ``"fifteen"``, ``"sixteen"``,` `        ``"seventeen"``, ``"eighteen"``, ``"nineteen"` `};`   `    ``// Stores strings for ten's place digits` `    ``String[] tens_multiple = ``new` `String[]{` `        ``""``, ``""``, ``"twenty"``, ``"thirty"``, ``"forty"``,` `        ``"fifty"``, ``"sixty"``, ``"seventy"``, ``"eighty"``, ``"ninety"` `};`   `    ``// Stores strings for powers of 10` `    ``String[] tens_power = ``new` `String[]{ ``"hundred"``, ` `                                        ``"thousand"` `};`   `    ``// If given number contains a single digit` `    ``if` `(len == 1) ` `    ``{` `        ``ans += single_digits[num - ``'0'``];` `        ``return` `ans;` `    ``}`   `    ``// Iterate over all the digits` `    ``int` `x = 0;` `    ``while` `(x < num.Length)` `    ``{` `        `  `        ``// Represent first 2 digits in words` `        ``if` `(len >= 3) ` `        ``{` `            ``if` `(num[x] - ``'0'` `!= 0) ` `            ``{` `                ``ans += single_digits[num[x] - ``'0'``];` `                ``ans += ``" "``;` `                ``ans += tens_power[len - 3];` `                ``ans += ``" "``;` `            ``}` `            ``--len;` `        ``}`   `        ``// Represent last 2 digits in words` `        ``else` `        ``{` `            `  `            ``// Explicitly handle corner cases [10, 19]` `            ``if` `(num[x] - ``'0'` `== 1) ` `            ``{` `                ``int` `sum = num[x] - ``'0'` `+ ` `                          ``num[x] - ``'0'``;` `                ``ans += two_digits[sum];` `                ``return` `ans;` `            ``}`   `            ``// Explicitly handle corner case 20` `            ``else` `if` `(num[x] - ``'0'` `== 2 && ` `                     ``num[x + 1] - ``'0'` `== 0)` `            ``{` `                ``ans += ``"twenty"``;` `                ``return` `ans;` `            ``}`   `            ``// For rest of the two digit` `            ``// numbers i.e., 21 to 99` `            ``else` `            ``{` `                ``int` `i = (num[x] - ``'0'``);` `                ``if` `(i > 0)` `                ``{` `                    ``ans += tens_multiple[i];` `                    ``ans += ``" "``;` `                ``}` `                ``else` `                    ``ans += ``""``;` `                    `  `                ``++x;` `                `  `                ``if` `(num[x] - ``'0'` `!= 0)` `                    ``ans += single_digits[num[x] - ``'0'``];` `            ``}` `        ``}` `        ``++x;` `    ``}` `    ``return` `""``;` `}`   `// Function to print all the numbers` `// up to n in lexicographical order` `static` `void` `lexNumbers(``int` `n)` `{` `    ``List s = ``new` `List();`   `    ``// Convert all numbers in words` `    ``for``(``int` `i = 1; i <= n; i++) ` `    ``{` `        ``s.Add(convert_to_words(i));` `    ``}`   `    ``// Sort all strings` `    ``s.Sort();` `    `  `    ``List ans = ``new` `List();`   `    ``for``(``int` `i = 0; i < n; i++)` `        ``ans.Add(s[i]);`   `    ``// Print answer` `    ``for``(``int` `i = 0; i < n - 1; i++)` `        ``Console.Write(ans[i] + ``", "``);`   `    ``Console.Write(ans[n - 1]);` `}`   `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` `    ``int` `n = 5;` `    `  `    ``lexNumbers(n);` `}` `}`   `// This code is contributed by Rajput-Ji`

Output:

```five, four, one, three, two

```

Time Complexity: O(NlogN) where N is the given integer.
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :