The Wayback Machine - https://web.archive.org/web/20240915101518/https://www.geeksforgeeks.org/print-all-substring-of-a-number-without-any-conversion/
Open In App

Print all substring of a number without any conversion

Last Updated : 01 Sep, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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: 
 

C++




// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the substrings of a number
void printSubstrings(int n)
{
    // Calculate the total number of digits
    int s = log10(n);
 
    // 0.5 has been added because of it will
    // return double value like 99.556
    int d = (int)(pow(10, s) + 0.5);
    int k = d;
 
    while (n) {
 
        // Print all the numbers from
        // starting position
        while (d) {
            cout << n / d << endl;
            d = d / 10;
        }
 
        // Update the no.
        n = n % k;
 
        // Update the no.of digits
        k = k / 10;
        d = k;
    }
}
 
// Driver code
int main()
{
    int n = 123;
    printSubstrings(n);
 
    return 0;
}


Java




// Java implementation
// of above approach
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
// Function to print the
// substrings of a number
static void printSubstrings(int n)
{
    // Calculate the total
    // number of digits
    int s = (int)Math.log10(n);
 
    // 0.5 has been added because
    // of it will return double
    // value like 99.556
    int d = (int)(Math.pow(10, s) + 0.5);
    int k = d;
 
    while (n > 0)
    {
 
        // Print all the numbers
        // from starting position
        while (d > 0)
        {
            System.out.println(n / d);
            d = d / 10;
        }
 
        // Update the no.
        n = n % k;
 
        // Update the no.of digits
        k = k / 10;
        d = k;
    }
}
 
// Driver code
public static void main(String args[])
{
    int n = 123;
    printSubstrings(n);
}
}
 
// This code is contributed
// by Subhadeep


Python3




# Python3 implementation of above approach
import math
 
