Generate all numbers up to N in Lexicographical Order

Given an integer N, the task is to print all numbers up to N in Lexicographical order.
Examples: 

Input: N = 15 
Output: 
1 10 11 12 13 14 15 2 3 4 5 6 7 8 9
Input: N = 19 
Output: 
1 10 11 12 13 14 15 16 17 18 19 2 3 4 5 6 7 8 9 

Approach: 
In order to solve the problem, follow the steps below: 

  • Iterate from 1 to N and store all the numbers in the form of strings.
  • Sort the vector containing the strings.

Below is the implementation of the above approach: 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to implement the
// above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print all the
// numbers up to n in
// lexicographical order
void lexNumbers(int n)
{
    vector<string> s;
  
    for (int i = 1; i <= n; i++) {
        s.push_back(to_string(i));
    }
  
    sort(s.begin(), s.end());
    vector<int> ans;
    for (int i = 0; i < n; i++)
        ans.push_back(stoi(s[i]));
  
    for (int i = 0; i < n; i++)
        cout << ans[i] << " ";
}
// Driver Program
int main()
{
  
    int n = 15;
    lexNumbers(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to implement the
// above approach
import java.util.*;
class GFG{
  
// Function to print all the
// numbers up to n in
// lexicographical order
static void lexNumbers(int n)
{
    Vector<String> s = new Vector<String>();
  
    for (int i = 1; i <= n; i++)
    {
        s.add(String.valueOf(i));
    }
  
    Collections.sort(s);
    Vector<Integer> ans = new Vector<Integer>();
    for (int i = 0; i < n; i++)
        ans.add(Integer.valueOf(s.get(i)));
  
    for (int i = 0; i < n; i++)
        System.out.print(ans.get(i) + " ");
}
// Driver Program
public static void main(String[] args)
{
    int n = 15;
    lexNumbers(n);
}
}
  
// This code is contributed by sapnasingh4991

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# above approach
  
# Function to print all the 
# numbers up to n in 
# lexicographical order 
def lexNumbers(n):
      
    s = []
    for i in range(1, n + 1):
        s.append(str(i))
          
    s.sort()
    ans = []
      
    for i in range(n):
        ans.append(int(s[i])) 
  
    for i in range(n):
        print(ans[i], end = ' ')
          
# Driver Code 
if __name__ == "__main__"
      
    n = 15
    lexNumbers(n)
      
# This code is contributed by Ediga_Manisha

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement the
// above approach
using System;
using System.Collections.Generic;
  
class GFG{
  
// Function to print all the
// numbers up to n in
// lexicographical order
static void lexNumbers(int n)
{
    List<String> s = new List<String>();
  
    for(int i = 1; i <= n; i++)
    {
       s.Add(String.Join("", i));
    }
  
    s.Sort();
    List<int> ans = new List<int>();
      
    for(int i = 0; i < n; i++)
       ans.Add(Int32.Parse(s[i]));
  
    for(int i = 0; i < n; i++)
       Console.Write(ans[i] + " ");
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 15;
      
    lexNumbers(n);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output

1 10 11 12 13 14 15 2 3 4 5 6 7 8 9 

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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.