Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.2 kB
2
Indexable
Never
import java.util.*;

public class Main {
  static int[] calSum(int a[], int b[], int n, int m) {
        
        // array is representing digits of the number and digit can be between 0 to 9 
        // Initially we took the bigger size as the final size
        int size = 0;
        int index = 0;
        if(n>m)
        {
            size = n; 
            index = n-1;
        }
        else 
        {
            size = m;
            index = m-1;
        }
        
        int result[] = new int[size];
        
        // We are given two arrays and we need to add these two arrays 
        
        // i will point to last element of a (First array)
        // j will point to last element of b (Second array)
        
        // right to left 
        // i will decrease by 1 
        // j will decrease by 1 
        
        // while loop we use in Two pointer 
        
        // We are starting from last index 
        int i = n-1;
        int j = m-1;
        
        int carry = 0;
        
        // a = [9, 9, 9, 9], n = 4
        // b = [9, 9], m = 2
        
        // STEP 1, i = 3, j = 1, index = 3, sum = 9 + 9 + 0 = 18, for next step carry = 1 
        // STEP 2, i = 2, j = 0, index = 2, sum = 9 + 9 + 1 = 19, for next step carry = 1 
        // STEP 3, i = 1, j = -1, index = 1, LOOP will stop 
        while(i>=0 && j>=0)
        {
            int sum = a[i] + b[j] + carry;
            if(sum>=10)
            {
                carry = 1;
            }
            else 
            {
                carry = 0;
            }
			
            result[index] = sum%10;
			
            // 0 0 9 8
			
            // both arrows will move towards left 
            i = i-1;
            j = j-1;
            index = index-1;
        }
    
        // STEP 1, i = 1, j = -1, index = 1, sum = 9 + 1 = 10, carry will be 1, result[1] = 0 
        // STEP 2, i = 0, j = -1, index = 0, sum = 9 + 1 = 10, carry will be 1, result[0] = 0 
        // STEP 3, i = -1, j = -1, index = -1, THE LOOP will stop 
        while(i>=0) // first array is remaining but second array is finished 
        {
            int sum = a[i] + carry;
            if(sum>=10)
            {
                carry = 1;
            }
            else 
            {
                carry = 0;
            }
            result[index] = sum%10;
            
            // both arrows will move towards left 
            i = i-1;
            index = index-1;
        }
        
        while(j>=0) // first array is finished but second array is remaining  
        {
            int sum = b[j] + carry;
            if(sum>=10)
            {
                carry = 1;
            }
            else 
            {
                carry = 0;
            }
            result[index] = sum%10;
            
            // both arrows will move towards left 
            j = j-1;
            index = index-1;
        }
        
        // result = [0, 0, 9, 8]
        
        // After addition if carry is 1 (we need to increase the size)
        if(carry == 1)
        {
            int answer[] = new int[size+1];
            answer[0] = carry;
            
            // answer = [1, 0, 0, 9, 8]
            
            // we will copy all values from result array to the answer array 
            
            // STEP 1, i=0, i+1 = 1, answer[1] = result[0]
            // STEP 2, i=1, i+1 = 2, answer[2] = result[1]
            // STEP 3, i=2, i+1 = 3, answer[3] = result[2]
            // STEP 4, i=3, i+1 = 4, answer[4] = result[3]
            
            for(int k=0; k<size; k=k+1)
            {
                answer[k+1] = result[k];
            }
            
            return answer;
        }
        
        return result;
  }

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n1 = sc.nextInt();
    int[] arr1 = new int[n1];
    for (int i = 0; i < n1; i++) arr1[i] = sc.nextInt();

    int n2 = sc.nextInt();
    int[] arr2 = new int[n2];
    for (int i = 0; i < n2; i++) arr2[i] = sc.nextInt();
    sc.close();

    int[] res = calSum(arr1, arr2, n1, n2);
    for (int i : res) System.out.println(i);
  }
}