Replace all the 0’s with 1 in a given integer

Problem Statement: You are given an integer. Your task is to replace all the zeros in the integer with ones.

Examples:

Example 1:
Input: N = 102003
Output: 112113
Explanation: The 2nd,4th and 5th position from left contain 0.The resultant integer has replaced the 0’s in those  positions with 1.

Example 2:
Input:  204
Output: 214
Explanation: The 2nd position from left contain 0. The resultant integer has replaced the 0 in that position with 1.

Solution

Disclaimer: Don’t jump directly to the solution, try it out yourself first.

Solution :

Approach:  

Maintain a variable and, store the modified integer. Initialize it to 0. Another variable tmp is used which is initialized to 1.

Procedure:-

  1. Isolate the last digit, say d from the number using % operator. If d is 0 make d’s value 1 else the digit remains as it is. 
  2. Use the formula ans = tmp*d+ ans  to form the number. 
  3. Divide the original integer by 10 to discard the last digit.
  4. Multiply tmp with 10.
  5. Repeat step 1-4  till input integer > 0. 

Dry run:

Code:

C++ Code

#include <iostream>
using namespace std;

int replaceZerosWithOnes(int num) {
   if (num == 0) {
      return 1;
   }
   int ans = 0, tmp = 1;
   while (num > 0) {
      int d = num % 10;
      if (d == 0) {
         d = 1;
      }
      ans = d * tmp + ans;
      num = num / 10;
      tmp = tmp * 10;
   }
   return ans;
}
int main() {
   int n = 204;
   int result = replaceZerosWithOnes(n);
   cout << "After replacing zeros with ones " << n << " becomes " << result;
}

Output:

After replacing zeros with ones 204 becomes 214

Time Complexity: O(N) where N is the number of digits in input integer

Space Complexity: O(1)

Java Code

public class Main {
   static int replaceZerosWithOnes(int num) {
      if (num == 0) {
         return 1;
      }
      int ans = 0, tmp = 1;
      while (num > 0) {
         int d = num % 10;
         if (d == 0) {
            d = 1;
         }
         ans = d * tmp + ans;
         num = num / 10;
         tmp = tmp * 10;
      }
      return ans;
   }

   public static void main(String[] args) {

      int n = 204;
      int result = replaceZerosWithOnes(n);
      System.out.println("After replacing zeros with ones " + n + " becomes " + result);
   }
}  

Output:

After replacing zeros with ones 204 becomes 214

Time Complexity: O(N) where N is the number of digits in input integer

Space Complexity: O(1)

Special thanks to Somparna Chakrabarti for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article