# Solution For Add To Array Form Of Integer

Problem Statement:

The problem statement asks the user to perform a special kind of addition operation on an array and a given integer. Given an array of non-negative integers and an integer K, the task is to add the integer K to the array in such a way that the addition operation is performed in the same way as we perform the arithmetic addition operation.

Solution:

The problem statement is straight forward. We have an array of integers and we have to add the integer K to it. But the tricky part here is to get the result in the same representation as that of the normal addition operation. To achieve this, we can perform the addition from the right side of the array (least significant digit) and take the carry forward element and add to the next element.

Algorithm:

1. Initialize a carry variable as 0.
2. Traverse the array from the rightmost element to left and repeat step 3 to 5 until all the elements are traversed.
3. Add the K to the last element of the array and the carry from step 4.
4. Divide the result from step 3 by 10 to get the carry for next iteration and update the carry variable with this value.
5. Append the remainder from step 3 to the list of output elements.
6. Reverse the list of output elements as we performed the operation from the rightmost element.

Code:

“`

``````# initialize a variable to store carry
carry = 0

# initialize an empty list to store output
output = []

# iterate the list from right to left
for i in range(len(A) - 1, -1, -1):

# add the K and carry to the last element of the list
temp = A[i] + K % 10 + carry

# get the carry for next iteration
carry = temp // 10

# append the remainder to the list of output elements
output.append(temp % 10)

# remove the last digit from K
K = K // 10

# if there is any carry left, append it to the output list
if carry:
output.append(carry)

# reverse the output list and return it
return output[::-1]
``````

“`

Time Complexity:

The time complexity of the above algorithm will be O(n) where n is the length of the array. Since we are iterating the array only once from right to left, the time complexity will be linear in the size of the input.

Space Complexity:

The space complexity of the above algorithm will also be O(n), as we are creating an output list of length n to store the result.

## Step by Step Implementation For Add To Array Form Of Integer

```class Solution {
public List addToArrayForm(int[] A, int K) {
// create a new list to store the result
List result = new ArrayList();

// edge case: if A is null or empty, return an empty list
if (A == null || A.length == 0) {
return result;
}

// create two pointers, one for A and one for K
int i = A.length - 1;
int j = 0;

// create a carry variable to keep track of the carry over
int carry = 0;

// while i is greater than or equal to 0, keep iterating
while (i >= 0) {
// if j is greater than or equal to the length of K, set digit to 0
int digit = 0;
if (j < K.length()) {
// otherwise, set digit to the jth digit of K
digit = K.charAt(j) - '0';
}

// sum the ith digit of A with the digit and carry
int sum = A[i] + digit + carry;

// set the carry to the sum / 10
carry = sum / 10;

// add the sum % 10 to the list

// decrement i and increment j
i--;
j++;
}

// if carry is not 0, add it to the list
if (carry != 0) {
}

// return the list
return result;
}
}```
```def addToArrayForm(self, A: List[int], K: int) -> List[int]:

# we initialize an empty list to store our result
result = []

# we convert the integer K into a list of digits
K_digits = list(map(int, str(K)))

# we set two pointers, one at the beginning of list A
# and the other at the beginning of list K_digits
i, j = 0, 0

# we set a carry variable to 0
carry = 0

# we iterate over both lists until we reach the end of one of them
while i < len(A) or j < len(K_digits):

# if we reach the end of list A, we set the current digit to 0
if i >= len(A):
a = 0
# otherwise, we set the current digit to the value at index i of list A
else:
a = A[i]

# if we reach the end of list K_digits, we set the current digit to 0
if j >= len(K_digits):
b = 0
# otherwise, we set the current digit to the value at index j of list K_digits
else:
b = K_digits[j]

# we compute the sum of the current digits and the carry
curr_sum = a + b + carry

# we append the last digit of the sum to our result
result.append(curr_sum % 10)

# we update the carry
carry = curr_sum // 10

# we increment both pointers
i += 1
j += 1

# if the carry is not 0, we append it to our result
if carry != 0:
result.append(carry)

# we return the result
return result```
```var addToArrayForm = function(A, K) {
};```
```class Solution {
public:
vector addToArrayForm(vector& A, int K) {
// reverse both arrays so we can start from the back
reverse(A.begin(), A.end());
// create a new vector to store the result
vector result;
// carry will keep track of the carry over
int carry = 0;
// we will loop through both arrays starting from the back
for (int i = 0; i < A.size(); i++) {
// sum will be the current digit + the carry over + the current digit from K
int sum = A[i] + carry + (K % 10);
// we add the sum % 10 to the result vector
result.push_back(sum % 10);
// update the carry over
carry = sum / 10;
// remove the last digit from K
K /= 10;
}
// if K is not empty, we still need to loop through it
while (K > 0) {
// sum will be the carry over + the current digit from K
int sum = carry + (K % 10);
// we add the sum % 10 to the result vector
result.push_back(sum % 10);
// update the carry over
carry = sum / 10;
// remove the last digit from K
K /= 10;
}
// if carry is not 0, we add it to the result vector
if (carry != 0) {
result.push_back(carry);
}
// reverse the result vector
reverse(result.begin(), result.end());
// return the result vector
return result;
}
};```
`public IList AddToArrayForm(int[] A, int K) { // create a list to store the results var result = new List(); // create a variable to keep track of the carry int carry = 0; // iterate through the array from the end to the beginning for (int i = A.Length - 1; i >= 0; i--) { // add the current digit to the carry int sum = A[i] + K % 10 + carry; // update the carry carry = sum / 10; // add the sum to the list result.Add(sum % 10); // update K K /= 10; } // if K is not 0, we need to add it to the list while (K != 0) { // add the current digit to the carry int sum = K % 10 + carry; // update the carry carry = sum / 10; // add the sum to the list result.Add(sum % 10); // update K K /= 10; } // if the carry is not 0, we need to add it to the list if (carry != 0) { result.Add(carry); } // reverse the list to get the correct order result.Reverse(); return result; }`

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]