<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jerin</title>
    <description>The latest articles on DEV Community by Jerin (@jerin_babu).</description>
    <link>https://dev.to/jerin_babu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3914172%2F1f1a253f-2ad3-4a2d-bd94-81f77f4d85de.png</url>
      <title>DEV Community: Jerin</title>
      <link>https://dev.to/jerin_babu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jerin_babu"/>
    <language>en</language>
    <item>
      <title>LeetCode 66: Plus One — Simple Explanation with Carry Logic (Java)</title>
      <dc:creator>Jerin</dc:creator>
      <pubDate>Tue, 05 May 2026 13:54:59 +0000</pubDate>
      <link>https://dev.to/jerin_babu/leetcode-66-plus-one-simple-explanation-with-carry-logic-java-2i59</link>
      <guid>https://dev.to/jerin_babu/leetcode-66-plus-one-simple-explanation-with-carry-logic-java-2i59</guid>
      <description>&lt;p&gt;&lt;strong&gt;Difficulty&lt;/strong&gt;: Easy&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics&lt;/strong&gt;: Array, Math&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Platform&lt;/strong&gt;: Leetcode&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.Increment the large integer by one and return the resulting array of digits.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement Simplified
&lt;/h2&gt;

&lt;p&gt;Check the last digit, add 1, then return&lt;/p&gt;

&lt;h2&gt;
  
  
  Mistakes and Learning
&lt;/h2&gt;

&lt;p&gt;Do not turn the array into number.&lt;br&gt;
Only check/change last digit.&lt;br&gt;
Look out for rare cases (ex:[9,9,9,…]&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 1
&lt;/h2&gt;

&lt;p&gt;Input: digits = [1,2,3]&lt;br&gt;
Output: [1,2,4]&lt;br&gt;
Explanation: The array represents the integer 123.&lt;br&gt;
Incrementing by one gives 123 + 1 = 124.&lt;br&gt;
Thus, the result should be [1,2,4].&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 2
&lt;/h2&gt;

&lt;p&gt;Input: digits = [4,3,2,1]&lt;br&gt;
Output: [4,3,2,2]&lt;br&gt;
Explanation: The array represents the integer 4321.&lt;br&gt;
Incrementing by one gives 4321 + 1 = 4322.&lt;br&gt;
Thus, the result should be [4,3,2,2].&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Insight
&lt;/h2&gt;

&lt;p&gt;Addition starts from the last digit&lt;br&gt;
If digit &amp;lt; 9 → just increment and stop&lt;br&gt;
If digit = 9 → set to 0 and carry forward&lt;/p&gt;

&lt;h2&gt;
  
  
  Algorithm
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Initialize a for loop with i starting from last digit and i decrement at each loop.&lt;/li&gt;
&lt;li&gt;Check if the digit[i] is less than 9&lt;/li&gt;
&lt;li&gt;If less than 9, then increment (digit[i]++).
return digit.&lt;/li&gt;
&lt;li&gt;end if.&lt;/li&gt;
&lt;li&gt;then digit[i]=0.&lt;/li&gt;
&lt;li&gt;end for loop&lt;/li&gt;
&lt;li&gt;Initialize an array with digits.length+1.&lt;/li&gt;
&lt;li&gt;Assign the first digit of new array as 1.&lt;/li&gt;
&lt;li&gt;return the new array.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Algorithm in simple words
&lt;/h2&gt;

&lt;p&gt;First, initialize a for loop that will start from the end of the array and check if that digit is less than 9, if yes then just increment that digit by 1 and return it, SIMPLE ! (ex: suppose the digits array is [4,2,3,2] check the last digit, it is less than 9 , which is 2. So when we increment 2 the final result will be [4,2,3,3], that is the final output).&lt;/p&gt;

&lt;p&gt;Suppose the last digit is 9 then make it 0 and then go to the second last digit and check in the if statment. if it is less than 9 then increment by 1 then return. (ex : suppose the digits array iss [4,3,2,9] check if less than 9, NO. Then digits&lt;a href="https://dev.toin%20here%20that%20is%209"&gt;i&lt;/a&gt; will become 0 the array will be [4,3,2,0], i decrease then check for the second last digit (here it is 2) it is less than 9 so increamnet by 1 then return so the final result will be [4,3,3,0]).&lt;/p&gt;

&lt;p&gt;Suppose the entire is 9 ie the array is [9,9,9] then we will initialize a new array increment the array length by 1, and make the first digit of the array 1.&lt;/p&gt;

&lt;h2&gt;
  
  
  Java code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;plusOne&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;digits&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;digits&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;digits&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;digits&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]++;&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;digits&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;digits&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;newDigits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;digits&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
        &lt;span class="n"&gt;newDigits&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;newDigits&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Time &amp;amp; Space Complexity
&lt;/h2&gt;

&lt;p&gt;Time Complexity: O(n)&lt;/p&gt;

&lt;p&gt;Space Complexity: O(1) (except when new array is created)&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>java</category>
      <category>challenge</category>
    </item>
  </channel>
</rss>
