# Add One To Number

Given a non-negative number represented as an array of digits,

add 1 to the number ( increment the number represented by the digits ).

The digits are stored such that the most significant digit is at the head of the list.

**Example:**

If the vector has `[1, 2, 3]`

the returned vector should be `[1, 2, 4]`

as `123 + 1 = 124`.

> **NOTE:** Certain things are intentionally left unclear in this question which you should practice asking the interviewer.\
> For example, for this problem, following are some good questions to ask :
>
> * **Q :** Can the input have 0’s before the most significant digit. Or in other words, is `0 1 2 3` a valid input?
> * **A :** For the purpose of this question, **YES**
> * **Q :** Can the output have 0’s before the most significant digit? Or in other words, is `0 1 2 4` a valid output?
> * **A :** For the purpose of this question, **NO**. Even if the input has zeroes before the most significant digit.

```java
public class Solution {
    public ArrayList<Integer> plusOne(ArrayList<Integer> A) {
        ArrayList<Integer> t=new ArrayList<>();
        int carry=1;
        for(int j=A.size()-1;j>=0;j--){
            int num=A.get(j)+carry;
            carry=num/10;
            num=num%10;
            t.add(0,num);
        }
        if(carry==1){
            t.add(0,1);
        }
        ArrayList<Integer> ans=new ArrayList<>();
        boolean flag=true;
        for(int i=0;i<t.size();i++){
            if(flag && t.get(i)==0){
                
            }
            else{
                flag=false;
                ans.add(t.get(i));
            }
        }
        return ans;
    }
}

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/strings-arrays-and-2-pointers/add-one-to-number.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
