Posts

Showing posts from August, 2021

LeetCode Problem 66 - Plus One - Algorithm and Java Solution

 Hi Friends, Today we are going to solve a leetcode problem number 66. Please feel free to read problem description from below link. https://leetcode.com/problems/plus-one/ In given problem , it states that you have a non-empty and non-negative array storing single digit value at each position and most significant bit is stored at head of list. Now we look at below example , when 1 is added to last digit 3 , it becomes 4 which would be simple to achieve. Input: digits = [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. But problem comes when last digit is 9 where you add 1 , it becomes 2 digit number = 10 which is not allowed in array. So in that you have keep 0 at unit place and use carry 1 to be added in next digit. For Example  Input: digits = [1,2,9] Output: [1,3,0] Explanation: The array represents the integer 130. Now in above example , 1 added to 9 is replaced with 0 and carry got added to 2 became 3. Corner Case: Everything works fine except where