Given an integer n. Convert all zeroes of n to 5.
Examples:
Input: n = 1004
Output: 1554
Explanation: There are two zeroes in 1004. On replacing all zeroes with 5, the new number will be 1554.Input: n = 121
Output: 121
Explanation: Since there are no zeroes in 121, the number remains as 121.
Table of Content
[Iterative Approach] Digit Extraction with Place Value - O(log n) Time and O(1) Space
Digits of n can be extracted one at a time from the rightmost end using modulo and division by 10. Each extracted 0 is replaced with 5, and then placed into the result at its correct position by multiplying it with the current place value (starting at 1 and growing by a factor of 10 with each step).
Illustration:
- Take n = 1004.
- placeValue = 1, result = 0. Extract 1004 % 10 = 4, keep as 4. result = 0 + 4×1 = 4. placeValue becomes 10. n becomes 100.
- Extract 100 % 10 = 0, replace with 5. result = 4 + 5×10 = 54. placeValue becomes 100. n becomes 10.
- Extract 10 % 10 = 0, replace with 5. result = 54 + 5×100 = 554. placeValue becomes 1000. n becomes 1.
- Extract 1 % 10 = 1, keep as 1. result = 554 + 1×1000 = 1554. placeValue becomes 10000. n becomes 0, loop ends.
- Final result is 1554.
#include <bits/stdc++.h>
using namespace std;
int convert0To5Iter(int num) {
int result = 0;
int placeValue = 1;
while (num > 0) {
int digit = num % 10;
// replace the digit with 5 if it is 0, otherwise keep it unchanged
if (digit == 0)
digit = 5;
// place the digit at its correct position using the growing place value
result = result + digit * placeValue;
placeValue *= 10;
num /= 10;
}
return result;
}
int convertFive(int n) {
// n = 0 is itself a single zero digit, handled directly
if (n == 0)
return 5;
else
return convert0To5Iter(n);
}
int main() {
int n = 1004;
cout << convertFive(n) << endl;
return 0;
}
class GfG {
static int convert0To5Iter(int num) {
int result = 0;
int placeValue = 1;
while (num > 0) {
int digit = num % 10;
// replace the digit with 5 if it is 0, otherwise keep it unchanged
if (digit == 0)
digit = 5;
// place the digit at its correct position using the growing place value
result = result + digit * placeValue;
placeValue *= 10;
num /= 10;
}
return result;
}
static int convertFive(int n) {
// n = 0 is itself a single zero digit, handled directly
if (n == 0)
return 5;
else
return convert0To5Iter(n);
}
public static void main(String[] args) {
int n = 1004;
System.out.println(convertFive(n));
}
}
def convert0to5Iter(num):
result = 0
placeValue = 1
while num > 0:
digit = num % 10
# replace the digit with 5 if it is 0, otherwise keep it unchanged
if digit == 0:
digit = 5
# place the digit at its correct position using the growing place value
result = result + digit * placeValue
placeValue *= 10
num //= 10
return result
def convertFive(n):
# n = 0 is itself a single zero digit, handled directly
if n == 0:
return 5
else:
return convert0to5Iter(n)
n = 1004
print(convertFive(n))
using System;
class GfG {
static int convert0To5Iter(int num) {
int result = 0;
int placeValue = 1;
while (num > 0) {
int digit = num % 10;
// replace the digit with 5 if it is 0, otherwise keep it unchanged
if (digit == 0)
digit = 5;
// place the digit at its correct position using the growing place value
result = result + digit * placeValue;
placeValue *= 10;
num /= 10;
}
return result;
}
static int convertFive(int n) {
// n = 0 is itself a single zero digit, handled directly
if (n == 0)
return 5;
else
return convert0To5Iter(n);
}
static void Main() {
int n = 1004;
Console.WriteLine(convertFive(n));
}
}
function convert0To5Iter(num) {
let result = 0;
let placeValue = 1;
while (num > 0) {
let digit = num % 10;
// replace the digit with 5 if it is 0, otherwise keep it unchanged
if (digit === 0)
digit = 5;
// place the digit at its correct position using the growing place value
result = result + digit * placeValue;
placeValue *= 10;
num = Math.floor(num / 10);
}
return result;
}
function convertFive(n) {
// n = 0 is itself a single zero digit, handled directly
if (n === 0)
return 5;
else
return convert0To5Iter(n);
}
// Driver Code
const n = 1004;
console.log(convertFive(n));
Output
1554
[Recursive Approach] Digit Extraction with Recursive Rebuild - O(log n) Time and O(log n) Space
Instead of tracking a place value explicitly, the digits can be rebuilt using recursion: the function recurses down to the last digit first (the base case, when the number becomes 0), and then as each recursive call returns, it multiplies the already-built result from deeper calls by 10 and appends its own digit.
Illustration:
- Take n = 1004.
- convert0To5Rec(1004) extracts the last digit (4, kept as is) and recurses on 100.
- convert0To5Rec(100) extracts the last digit (0, replaced with 5) and recurses on 10.
- convert0To5Rec(10) extracts the last digit (0, replaced with 5) and recurses on 1.
- convert0To5Rec(1) extracts the last digit (1, kept as is) and recurses on 0.
- convert0To5Rec(0) hits the base case and returns 0.
- Unwinding back up: convert0To5Rec(1) returns 0 × 10 + 1 = 1. convert0To5Rec(10) returns 1 × 10 + 5 = 15. convert0To5Rec(100) returns 15 × 10 + 5 = 155. convert0To5Rec(1004) returns 155 × 10 + 4 = 1554.
#include <bits/stdc++.h>
using namespace std;
int convert0To5Rec(int num) {
// base case for recursion termination
if (num == 0)
return 0;
// extract the last digit and change it if needed
int digit = num % 10;
if (digit == 0)
digit = 5;
// convert remaining digits and append the last digit
return convert0To5Rec(num / 10) * 10 + digit;
}
int convertFive(int n) {
// n = 0 is itself a single zero digit, handled directly
if (n == 0)
return 5;
else
return convert0To5Rec(n);
}
int main() {
int n = 1004;
cout << convertFive(n) << endl;
return 0;
}
class GfG {
static int convert0To5Rec(int num) {
// base case for recursion termination
if (num == 0)
return 0;
// extract the last digit and change it if needed
int digit = num % 10;
if (digit == 0)
digit = 5;
// convert remaining digits and append the last digit
return convert0To5Rec(num / 10) * 10 + digit;
}
static int convertFive(int n) {
// n = 0 is itself a single zero digit, handled directly
if (n == 0)
return 5;
else
return convert0To5Rec(n);
}
public static void main(String[] args) {
int n = 1004;
System.out.println(convertFive(n));
}
}
def convert0to5Rec(num):
# base case for recursion termination
if num == 0:
return 0
# extract the last digit and change it if needed
digit = num % 10
if digit == 0:
digit = 5
# convert remaining digits and append the last digit
return convert0to5Rec(num // 10) * 10 + digit
def convertFive(n):
# n = 0 is itself a single zero digit, handled directly
if n == 0:
return 5
else:
return convert0to5Rec(n)
n = 1004
print(convertFive(n))
using System;
class GfG {
static int convert0To5Rec(int num) {
// base case for recursion termination
if (num == 0)
return 0;
// extract the last digit and change it if needed
int digit = num % 10;
if (digit == 0)
digit = 5;
// convert remaining digits and append the last digit
return convert0To5Rec(num / 10) * 10 + digit;
}
static int convertFive(int n) {
// n = 0 is itself a single zero digit, handled directly
if (n == 0)
return 5;
else
return convert0To5Rec(n);
}
static void Main() {
int n = 1004;
Console.WriteLine(convertFive(n));
}
}
function convert0To5Rec(num) {
// base case for recursion termination
if (num === 0)
return 0;
// extract the last digit and change it if needed
let digit = num % 10;
if (digit === 0)
digit = 5;
// convert remaining digits and append the last digit
return convert0To5Rec(Math.floor(num / 10)) * 10 + digit;
}
function convertFive(n) {
// n = 0 is itself a single zero digit, handled directly
if (n === 0)
return 5;
else
return convert0To5Rec(n);
}
// Driver Code
const n = 1004;
console.log(convertFive(n));
Output
1554
[Alternate Approach] Using Built In Methods
#include <bits/stdc++.h>
using namespace std;
int convertFive(int n)
{
string temp = to_string(n) + "";
replace(temp.begin(), temp.end(), '0', '5');
return stoi(temp);
}
int main()
{
int n = 10120;
cout << (convertFive(n));
return 0;
}
import java.util.stream.Collectors;
public class Main {
public static int convertFive(int n) {
String temp = Integer.toString(n);
temp = temp.replace('0', '5');
return Integer.parseInt(temp);
}
public static void main(String[] args) {
int num = 10120;
System.out.println(convertFive(num));
}
}
def convertFive(n):
temp = str(n)
temp = temp.replace('0', '5')
return int(temp)
if __name__ == '__main__':
num = 10120
print(convertFive(num))
using System;
public class Program
{
public static int convertFive(int n)
{
string temp = n.ToString();
temp = temp.Replace('0', '5');
return int.Parse(temp);
}
public static void Main()
{
int num = 10120;
Console.WriteLine(convertFive(num));
}
}
function convertFive(n) {
let temp = n.toString();
temp = temp.replace(/0/g, '5');
return parseInt(temp);
}
// Main function
let num = 10120;
console.log(convertFive(num));