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.
Iterative Approach-1: 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.
Algorithm:
- 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:
C++
#include <iostream>
using namespace std;
int reverseTheNumber( int temp)
{
int ans = 0;
while (temp > 0) {
int rem = temp % 10;
ans = ans * 10 + rem;
temp = temp / 10;
}
return ans;
}
int convert0To5( int num)
{
if (num == 0)
return 5;
else {
int temp = 0;
while (num > 0) {
int digit = num % 10;
if (digit == 0)
digit = 5;
temp = temp * 10 + digit;
num = num / 10;
}
return reverseTheNumber(temp);
}
}
int main()
{
int num = 10120;
cout << convert0To5(num);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int reverseTheNumber( int temp)
{
int ans = 0 ;
while (temp > 0 ) {
int rem = temp % 10 ;
ans = ans * 10 + rem;
temp = temp / 10 ;
}
return ans;
}
static int convert0To5( int num)
{
if (num == 0 )
return 5 ;
else {
int temp = 0 ;
while (num > 0 ) {
int digit = num % 10 ;
if (digit == 0 )
digit = 5 ;
temp = temp * 10 + digit;
num = num / 10 ;
}
return reverseTheNumber(temp);
}
}
public static void main(String args[])
{
int num = 10120 ;
System.out.println(convert0To5(num));
}
}
|
Python3
def reverseTheNumber(temp):
ans = 0 ;
while (temp > 0 ):
rem = temp % 10 ;
ans = ans * 10 + rem;
temp = temp / / 10 ;
return ans;
def convert0To5(num):
if (num = = 0 ):
return 5 ;
else :
temp = 0 ;
while (num > 0 ):
digit = num % 10 ;
if (digit = = 0 ):
digit = 5 ;
temp = temp * 10 + digit;
num = num / / 10 ;
return reverseTheNumber(temp);
if __name__ = = '__main__' :
num = 10120 ;
print (convert0To5(num));
|
C#
using System;
public class GFG {
static int reverseTheNumber( int temp)
{
int ans = 0;
while (temp > 0) {
int rem = temp % 10;
ans = ans * 10 + rem;
temp = temp / 10;
}
return ans;
}
static int convert0To5( int num)
{
if (num == 0)
return 5;
else {
int temp = 0;
while (num > 0) {
int digit = num % 10;
if (digit == 0)
digit = 5;
temp = temp * 10 + digit;
num = num / 10;
}
return reverseTheNumber(temp);
}
}
public static void Main ( string [] args) {
int num = 10120;
Console.Write(convert0To5(num));
}
}
|
Javascript
<script>
function reverseTheNumber(temp) {
var ans = 0;
while (temp > 0) {
var rem = temp % 10;
ans = ans * 10 + rem;
temp = parseInt(temp / 10);
}
return ans;
}
function convert0To5(num)
{
if (num == 0)
return 5;
else {
var temp = 0;
while (num > 0) {
var digit = num % 10;
if (digit == 0)
digit = 5;
temp = temp * 10 + digit;
num = parseInt(num / 10);
}
return reverseTheNumber(temp);
}
}
var num = 10120;
document.write(convert0To5(num));
</script>
|
Complexity Analysis:
- Time Complexity: O(n), where n is number of digits in the number.
- Auxiliary Space: O(1), no extra space is required.
Iterative Approach-2: 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:
C++
#include<bits/stdc++.h>
using namespace std;
int calculateAddedValue( int number)
{
int result = 0;
int decimalPlace = 1;
if (number == 0)
{
result += (5 * decimalPlace);
}
while (number > 0)
{
if (number % 10 == 0)
{
result += (5 * decimalPlace);
}
number /= 10;
decimalPlace *= 10;
}
return result;
}
int replace0with5( int number)
{
return number += calculateAddedValue(number);
}
int main()
{
cout << replace0with5(1020);
}
|
Java
import java.io.*;
public class ReplaceDigits {
static int replace0with5( int number)
{
return number += calculateAddedValue(number);
}
private static int calculateAddedValue( int number)
{
int result = 0 ;
int decimalPlace = 1 ;
if (number == 0 ) {
result += ( 5 * decimalPlace);
}
while (number > 0 ) {
if (number % 10 == 0 )
result += ( 5 * decimalPlace);
number /= 10 ;
decimalPlace *= 10 ;
}
return result;
}
public static void main(String[] args)
{
System.out.print(replace0with5( 1020 ));
}
}
|
Python3
def replace0with5(number):
number + = calculateAddedValue(number)
return number
def calculateAddedValue(number):
result = 0
decimalPlace = 1
if (number = = 0 ):
result + = ( 5 * decimalPlace)
while (number > 0 ):
if (number % 10 = = 0 ):
result + = ( 5 * decimalPlace)
number / / = 10
decimalPlace * = 10
return result
print (replace0with5( 1020 ))
|
C#
using System;
class GFG{
static int replace0with5( int number)
{
return number += calculateAddedValue(number);
}
static int calculateAddedValue( int number)
{
int result = 0;
int decimalPlace = 1;
if (number == 0)
{
result += (5 * decimalPlace);
}
while (number > 0)
{
if (number % 10 == 0)
result += (5 * decimalPlace);
number /= 10;
decimalPlace *= 10;
}
return result;
}
static public void Main()
{
Console.WriteLine(replace0with5(1020));
}
}
|
Javascript
<script>
function calculateAddedValue(number){
let result = 0;
let decimalPlace = 1;
if (number == 0) {
result += (5 * decimalPlace);
}
while (number > 0){
if (number % 10 == 0){
result += (5 * decimalPlace);
}
number = Math.floor(number/10);
decimalPlace *= 10;
}
return result;
}
function replace0with5(number){
return number += calculateAddedValue(number);
}
document.write(replace0with5(1020));
</script>
|
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.
Recursive Approach: 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:
C++
#include <bits/stdc++.h>
using namespace std;
int convert0To5Rec( int num)
{
if (num == 0)
return 0;
int digit = num % 10;
if (digit == 0)
digit = 5;
return convert0To5Rec(num / 10) * 10 + digit;
}
int convert0To5( int num)
{
if (num == 0)
return 5;
else
return convert0To5Rec(num);
}
int main()
{
int num = 10120;
cout << convert0To5(num);
return 0;
}
|
C
#include <stdio.h>
int convert0To5Rec( int num)
{
if (num == 0)
return 0;
int digit = num % 10;
if (digit == 0)
digit = 5;
return convert0To5Rec(num / 10) * 10 + digit;
}
int convert0To5( int num)
{
if (num == 0)
return 5;
else
return convert0To5Rec(num);
}
int main()
{
int num = 10120;
printf ( "%d" , convert0To5(num));
return 0;
}
|
Java
import java.io.*;
public class GFG {
static int convert0To5Rec( int num)
{
if (num == 0 )
return 0 ;
int digit = num % 10 ;
if (digit == 0 )
digit = 5 ;
return convert0To5Rec(num / 10 ) * 10 + digit;
}
static int convert0To5( int num)
{
if (num == 0 )
return 5 ;
else
return convert0To5Rec(num);
}
public static void main(String[] args)
{
System.out.println(convert0To5( 10120 ));
}
}
|
Python3
def convert0to5rec(num):
if num = = 0 :
return 0
digit = num % 10
if digit = = 0 :
digit = 5
return convert0to5rec(num / / 10 ) * 10 + digit
def convert0to5(num):
if num = = 0 :
return 5
else :
return convert0to5rec(num)
num = 10120
print (convert0to5(num))
|
C#
using System;
class GFG {
static int convert0To5Rec( int num)
{
if (num == 0)
return 0;
int digit = num % 10;
if (digit == 0)
digit = 5;
return convert0To5Rec(num / 10) * 10 + digit;
}
static int convert0To5( int num)
{
if (num == 0)
return 5;
else
return convert0To5Rec(num);
}
static public void Main()
{
Console.Write(convert0To5(10120));
}
}
|
PHP
<?php
function convert0to5rec( $num )
{
if ( $num == 0)
return 0;
$digit = ( $num % 10);
if ( $digit == 0)
$digit = 5;
return convert0to5rec((int)( $num / 10)) *
10 + $digit ;
}
function convert0to5( $num )
{
if ( $num == 0)
return 5;
else
return convert0to5rec( $num );
}
$num = 10120;
print (convert0to5( $num ));
?>
|
Javascript
<script>
function convert0To5Rec(num) {
if (num == 0)
return 0;
var digit = num % 10;
if (digit == 0)
digit = 5;
return convert0To5Rec(parseInt(num / 10)) * 10 + digit;
}
function convert0To5(num) {
if (num == 0)
return 5;
else
return convert0To5Rec(num);
}
document.write(convert0To5(10120));
</script>
|
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())
C++
#include <bits/stdc++.h>
using namespace std;
string change( int n)
{
string temp = to_string(n) + "" ;
replace(temp.begin(), temp.end(), '0' , '5' );
return temp;
}
int main()
{
int num = 10120;
cout << (change(num));
return 0;
}
|
Java
import java.io.*;
class GFG {
static String change( int n){
String temp = n + "" ;
return temp.replace( '0' , '5' );
}
public static void main (String[] args) {
int num = 10120 ;
System.out.println(change(num));
}
}
|
Python3
def change(num):
s = str (num)
s = s.replace( '0' , '5' )
return s
if __name__ = = '__main__' :
num = 10120
print (change(num))
|
C#
using System;
public class GFG {
static String change( int n)
{
String temp = n + "" ;
return temp.Replace( '0' , '5' );
}
static public void Main()
{
int num = 10120;
Console.WriteLine(change(num));
}
}
|
Javascript
function change(n)
{
let temp = n.toString();
temp = temp.replaceAll( '0' , '5' );
return temp;
}
let num = 10120;
console.log((change(num)));
|
Time Complexity: O(log(num)), where num is the number of digits in num variable.
Auxiliary Space: O(num)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!