1

I am designing a problem in which I have to use an int array to add or subtract values. For example instead of changing 100 to 101 by adding 1, I want to do the same thing using the int array. It work like this:

int[] val = new int[3];

val[0] = 1;
val[1] = 0;
val[2] = 0;

val[2] += 1;

so, If I have to get a value of 101, I will add 1 to val[2].

The only problem I have is finding a way to make int array work like how adding and subtracting from an ordinary integer data set works.

Is this possible using a for loop or a while loop? Any help will be appreciated!

2
  • Could you add a more complete example for clarifying the problem? Commented Sep 15, 2015 at 23:15
  • It is a bit tricky to explain what I want. What I trying to achieve is having an array of integer storing each single digit from the integer. For example: 100 will be stored in a array called val like: val[0] = 1, val[1] = 0 and val[2] = 0. Then, When I add something to the hundred, I wanna do it using the array that represents the integer. I hope it explains it! Commented Sep 15, 2015 at 23:27

5 Answers 5

3

Here's your homework:

public static int[] increment(int[] val) {
    for (int i = val.length - 1; i >= 0; i--) {
        if (++val[i] < 10)
            return val;
        val[i] = 0;
    }
    val = new int[val.length + 1];
    val[0] = 1;
    return val;
}

Make sure you understand how and why it works before submitting it as your own work.

Sign up to request clarification or add additional context in comments.

5 Comments

Could you please explain me the purpose of the three lines of code at the end of the for loop? This doesn't even get executed.
Actually never mind. I got it now. So, you don't have to explain the purpose of the lines of code.
Anyway, since the question arised: The last 3 lines handle the overflow. When incrementing an array with length n filled with all 9s, a new array with length n+1 is created to fit the result. Incrementing {9, 9} results in {1, 0, 0}.
@adityajain019 how so? Hint: The “last 3 lines” are executed if, and only if, the input is all 9’s in which case it return 1 followed by zeros where there were 9’s
My bad. removing my comment.
1

Solution of this problem is designed by using String
You can refer to this method which will return sum of 2 nos having input in String format.

Input String should contain only digits.

class Demo {
public static String add(String a1, String b1) {
    int[] a = String_to_int_Array(a1);
    int[] b = String_to_int_Array(b1);
    int l = a.length - 1;
    int m = b.length - 1;
    int sum = 0;
    int carry = 0;
    int rem = 0;
    String temp = "";
    if (a.length > b.length) {
        while (m >= 0) {
            sum = a[l] + b[m] + carry;
            carry = sum / 10;
            rem = sum % 10;
            temp = rem + temp;
            m--;
            l--;
        }
        while (l >= 0) {
            sum = a[l] + carry;
            carry = sum / 10;
            rem = sum % 10;
            temp = rem + temp;
            l--;
        }
        if (carry > 0) {
            temp = carry + temp;
        }
    } else {
        while (l >= 0) {
            sum = a[l] + b[m] + carry;
            carry = sum / 10;
            rem = sum % 10;
            temp = rem + temp;
            m--;
            l--;
        }
        while (m >= 0) {
            sum = b[m] + carry;
            carry = sum / 10;
            rem = sum % 10;
            temp = rem + temp;
            m--;
        }
        if (carry > 0) {
            temp = carry + temp;
        }
    }
    return temp;

}

public static int[] String_to_int_Array(String s) {
    int arr[] = new int[s.length()], i;
    for (i = 0; i < s.length(); i++)
    arr[i] = Character.digit(s.charAt(i), 10);
    return arr;
}

public static void main(String a[]) {
    System.out.println(add("222", "111"));
}
}

Comments

1

Quick & dirty:

static void increment(int[] array){
    int i = array.length-1;
    do{
        array[i]=(array[i]+1)%10;
    }while(array[i--]==0 && i>=0);
}

Note the overflow when incementing e.g. {9, 9}. Result is {0, 0} here.

Comments

0

enter image description here

public static void increment() {    
    int[] acc = {9,9,9,9};
    String s="";
    for (int i = 0; i < acc.length; i++)
        s += (acc[i] + "");
    int i = Integer.parseInt(s);
    i++;
    System.out.println("\n"+i);
    String temp = Integer.toString(i);
    int[] newGuess = new int[temp.length()];
    for (i = 0; i < temp.length(); i++)
    {
        newGuess[i] = temp.charAt(i) - '0';
    }

    printNumbers(newGuess);
}

public static void printNumbers(int[] input) {      
    for (int i = 0; i < input.length; i++) {
        System.out.print(input[i] + ", ");
    }
    System.out.println("\n");
}

Comments

0

If someone is looking for this solution using JavaScript or if you can translate it to java, here's your optimum solution:

function incrementArr(arr) {
  let toBeIncrementedFlag = 1,   // carry over logic
    i = arr.length - 1;
  while (toBeIncrementedFlag) {
    if (arr[i] === 9) {
      arr[i] = 0;    // setting the digit as 0 and using carry over
      toBeIncrementedFlag = 1;
    } else {
      toBeIncrementedFlag = 0;
      arr[i] += 1;
      break;    // Breaking loop once no carry over is left
    }
    if (i === 0) {    // handling case of [9,9] [9,9,9] and so on
      arr.unshift(1);
      break;
    }
    i--;    // going left to right because of carry over
  }

  return arr;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.