HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Add one to a number represented as an array of digits

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
representednumberarraydigitsoneadd

Problem

I submitted this problem for an interview, and I was wondering if I could have done better. My solution works, and I solved in about 12-15 minutes. I feel like I did a bad job with the code. Please be honest.

Here is the question:


Given a non-negative number represented as an array of digits, plus
one to the number. Also, the numbers are stored such that the most
significant digit is at the head of the list.

In particular, what stumped me was the fact that I'd get the right answer, but have the initial index (at 0) in sometimes empty with the answer on the other half, and couldn't figure out a quick half to shift all the elements to the left without using extra storage.

public int[] plusOne(int[] digits) {
    int[] toRet=new int[digits.length+1]; 
    int[] temp=null;

    for(int i=toRet.length-1; i>=0; i--){
        if(i-1>=0){
            toRet[i]=digits[i-1];
        }
    }
    for(int i=toRet.length-1; i>=0; i--){
        if(toRet[i]+1 == 10){
            toRet[i]=0;
        }
        else{
            toRet[i] = toRet[i]+1;
            break;
        }
    }
    if(toRet[0]==0){
        temp = new int[toRet.length-1];
        for(int i=1; i<toRet.length;i++){
            temp[i-1] = toRet[i];
        }
       return temp;
    }
    else{
        return toRet;
    }

}

Solution

What you did well

I like that you have:

  • decently named varaibles



  • you use good backward looping through the data



  • the formatting and style is good.



Issues

  • you are doing a lot of manual copying... Arrays.copyOf() is your friend.



  • you should be declaring variables where you need them, not at the beginning of the method (temp).



  • the algorithm is not a natural fit for the problem.... too complicated, and that is why there is the odd offset in your results.



Alternative...

Consider this code alternative, which uses a standard adder-with-carry:

public static final int[] addOne(int[] digits) {
    int carry = 1;
    int[] result = new int[digits.length];
    for (int i = digits.length - 1; i >= 0; i--) {
        int val = digits[i] + carry;
        result[i] = val % 10;
        carry = val / 10;
    }
    if (carry == 1) {
        result = new int[digits.length + 1];
        result[0] = 1;
    }
    return result;
}


The carry is initialized with the value-to-add 1, and it is added to the least significant value.

If the overall addition results in a carry still, then we add a new digit to the result, and, because the sub being added is a 1, we can make assumptions about the result.
Edit:

My test code:

public static void main(String[] args) {
    System.out.println(Arrays.toString(addOne(new int[]{})));
    System.out.println(Arrays.toString(addOne(new int[]{1})));
    System.out.println(Arrays.toString(addOne(new int[]{9})));
    System.out.println(Arrays.toString(addOne(new int[]{3, 9, 9})));
    System.out.println(Arrays.toString(addOne(new int[]{3, 9, 9, 9})));
    System.out.println(Arrays.toString(addOne(new int[]{9, 9, 9, 9})));
    System.out.println(Arrays.toString(addOne(new int[]{9, 9, 9, 8})));
}


and my results:

[1]
[2]
[1, 0]
[4, 0, 0]
[4, 0, 0, 0]
[1, 0, 0, 0, 0]
[9, 9, 9, 9]

Code Snippets

public static final int[] addOne(int[] digits) {
    int carry = 1;
    int[] result = new int[digits.length];
    for (int i = digits.length - 1; i >= 0; i--) {
        int val = digits[i] + carry;
        result[i] = val % 10;
        carry = val / 10;
    }
    if (carry == 1) {
        result = new int[digits.length + 1];
        result[0] = 1;
    }
    return result;
}
public static void main(String[] args) {
    System.out.println(Arrays.toString(addOne(new int[]{})));
    System.out.println(Arrays.toString(addOne(new int[]{1})));
    System.out.println(Arrays.toString(addOne(new int[]{9})));
    System.out.println(Arrays.toString(addOne(new int[]{3, 9, 9})));
    System.out.println(Arrays.toString(addOne(new int[]{3, 9, 9, 9})));
    System.out.println(Arrays.toString(addOne(new int[]{9, 9, 9, 9})));
    System.out.println(Arrays.toString(addOne(new int[]{9, 9, 9, 8})));
}
[1]
[2]
[1, 0]
[4, 0, 0]
[4, 0, 0, 0]
[1, 0, 0, 0, 0]
[9, 9, 9, 9]

Context

StackExchange Code Review Q#43343, answer score: 7

Revisions (0)

No revisions yet.