Javatpoint Logo
Javatpoint Logo

Plus One to Array Problem in Java

It is a very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, Adobe, Apple, Infosys, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to add (plus) one to an array problem in Java with different approaches and logic. Also, we will create Java programs for the same.

Plus One to Array Problem in Java

Problem Statement

In this problem, we have given an array in which each element represents a digit and the complete array represents a number. The digits are ordered from MSB to LSB in left to right order. Assume that there is no leading 0's in the number.

The task is to add 1 to the number and return the result in the form of an array of digits.

Let's understand it through examples.

Example

Input: arr = [2, 5, 9, 7]

Output: [2, 5, 9, 8]

It represents the number 2597. Add 1 to the most significant bit, we get 2598. The output will be [2, 5, 9, 8].

Let's see another example.

Input: arr = [9, 9]

Output: [1, 0, 0]

It represents the number 99. Add 1 to the most significant bit, we get 100. The output will be [1, 0, 0].

Solution to The Problem

Naive Approach

The simple approach is that first convert the array into a number, add one to it, and return the result in the form of an array. But the approach is not applicable to the large array. So, we will process each digit one by one.

  1. Begin from the least significant bit and process each digit till the most significant bit.
  2. If the current digit is smaller than 9, add 1 to the current digit and return the array else assign zero to the current digit.
  3. If the last element is processed and it was also equal to 9, it means that all the digits were 9. So, we will increase the size of the array by one and assign 1 to the MSB.
  4. Return the resultant array.

Plus One to Array Java Program

PlusOneArray.java

Output:

[6, 3, 8, 3]

Using Vector

Another approach is using the vector. In this approach, we begin from the end of the vector. If the last element is 9, set it to 0, else exit from the loop.

If all digits are 9, set 1 at starting, else increment the element at the position where the loop stopped. Note that no carry, division, and modulo is required.

Let's implement the above approach in a Java program.

PlusOneExample1.java

Output:

12 45 90 35

Let's see another approach.

In this approach, first, we reverse the given array. After that take a carry variable to store the carry. Iterate over the array from starting and add 1. Carry to the value of that array at that index.

Now check whether the value of that index is greater than equal to 9 or not. If yes, take the carry as the ten's value and the value at that index in the array will be the value present at the one place, else simply move on.

Repeat the above step until we reach the last element of the array. Note that if carry is not 0, simply add 1 to the array. At last, reverse the array, so that we can get the original array.

Let's implement the above approach in a Java program.

PlusOneExample2.java

Output:

1 0 0 0 0

Complexity

The time complexity of all the above approaches is O(n) because we have traversed over the array only once. Where n is the length of the array.

The space complexity of the approach is O(1) if the array contains at least one digit smaller than 9, else O(n).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA