1

I was solving a problem in leet code, Solution I came up with made all test cases passed except for one. Input of that test case is an array = [6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3]. so I have convert the array into a number add 1 to entire number and then convert it back to array format with result, in my solution one step before final statement, I have used parseInt("6145390195186705543")+1 then I convert to string and split and then convert to number. but during parseInt(), this in-built method is not able to convert after 15th digits, im getting output as [6145390195186705000], sending 0's after 15 digits, so does anyone has any idea on how to convert a string of number length more than 16 to Number P.S: I have tried bigInt() method, techincally it would work but for the problem bigInt() is not working and output isnt working

var plusOne = function(digits) {
  let y = digits.map(String);
  let z = ''
  for (let i = 0; i < y.length; i++) {
    z += y[i];
  }
  let a = (parseInt(z) + 1).toString().split('')
  return a.map(Number)
};
VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • The JavaScript Number type does not have the precision to accurately represent such large numbers. – phuzi Jul 08 '22 at 09:58
  • `6145390195186705543 > Number.MAX_SAFE_INTEGER` is `true`. You cannot use `parseInt` for it. – VLAZ Jul 08 '22 at 09:58
  • @VLAZ I have added the code for the problem, any thing I can do now? if not parseInt is there any other approach that I can use? Thankyou for reply – Sai Anvesh Reddy Jul 08 '22 at 10:13
  • 2
    Maybe that is what the problem aims for. You should not be able to do that with Int. Another solution I can think of is that you loop the array in reverse order and then calculate each element of the array individually like one would do it by hand. If it has remainder then you put it into the next number and so on. This way you will not have the limitation at only15 digits. – holydragon Jul 08 '22 at 10:13
  • yes @phuzi as -VLAZ said parseInt() will not work on number greater than '9007199254740998' – Sai Anvesh Reddy Jul 08 '22 at 10:15
  • @holydragon yes the solution you suggested it worked when I thought of alternate solution, as per this scenario I had no prior knowledge on Number,MAX_SAFE_INTEGER. what you said could be correct, but if I occur with a situation like this other than bigInt(), I wanted to know if anything is possible – Sai Anvesh Reddy Jul 08 '22 at 10:17
  • The number is purposefully that big. So the challenge is to find an algorithm which works on the digits in the array. – lukas.j Jul 08 '22 at 10:19
  • @SaiAnveshReddy either implement a proper arithmetic with each digit in the array or maybe use BigInt. – VLAZ Jul 08 '22 at 10:23

1 Answers1

0

Loop backwards over the digits, and

  • if the digit is less than 9, add 1 and stop
  • otherwise the digit is 9, so set it to 0 and if the 9 was the first digit in the array, insert a 1 at the beginning of the array

function addOne(digitArray) {
  for (let i = digitArray.length - 1; i >= 0; i--) {
    if (digitArray[i] < 9) {
      digitArray[i] += 1
      break
    } else {
      digitArray[i] = 0
      if (i === 0) {
        digitArray.unshift(1)
      }
    }
  }
  return digitArray
}

console.log(addOne([]))         // []
console.log(addOne([ 0 ]))      // [ 1 ]
console.log(addOne([ 9 ]))      // [ 1, 0 ]
console.log(addOne([ 1, 0 ]))   // [ 1, 1 ]
console.log(addOne([ 1, 9 ]))   // [ 2, 0 ]
console.log(addOne([ 9, 9 ]))   // [ 1, 0, 0 ]

console.log(addOne([ 6, 1, 4, 5, 3, 9, 0, 1, 9, 5, 1, 8, 6, 7, 0, 5, 5, 4, 3 ]))
// [ 6, 1, 4, 5, 3, 9, 0, 1, 9, 5, 1, 8, 6, 7, 0, 5, 5, 4, 3 ]
lukas.j
  • 6,453
  • 2
  • 5
  • 24
  • [This SO answer](https://stackoverflow.com/questions/41952655/how-to-add-two-big-numbers-in-javascript#answer-64739701) covers sum of any long numbers. – skobaljic Jul 08 '22 at 10:47
  • @skobaljic the challenge – as far as I understand it – is not about that. It is from _array of digits_ to _array of digits plus 1_. So there is no need to try to turn the input into a big number at all. – lukas.j Jul 08 '22 at 10:58
  • this solution works for the problem, in other case, for example: I have a string "length>MAX_SAFE_INTEGER" , is there any way to add +1 or +any number to that string and get the output? – Sai Anvesh Reddy Jul 08 '22 at 11:01
  • @SaiAnveshReddy You can convert a string number into an array of digits with _const digitArray = Array.from('6145390195186705543', Number)_ – lukas.j Jul 08 '22 at 11:07