# Function to print the substrings of a number
def printSubstrings(n):
     
    # Calculate the total number of digits
    s = int(math.log10(n));
 
    # 0.5 has been added because of it will
    # return double value like 99.556
    d = (math.pow(10, s));
    k = d;
 
    while (n > 0):
 
        # Print all the numbers from
        # starting position
        while (d > 0):
            print(int(n // d));
            d = int(d / 10);
 
        # Update the no.
        n = int(n % k);
 
        # Update the no.of digits
        k = int(k // 10);
        d = k;
 
# Driver code
if __name__ == '__main__':
    n = 123;
    printSubstrings(n);
 
# This code is contributed by Rajput-Ji


C#




// C# implementation
// of above approach
using System;
 
class GFG
{
// Function to print the
// substrings of a number
static void printSubstrings(int n)
{
    // Calculate the total
    // number of digits
    int s = (int)Math.Log10(n);
 
    // 0.5 has been added because
    // of it will return double
    // value like 99.556
    int d = (int)(Math.Pow(10, s) + 0.5);
    int k = d;
 
    while (n > 0)
    {
 
        // Print all the numbers
        // from starting position
        while (d > 0)
        {
            Console.WriteLine(n / d);
            d = d / 10;
        }
 
        // Update the no.
        n = n % k;
 
        // Update the no.of digits
        k = k / 10;
        d = k;
    }
}
 
// Driver code
public static void Main()
{
    int n = 123;
    printSubstrings(n);
}
}
 
// This code is contributed
// by mits


PHP




<?php
// PHP implementation of above approach
 
// Function to print the substrings
// of a number
function printSubstrings($n)
{
    // Calculate the total number
    // of digits
    $s = (int)log10($n);
 
    // 0.5 has been added because
    // of it will return double
    // value like 99.556
    $d = (int)(pow(10, $s) + 0.5);
    $k = $d;
 
    while ($n)
    {
 
        // Print all the numbers from
        // starting position
        while ($d)
        {
            echo (int)($n / $d) . "\n";
            $d = (int)($d / 10);
        }
 
        // Update the no.
        $n = $n % $k;
 
        // Update the no.of digits
        $k = (int)($k / 10);
        $d = $k;
    }
}
 
// Driver code
$n = 123;
printSubstrings($n);
 
// This code is contributed by mits
?>


Javascript




<script>
 
// javascript implementation
// of above approach
// Function to print the
// substrings of a number
function printSubstrings(n)
{
    // Calculate the total
    // number of digits
    var s = parseInt(Math.log10(n));
 
    // 0.5 has been added because
    // of it will return double
    // value like 99.556
    var d = parseInt((Math.pow(10, s) + 0.5));
    var k = d;
 
    while (n > 0)
    {
 
        // Print all the numbers
        // from starting position
        while (d > 0)
        {
            document.write(parseInt(n / d)+"<br>");
            d = parseInt(d / 10);
        }
 
        // Update the no.
        n = n % k;
 
        // Update the no.of digits
        k = parseInt(k / 10);
        d = k;
    }
}
 
// Driver code
 
var n = 123;
printSubstrings(n);
 
 
// This code contributed by Princi Singh
</script>


Output: 

1
12
123
2
23
3

 

Time Complexity: O(nlogn)

Auxiliary Space: O(1)



Similar Reads

Check if substring S1 appear after any occurrence of substring S2 in given sentence
Given strings S1, S2 and S, the task is to check if every substring of S which is same as S1 has another substring of S same as S2 before it. It is given that S1 is always present as a substring in the string S. Examples: Input: S1 = "code", S2 = "geek", S = "sxygeeksgcodetecode"Output: TrueExplanation: Substring S2 is present before both the occur
4 min read
Longest substring whose any non-empty substring not prefix or suffix of given String
Given a string S of length N, the task is to find the length of the longest substring X of the string S such that: No non-empty substring of X is a prefix of S.No non-empty substring of X is a suffix of S.If no such string is possible, print −1. Examples: Input: S = "abcdefb"Output: 4Explanation: cdef is the substring satisfying the conditions. Inp
5 min read
Check if any permutation of a number without any leading zeros is a power of 2 or not
Given an integer N, the task is to check if any permutation of N without any leading zeros is a power of 2. If any such permutation of the given number exists, then print that permutation. Otherwise, print No. Examples: Input: N = 46Output: 64Explanation:The permutation of 46 which is perfect power of 2 is 64( = 26) Input: N = 75Output: NoExplanati
9 min read
Print Longest substring without repeating characters
Given a string, print the longest substring without repeating characters. For example, the longest substrings without repeating characters for “ABDEFGABEF” are “BDEFGA” and “DEFGAB”, with length 6. For “BBBB” the longest substring is “B”, with length 1. The desired time complexity is O(n) where n is the length of the string. Prerequisite: Length of
14 min read
Find the Longest Substring Conversion
Given two strings source and target of equal length, and two positive integers, maxCost, and conversionCost, the task is to find the indices of the longest substring in source that can be converted into the same substring in target with a cost less than or equal to maxCost, where each character conversion having a cost of conversionCost units. Note
12 min read
Print a pattern without using any loop
Given a number n, print the following pattern without using any loop. n, n-5, n-10, ..., 0, 5, 10, ..., n-5, n Examples : Input: n = 16Output: 16, 11, 6, 1, -4, 1, 6, 11, 16 Input: n = 10Output: 10, 5, 0, 5, 10 We strongly recommend that you click here and practice it, before moving on to the solution.Print a pattern without using any loop (using r
11 min read
Decimal to binary conversion without using arithmetic operators
Find the binary equivalent of the given non-negative number n without using arithmetic operators. Examples: Input : n = 10Output : 1010 Input : n = 38Output : 100110 Note that + in below algorithm/program is used for concatenation purpose. Algorithm: decToBin(n) if n == 0 return "0" Declare bin = "" Declare ch while n &gt; 0 if (n &amp; 1) == 0 ch
8 min read
Count of all cycles without any inner cycle in a given Graph
Given an undirected connected graph consisting of N vertices numbered [0, N-1] and E edges, the task is to count the number of cycles such that any subset of vertices of a cycle does not form another cycle.Examples: Input: N = 2, E = 2, edges = [{0, 1}, {1, 0}] Output: 1 Explanation: Only one cycle exists between the two vertices. Input: N = 6, E =
5 min read
Efficiently check if a string has all unique characters without using any additional data structure
Implement an space efficient algorithm to determine if a string (of characters from 'a' to 'z') has all unique characters or not. Use additional data structures like count array, hash, etc is not allowed.Expected Time Complexity : O(n) Examples : Input : str = "aaabbccdaa"Output : NoInput : str = "abcd"Output : YesBrute Force Approach: The brute fo
9 min read
Check whether the cost of going from any node to any other node via all possible paths is same
Given adjacency list representation of a directed graph, the task is to check whether the cost of going from any vertex to any other vertex via all possible paths is equal or not. If there is a cost c for going from vertex A to vertex B, then the cost to travel from vertex B to vertex A will be -c. Examples: Input: arr[][] = {{0, 2, 0, 1}, {-2, 0,
9 min read
Reduce sum of any subset of an array to 1 by multiplying all its elements by any value
Given an array arr[] consisting of N positive integers, the task is to check if the sum of the elements of any subset of the given array can be reduced to 1 after multiplying all its elements by any integer. If it is not possible to do so, then print "No". Otherwise, print "Yes". Examples: Input: arr[] = {29, 6, 4, 10}Output: YesExplanation:Choose
7 min read
Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'nd substring. Examples: Input : "11235813" Output : ["1", "1", "2", "3", "5", "8", "13"] Input : "1111223" Output : ["1", "11", "12", "23"] Input : "1111213" Output : ["11", "1", "12", "13"] Input : "11121114" Output : []Iterate through the given string by pickin
9 min read
Length of the largest substring which have character with frequency greater than or equal to half of the substring
Given a string S consisting of characters from 'a' to 'z'. The task is to find the length of the largest substring of S which contains a character whose frequency in the sub-string is greater than or equal to half of the length of the substring.Note: For odd-length substring, to calculate half-length consider integer division. For example, half of
10 min read
Minimum length of substring whose rotation generates a palindromic substring
Given a string str, the task is to find the minimum length of substring required to rotate that generates a palindromic substring from the given string. Examples: Input: str = "abcbd" Output: 0 Explanation: No palindromic substring can be generated. There is no repeated character in the string. Input: str = "abcdeba" Output: 3 Explanation: Rotate s
7 min read
Check if a string can be split into two substrings such that one substring is a substring of the other
Given a string S of length N, the task is to check if a string can be split into two substrings, say A and B such that B is a substring of A. If not possible, print No. Otherwise, print Yes. Examples : Input: S = "abcdab"Output: YesExplanation: Considering the two splits to be A="abcd" and B="ab", B is a substring of A. Input: S = "abcd"Output: No
5 min read
Count occurrences of substring X before every occurrence of substring Y in a given string
Given three strings S, X, and Y consisting of N, A, and B characters respectively, the task is to find the number of occurrences of the substring X before every occurrence of the substring Y in the given string S. Examples: Input S = ”abcdefdefabc”, X = ”def”, Y = ”abc”Output: 0 2Explanation:First occurrence of Y(= "abc"): No of occurrences of X(=
6 min read
Minimize replacement of bits to make the count of 01 substring equal to 10 substring
Given a binary string str. The task is to minimize the number of replacements of '0' by '1' or '1' by '0' to balance the binary string. A binary string is said to be balanced: "if the number of "01" substring = number of "10" substring". Examples: Input: str = "101010" Output: 1Explanation: "01" = 2 &amp; "10" = 3. So change the last character to '
4 min read
Longest Substring of A that can be changed to Substring of B in at most T cost
Given two strings A and B of the same length and two positive integers K and T. The task is to find the longest substring of A that can be converted to the same substring at the same position in B in less than or equal to T cost. Converting any character of A to any other character costs K units. Note: If multiple valid substrings are possible then
13 min read
Minimum removals to make a string concatenation of a substring of 0s followed by a substring of 1s
Given binary string str of length N​​​​, the task is to find the minimum number of characters required to be deleted from the given binary string to make a substring of 0s followed by a substring of 1s. Examples: Input: str = "00101101"Output: 2Explanation: Removing str[2] and str[6] or removing str[3] and str[6] modifies the given binary string to
11 min read
Find if a given string can be represented from a substring by iterating the substring “n” times
Given a string 'str', check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. Examples: Input: str = "abcabcabc" Output: true The given string is 3 times repetition of "abc" Input: str = "abadabad" Output: true The given string is 2 times repetition of "abad" Input: str = "aabaabaabaab" Ou
15+ min read
Print all strings in the given array that occur as the substring in the given string
Given an array of string arr[] and a string str, the task is to print all the strings in arr[] that occur as a substring in str. Example: Input: str ="geeksforgeeks", arr[] ={ "forg", "geek", "ek", "dog", "sfor"}Output: forggeekeksforExplanation: The strings "forg", "geek", "ek" and "sfor" occur as a substring in str. Therefore, the required count
5 min read
Print all occurrences of a string as a substring in another string
Given two strings, str1 and str2, the task is to print the indices(Consider, indices starting from 0) of the occurrence of str2 in str1. If no such index occurs, print "NONE". Examples: Input : GeeksforGeeks GeeksOutput : 0 8Input : GFG gOutput : NONEApproach 1 (Naive Approach): A simple solution is to check all substrings of a given string one by
12 min read
Check if a number is palindrome or not without using any extra space | Set 2
Given a number ‘n’ and our goal is to find out it is palindrome or not without using any extra space. We can’t make a new copy of number. Examples: Input: n = 2332Output: Yes it is Palindrome.Explanation:original number = 2332reversed number = 2332Both are same hence the number is palindrome. Input: n=1111Output: Yes it is Palindrome. Input: n = 12
5 min read
Divide two number using Binary search without using any / and % operator
Given two integers one is a dividend and the other is the divisor, we need to find the quotient when the dividend is divided by the divisor without the use of any " / " and " % " operators. Examples: Input: dividend = 10, divisor = 2Output: 5Explanation: 10/2 = 5. Input: dividend = 10, divisor = 3Output: 3Explanation: 10/3 = 3.33333... which is tru
7 min read
To check a number is palindrome or not without using any extra space
Given a number 'n' and our goal is to find out it is palindrome or not without using any extra space. We can't make a new copy of the number . Examples: Input : 2332 Output : Yes it is Palindrome. Explanation: original number = 2332 reversed number = 2332 Both are same hence the number is palindrome. Input :1111 Output :Yes it is Palindrome. Input
6 min read
Maximize array product by changing any array element arr[i] to (-1)*arr[i] - 1 any number of times
Given an array arr[] consisting of N integers, the task is to modify the array elements by changing each array element arr[i] to (-1)*arr[i] - 1 any number of times such that the product of the array is maximized. Examples: Input: arr[] = {-3, 0, 1}Output: 2 -1 -2Explanation:Performing the following operations on the array elements maximizes the pr
7 min read
Minimize product of first 2^K–1 Natural Numbers by swapping bits for any pair any number of times
Given a positive integer K, the task is to minimize the positive product of the first (2K - 1) Natural Numbers by swapping the bits at the corresponding position of any two numbers any number of times. Examples: Input: K = 3Output: 1512Explanation: The original product is 5040. The given array in binary notation is {001, 010, 011, 100, 101, 110, 11
5 min read
Javascript Program To Find Length Of The Longest Substring Without Repeating Characters
Given a string str, find the length of the longest substring without repeating characters.  For “ABDEFGABEF”, the longest substring are “BDEFGA” and "DEFGAB", with length 6.For “BBBB” the longest substring is “B”, with length 1.For "GEEKSFORGEEKS", there are two longest substrings shown in the below diagrams, with length 7 The desired time complexi
5 min read
Longest substring without repeating characters
Given a string s, find the length of the longest substring without repeating characters. Examples: Input: "ABCBC"Output: 3Explanation: The longest substring without repeating characters is "ABC"Input: "AAA"Output: 1Explanation: The longest substring without repeating characters is "A"Input: "GEEKSFORGEEKS"Output: 7 Explanation: The longest substrin
15+ min read
Print all the permutations of a string without repetition using Collections in Java
Given a string str, the task is to print all the permutations of str. A permutation is an arrangement of all or part of a set of objects, with regard to the order of the arrangement. A permutation should not have repeated strings in the output. Examples: Input: str = "aa" Output: aa Note that "aa" will be printed only once as duplicates are not all
2 min read
Article Tags :
Practice Tags :