Back to Practice Dashboard
Python BasicsEasy

Replace all 0’s with 1 in a given integer

Learn how to solve the 'Replace all 0’s with 1 in a given integer' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

Write a function replace_zeros(n) that takes a positive integer n and returns a new integer where every digit '0' in n has been replaced with '1'.

For example, if n = 102030, the result should be 112131 (each 0 becomes 1).

Constraints
  • 1 <= n <= 10^9

Examples

Example 1
Input
replace_zeros(102030)
Output
112131
Explanation

The digits of 102030 are 1,0,2,0,3,0. Replacing each 0 with 1 gives 1,1,2,1,3,1 = 112131.

Example 2
Input
replace_zeros(1000)
Output
1111
Explanation

1,0,0,0 becomes 1,1,1,1 = 1111.

Example 3
Input
replace_zeros(123)
Output
123
Explanation

No zeros in 123, so it remains unchanged.

Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor