VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-all-substring-of-a-number-without-any-conversion/

⇱ Print all substring of a number without any conversion - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print all substring of a number without any conversion

Last Updated : 1 Sep, 2022

Given an integer N, the task is to print all the substring of N without doing any conversion i.e converting it into a string or an array.


Examples:  

Input: N = 12345 
Output: Possible Substrings: {1, 12, 123, 1234, 12345, 2, 23, 234, 2345, 3, 34, 345, 4, 45, 5}
Input: N = 123 
Output: Possible Substrings: {1, 12, 123, 2, 23, 3}  

Approach:  

  1. Take the power of 10 according to size.
  2. Divide the number till it will become 0 and print.
  3. Then change the number to next the position of that number by taking the modulo with k.
  4. Update the no. of digits.
  5. Repeat the same process till n becomes 0.


Below is the implementation of above approach: 
 


Output: 
1
12
123
2
23
3

 

Time Complexity: O(nlogn)

Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: