VOOZH about

URL: https://www.geeksforgeeks.org/dsa/replace-0-5-input-integer/

⇱ Replace all ‘0’ with ‘5’ in an input Integer - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Replace all ‘0’ with ‘5’ in an input Integer

Last Updated : 23 Jul, 2025

Given an integer as input and replace all the ‘0’ with ‘5’ in the integer. 

Examples: 

Input: 102 
Output: 152
Explanation: All the digits which are '0' is replaced by '5' 

Input: 1020 
Output: 1525
Explanation: All the digits which are '0' is replaced by '5'

The use of an array to store all digits is not allowed.

Recommended Practice

: By observing the test cases it is evident that all the 0 digits are replaced by 5. For Example, for input = 1020, output = 1525. The idea is simple, we assign a variable 'temp' to 0, we get the last digit using mod operator '%'. If the digit is 0, we replace it with 5, otherwise, keep it as it is. Then we multiply the 'temp' with 10 and add the digit got by mod operation. After that, we divide the original number by 10 to get the other digits. In this way, we will have a number in which all the '0's are assigned with '5's. If we reverse this number, we will get the desired answer.

:

  • if the number is 0, directly return 5.
  • else do the steps below.
  • Create a variable temp= 0 to store the reversed number having all '0's assigned to '5's.
  • Find the last digit using the mod operator '%'. If the digit is '0', then make the last digit '5'.
  • Multiply temp with 10 and add the last digit.
  • Divide the number by 10 to get more digits by mod operation.
  • Then reverse this number.
  • return the reversed number.

Implementation:


Output
15125

Complexity Analysis:

  • Time Complexity: O(n), where n is number of digits in the number.
  • Auxiliary Space: O(1), no extra space is required.

  By observing the test cases it is evident that all the 0 digits are replaced by 5. For Example, for input = 1020, output = 1525, which can be written as 1020 + 505, which can be further written as 1020 + 5*(10^2) + 5*(10^0). So the solution can be formed in an iterative way where if a '0' digit is encountered find the place value of that digit and multiply it with 5 and find the sum for all 0's in the number. Add that sum to the input number to find the output number.

Algorithm: 

  • Create a variable sum = 0 to store the sum, place = 1 to store the place value of the current digit, and create a copy of the input variable
  • If the number is zero return 5
  • Iterate the next step while the input variable is greater than 0
  • Extract the last digit (n%10) and if the digit is zero, then update sum = sum + place*5, remove the last digit from the number n = n/10 and update place = place * 10
  • Return the sum.

Implementation:


Output
1525

Complexity Analysis:

  • Time Complexity: O(k), the loops run only k times, where k is the number of digits of the number.
  • Auxiliary Space: O(1), no extra space is required.

The idea is simple, we get the last digit using the mod operator '%'. If the digit is 0, we replace it with 5, otherwise, keep it as it is. Then we recur for the remaining digits. The approach remains the same, the basic difference is the loop is replaced by a recursive function.

Algorithm: 

  • Check a base case when the number is 0 return 5, and for all other cases, form a recursive function.
  • The function (solve(int n))can be defined as follows, if the number passed is 0 then return 0, else extract the last digit i.e. n = n/10 and remove the last digit. If the last digit is zero assigns 5 to it.
  • Now return the value by calling the recursive function for n, i.e return solve(n)*10 + digit.
  • Print the answer.

Implementation:


Output
15125

Complexity Analysis: 

  • Time Complexity: O(k), the recursive function is called only k times, where k is the number of digits of the number
  • Auxiliary Space: O(1), no extra space is required.

Approach (Using builtin function replace())


Output
15125

Time Complexity: O(log(num)), where num is the number of digits in num variable.

Auxiliary Space: O(num)

Comment