Categories: Data Structure

Array Carry Forward

What is Carry Forward?

The word “carry forward” does not refer to a specific idea in data structures and algorithms. However, “carry forward” in the context of arrays often refers to the process of relocating entries in an array to fill in empty spaces left by deleted or removed elements. This is also referred to as “moving” or “sliding” elements.

Let us see a code that uses “carry” in the sense of adding a 1 to the last digit of an array and propagating any carry to the next digit, analogous to binary arithmetic carry operations.


class Test {

    static int[] plusOne(int digits[]) {
    int cary = 1;
    for (int i = digits.length - 1; i >= 0; i--) {
        int sum = digits[i] + cary;
        digits[i] = sum % 10;
        cary = sum / 10;
    }

    if (cary != 0) {
        int[] newArray = new int[digits.length + 1];
        System.arraycopy(digits, 0, newArray, 1, digits.length);
        newArray[0] = cary;
        return newArray;
    }

    return digits;
}
 public static void main(String[] args)
    {
        Test obj = new Test();
        int arr[] = { 1, 4, 2, 10, 2, 3, 1, 0, 20 };
         int[] result = obj.plusOne(arr); // Call plusOne method on the instance
        System.out.println("Result: ");
        for (int i : result) {
            System.out.print(i + " ");
        }
    }
}

Output:

Result: 
1 4 3 0 2 3 1 2 1 

The Java solution presented uses the “carry forward” approach to add one to an array of digits that represents a non-negative integer in reverse order. Here’s a detailed breakdown of how the solution works:

  1. Initialize a variable cary to 1, which represents the carry value initially.
  2. Loop through the digits array from right to left, starting from digits.length-1 to 0, using the variable i as the loop index.
  3. At each iteration, calculate the sum of the current digit digits[i] and the carry cary, and store it in a variable sum.
  4. Update the current digit digits[i] with the value of sum modulo 10, which gives the remainder when divided by 10, using the expression digits[i] = sum % 10. This represents the carry forward operation, where the remainder is the updated digit and the quotient is the carry for the next iteration.
  5. Update the carry cary with the value of sum divided by 10, which gives the quotient when divided by 10, using the expression cary = sum / 10.
  6. Continue this process for all digits in the array, moving from right to left.
  7. After the loop completes, check if there is any remaining carry cary that needs to be added as a new digit at the beginning of the array.
  8. If there is a non-zero carry, create a new array newArray with a length of digits.length + 1 to accommodate the new digit.
  9. Use the System.arraycopy() method to copy the elements of the original digits array to the newArray, starting from index 0 of digits to index 1 of newArray, and copying digits.length elements. This shifts the original digits to the right by one position to make room for the carry at the beginning.
  10. Set the first element of newArray to the value of cary, which represents the new digit.
  11. Return newArray as the updated array if there is a carry, or return the original digits array if there is no carry.

Note: also read about DSA: Concept of Array

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago

Binary Search Tree (BST)

A Binary Search Tree (BST) is a type of binary tree that satisfies the following…

1 year ago