Advertisement
zarkoto223

bigNumbers

Jan 9th, 2024
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function sumArrays(arrsLen, arr1, arr2) {
  2.  
  3.     arrsLen = gets().split(' ').map(Number);
  4.  
  5.     arr1 = gets().split(' ').map(Number);
  6.     arr2 = gets().split(' ').map(Number);
  7.  
  8.  
  9.     let result = [];
  10.     let carry = 0;
  11.  
  12.     let maxLength = {}
  13.     if (arr1.length >= arr2.length) {
  14.         maxLength = arr1.length;
  15.     }
  16.     else { maxLength = arr2.length; }
  17.  
  18.  
  19.  
  20.     for (let i = 0; i < maxLength; i++) {
  21.         let num1 = arr1[i] || 0;
  22.         let num2 = arr2[i] || 0;
  23.  
  24.         let sum = num1 + num2 + carry;
  25.         result.push(sum % 10);
  26.         carry = Math.floor(sum / 10);
  27.     }
  28.  
  29.     while (carry > 0) {
  30.         result.push(carry % 10);
  31.         carry = Math.floor(carry / 10);
  32.     }
  33.  
  34.     console.log(result.join(' '))
  35. }
  36. sumArrays()
  37.  
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement