- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Add One in Python

Suppose we have a list of integers called n, this is representing a decimal number and n[i] is between [0, 9]. So, if the n is [2, 4, 9] represents the number 249. We have to find the same list in the same representation except modified so that 1 is added to the number.

So, if the input is like n = [9,9], then the output will be [1, 0, 0]

To solve this, we will follow these steps −

n := add 0 at the beginning of n

increase last element of n by 1

for i in range size of n - 1 to 0, decrease by 1, do

n[i-1] := n[i-1] + quotient of (n[i] / 10)

n[i] := n[i] mod 10

return n if n[0] > 0 otherwise n from index 1 to end

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, n): n = [0] + n n[-1] += 1 for i in range(len(n) - 1, 0, -1): n[i-1] += n[i] // 10 n[i] = n[i] % 10 return n if n[0] > 0 else n[1:] ob = Solution() print(ob.solve([9,9]))

## Input

[9,9]

## Output

[1, 0, 0]

- Related Questions & Answers
- Add one Python string to another
- Add one polynomial to another in Python
- Add one Chebyshev series to another in Python
- Add one Hermite series to another in Python
- Add one Laguerre series to another in Python
- Add one Legendre series to another in Python
- Add one Hermite_e series to another in Python
- Add One Row to Tree in C++
- Plus One in Python
- set add() in python
- How to programmatically add buttons into a layout one by one in several lines in Android
- Program to add one to a number that is shown as a digit list in Python
- How do I programmatically add buttons into layout one by one in several lines using Kotlin?
- Add Two Numbers in Python
- How to add multiple LineCharts into one Scene/Stage in JavaFX?

Advertisements