<?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 747 in Java: Largest Number At Least Twice of Others Explained</title>
      <dc:creator>Jerin</dc:creator>
      <pubDate>Sun, 24 May 2026 15:02:08 +0000</pubDate>
      <link>https://dev.to/jerin_babu/leetcode-747-in-java-largest-number-at-least-twice-of-others-explained-4nhl</link>
      <guid>https://dev.to/jerin_babu/leetcode-747-in-java-largest-number-at-least-twice-of-others-explained-4nhl</guid>
      <description>&lt;p&gt;&lt;strong&gt;Difficulty&lt;/strong&gt;: Easy&lt;br&gt;
&lt;strong&gt;Topics&lt;/strong&gt;: Array, Sorting&lt;br&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 an integer array nums where the largest integer is unique.&lt;br&gt;
Determine whether the largest element in the array is at least twice as much as every other number in the array. If it is, return the index of the largest element, or return -1 otherwise.&lt;br&gt;
Problem Statement Simplified&lt;br&gt;
Check if the largest didgit is twice the other&lt;/p&gt;

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

&lt;p&gt;Started working on it with the assumption to check if the largest number is twice - but the problem is atleast twice. &lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [3,6,1,0]
Output: 1
Explanation: 6 is the largest integer.
For every other number in the array x, 6 is at least twice as big as x.
The index of value 6 is 1, so we return 1.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [1,2,3,4]
Output: -1
Explanation: 4 is less than twice the value of 3, so we return -1.
Key Insight
Check the largest number
Check if it is atleast twice the other
If yes then return the index 
If no then return -1

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ol&gt;
&lt;li&gt;Copy nums array to another array.&lt;/li&gt;
&lt;li&gt;Sort the new array.&lt;/li&gt;
&lt;li&gt;Check if the largest is atleast twice the second largest&lt;/li&gt;
&lt;li&gt;5If yes then loop through the nums array&lt;/li&gt;
&lt;li&gt;Check if nums[i]==largest in new array.&lt;/li&gt;
&lt;li&gt;Return i;&lt;/li&gt;
&lt;li&gt;End if &lt;/li&gt;
&lt;li&gt;End loop&lt;/li&gt;
&lt;li&gt;End if&lt;/li&gt;
&lt;li&gt;Else return -1.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;Copy the nums array to another array to preserve the original index.&lt;br&gt;
Then sort the new array, and check of the largest of new array is atleast twice the second largest array, if yes then loop through the nums array and find the original index of that largest array.&lt;br&gt;
If the largest is not atleast twice the second largest then return -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="nf"&gt;dominantIndex&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;nums&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;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;copyOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&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="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&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;index&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;secondLargest&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;arr&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;2&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;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;arr&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;]&amp;gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;secondLargest&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="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;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;nums&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="n"&gt;i&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;nums&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="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;arr&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;index&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="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;index&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="o"&gt;}&lt;/span&gt;       
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;index&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;&lt;strong&gt;Time Complexity:&lt;/strong&gt; O(nlogn)&lt;br&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; O(n)&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>java</category>
      <category>programming</category>
      <category>sorting</category>
    </item>
    <item>
      <title>LeetCode 228: Summary Ranges Solution in Java (Time: O(n))</title>
      <dc:creator>Jerin</dc:creator>
      <pubDate>Wed, 13 May 2026 14:11:33 +0000</pubDate>
      <link>https://dev.to/jerin_babu/leetcode-228-summary-ranges-solution-in-java-time-on-2hga</link>
      <guid>https://dev.to/jerin_babu/leetcode-228-summary-ranges-solution-in-java-time-on-2hga</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&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 sorted unique integer array nums.&lt;/p&gt;

&lt;p&gt;A range [a,b] is the set of all integers from a to b (inclusive).&lt;/p&gt;

&lt;p&gt;Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.&lt;br&gt;
Each range [a,b] in the list should be output as:&lt;/p&gt;

&lt;p&gt;"a-&amp;gt;b" if a != b&lt;br&gt;
"a" if a == b&lt;/p&gt;

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

&lt;p&gt;Get back the range of continuous numbers in the array.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;Forgot to check for the leftover at the end of the for loop — always need to check the end-of-data scenario.&lt;/li&gt;
&lt;li&gt;Forgot to check if nums. length is 0 or not—always need to check if the nums array has any element in it.&lt;/li&gt;
&lt;/ol&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [0,1,2,4,5,7]
Output: ["0-&amp;gt;2","4-&amp;gt;5","7"]
Explanation: The ranges are:
[0,2] --&amp;gt; "0-&amp;gt;2"
[4,5] --&amp;gt; "4-&amp;gt;5"
[7,7] --&amp;gt; "7"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [0,2,3,4,6,8,9]
Output: ["0","2-&amp;gt;4","6","8-&amp;gt;9"]
Explanation: The ranges are:
[0,0] --&amp;gt; "0"
[2,4] --&amp;gt; "2-&amp;gt;4"
[6,6] --&amp;gt; "6"
[8,9] --&amp;gt; "8-&amp;gt;9"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ol&gt;
&lt;li&gt;If the array is not empty, loop through it.&lt;/li&gt;
&lt;li&gt;While i+1 is less than nums.length and nums[i+1] equals nums[i+1]+1, then i++.&lt;/li&gt;
&lt;li&gt;Else, add to the list.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;ol&gt;
&lt;li&gt;Initialize a list to store the range.&lt;/li&gt;
&lt;li&gt;Check if the nums is empty if yes then return list.&lt;/li&gt;
&lt;li&gt;Loop through the nums.&lt;/li&gt;
&lt;li&gt;Initialize a start variable and assign nums[i] to it.&lt;/li&gt;
&lt;li&gt;while i+1 is less than nums.length and nums[i] equals num[i]+1.&lt;/li&gt;
&lt;li&gt;Then i++.&lt;/li&gt;
&lt;li&gt;End while.&lt;/li&gt;
&lt;li&gt;If start is not equal to nums[i] then add first -&amp;gt; nums[i] to the list.&lt;/li&gt;
&lt;li&gt;Else, convert to string value and add to list&lt;/li&gt;
&lt;li&gt;End if.&lt;/li&gt;
&lt;li&gt;End for.&lt;/li&gt;
&lt;li&gt;Return list.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;First, initialize a list to store the range, then check if nums is empty if empty, then return the list else we can continue.&lt;/p&gt;

&lt;p&gt;Loop through the nums and assign the current value to an int, start. While i+1 is less than total length and nums[i]=nums[i]+1, then i++, ie goto the next value, because this is a continuous number.&lt;/p&gt;

&lt;p&gt;So when nums[i] != nums[i]+1 or have reached the end of the array, we will check if start!=num[i] — that means to check if start and current value is same or not; if they are not the same, then add start + "-&amp;gt;" + nums[i] to the list&lt;/p&gt;

&lt;p&gt;If they are same, then convert the int to a string and add to the list. And return the list.&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="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;summaryRanges&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;nums&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="nc"&gt;List&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;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;nums&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;0&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;result&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="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;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;nums&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="n"&gt;i&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;start&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&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;while&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="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;nums&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;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;nums&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="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]==&lt;/span&gt;&lt;span class="n"&gt;nums&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="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;++;&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;start&lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="n"&gt;nums&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="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="s"&gt;"-&amp;gt;"&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;nums&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;else&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;valueOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&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;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&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;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(n)&lt;/p&gt;

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

</description>
      <category>leetcode</category>
      <category>java</category>
      <category>programming</category>
    </item>
    <item>
      <title>Solving LeetCode 414 Third Maximum Number: A Streamlined Java Approach</title>
      <dc:creator>Jerin</dc:creator>
      <pubDate>Tue, 12 May 2026 15:36:14 +0000</pubDate>
      <link>https://dev.to/jerin_babu/solving-leetcode-414-third-maximum-number-a-streamlined-java-approach-1h6p</link>
      <guid>https://dev.to/jerin_babu/solving-leetcode-414-third-maximum-number-a-streamlined-java-approach-1h6p</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, Sorting&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;Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.&lt;/p&gt;

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

&lt;p&gt;Give back the 3rd largest number from the array if it exists, else give back the largest number.&lt;/p&gt;

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

&lt;p&gt;Confusing over the array indexing and length—array indexing starts at 0 and length starts at 1, always forget that.&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [3,2,1]
Output: 1
Explanation:
The first distinct maximum is 3.
The second distinct maximum is 2.
The third distinct maximum is 1.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [1,2]
Output: 2
Explanation:
The first distinct maximum is 2.
The second distinct maximum is 1.
The third distinct maximum does not exist, so the maximum (2) is returned instead.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ol&gt;
&lt;li&gt;Sort the array.&lt;/li&gt;
&lt;li&gt;Get the unique array.&lt;/li&gt;
&lt;li&gt;If less than 3, then return the largest.&lt;/li&gt;
&lt;li&gt;Else return the nums.length - 3.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;ol&gt;
&lt;li&gt;Sort the array.&lt;/li&gt;
&lt;li&gt;Initialize an array to store the unique array, using stream , distinct operation and toArray to make it into an array.&lt;/li&gt;
&lt;li&gt;If the array length is less than 3 then return the last digit of the array.&lt;/li&gt;
&lt;li&gt;Else return the element in total length-3.&lt;/li&gt;
&lt;li&gt;End if.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;Fisrt sort the array in ascending order to get the elements in order. Then find out the duplicating element using distinct() operation and discard it.&lt;br&gt;
Then using an if statment check if the array length is less than 3 , if yes then return the last digit which will be the largest digit.&lt;/p&gt;

&lt;p&gt;If greater than 3 then return the element in total length-3 that will be the 3rd largest digit.&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="nf"&gt;thirdMax&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;nums&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&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;uniqueArr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;distinct&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;toArray&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;uniqueArr&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;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;3&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;uniqueArr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;uniqueArr&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="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&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;uniqueArr&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;uniqueArr&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;3&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;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;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(nlogn)&lt;/p&gt;

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

</description>
      <category>leetcode</category>
      <category>devchallenge</category>
      <category>programming</category>
      <category>java</category>
    </item>
    <item>
      <title>Solving LeetCode 2553: Separate the Digits in an Array using Java</title>
      <dc:creator>Jerin</dc:creator>
      <pubDate>Mon, 11 May 2026 14:44:14 +0000</pubDate>
      <link>https://dev.to/jerin_babu/solving-leetcode-2553-separate-the-digits-in-an-array-using-java-18om</link>
      <guid>https://dev.to/jerin_babu/solving-leetcode-2553-separate-the-digits-in-an-array-using-java-18om</guid>
      <description>&lt;p&gt;&lt;strong&gt;Difficulty&lt;/strong&gt;: Easy&lt;br&gt;
&lt;strong&gt;Topics&lt;/strong&gt;: Array, Simulation&lt;br&gt;
&lt;strong&gt;Platform&lt;/strong&gt;: Leetcode&lt;/p&gt;

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

&lt;p&gt;Given an array of positive integers nums, return an array answer that consists of the digits of each integer in nums after separating them in the same order they appear in nums.&lt;br&gt;
To separate the digits of an integer is to get all the digits it has in the same order.&lt;br&gt;
For example, for the integer 10921, the separation of its digits is [1,0,9,2,1].&lt;/p&gt;

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

&lt;p&gt;Deconstruct each digits in nums array and return inorder in answer array.&lt;br&gt;
Mistakes and Learning&lt;br&gt;
Added the modules value to final array list without reversing it - Learned: Created another arraylist and added the values to it and then reversed and added to the main arraylist&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [13,25,83,77]
Output: [1,3,2,5,8,3,7,7]
Explanation: 
- The separation of 13 is [1,3].
- The separation of 25 is [2,5].
- The separation of 83 is [8,3].
- The separation of 77 is [7,7].
answer = [1,3,2,5,8,3,7,7]. Note that answer contains the separations in the same order.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [7,1,3,9]
Output: [7,1,3,9]
Explanation: The separation of each integer in nums is itself.
answer = [7,1,3,9].
Key Insight
Loop through each value in the array.
Take the modules of each digit and add them to an arraylist.
Reverse it and add it to the arraylist.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ol&gt;
&lt;li&gt;Initialize 2 arraylists.&lt;/li&gt;
&lt;li&gt;Loop through each digits of nums array.&lt;/li&gt;
&lt;li&gt;Initialize an integer to store the deconstructed values.&lt;/li&gt;
&lt;li&gt;While the nums[i]&amp;gt;0.&lt;/li&gt;
&lt;li&gt;Assign 10 modules of nums[i] to integer value.&lt;/li&gt;
&lt;li&gt;Add that value to an arraylist. &lt;/li&gt;
&lt;li&gt;Divide the nums[i] by 10.&lt;/li&gt;
&lt;li&gt;End while.&lt;/li&gt;
&lt;li&gt;Reverse the arraylist that stored the value.&lt;/li&gt;
&lt;li&gt;Add the reversed arraylist to another arraylist.&lt;/li&gt;
&lt;li&gt;Clear the reversed arraylist.&lt;/li&gt;
&lt;li&gt;Convert arraylist to int[].&lt;/li&gt;
&lt;li&gt;return answer.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;Initialize 2 arraylists one to save the array and another to save the modules value of element having more than single digit and reverse it. &lt;br&gt;
Loop through nums and initialize an integer to assign the modules value. While the nums[i] is greater than 0 so that when the nums[i] is &amp;lt; or = 0 it will go for another for loop iteration. The value becomes nums[i]%10. and add this value to the 2nd arraylist. Then nums[i]=nums[i]/10 - this will remove the last digit of the element. &lt;br&gt;
After the while loop ends we will reverse the arraylist and add it to the 1st arraylist, and clear the 2nd arraylist for next element.&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;separateDigits&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;nums&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;ArrayList&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;answers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
         &lt;span class="nc"&gt;ArrayList&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;answers2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;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="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;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;nums&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="n"&gt;i&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;value&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="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&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;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;nums&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="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;answers2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;nums&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="n"&gt;nums&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="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="nc"&gt;Collections&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;answers2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;answers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addAll&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;answers2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;answers2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;clear&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;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;answers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                         &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;mapToInt&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="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="na"&gt;toArray&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;answer&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;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(nm)&lt;br&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: O(nm)&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>programming</category>
      <category>java</category>
    </item>
    <item>
      <title>Optimizing Generosity: A Greedy Approach to LeetCode 455 (Assign Cookies)</title>
      <dc:creator>Jerin</dc:creator>
      <pubDate>Wed, 06 May 2026 14:49:24 +0000</pubDate>
      <link>https://dev.to/jerin_babu/leetcode-455-assign-cookies-simple-explanation-with-carry-logic-java-46hj</link>
      <guid>https://dev.to/jerin_babu/leetcode-455-assign-cookies-simple-explanation-with-carry-logic-java-46hj</guid>
      <description>&lt;p&gt;&lt;strong&gt;Difficulty&lt;/strong&gt;: Easy&lt;br&gt;
&lt;strong&gt;Topics&lt;/strong&gt;: Array, Two Pointers, Greedy, Sorting&lt;br&gt;
&lt;strong&gt;Platform&lt;/strong&gt;: Leetcode&lt;/p&gt;

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

&lt;p&gt;Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.&lt;br&gt;
Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] &amp;gt;= g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.&lt;br&gt;
Problem Statement Simplified&lt;br&gt;
Give each children a cookies and each children have a dreed factor, give the result of how many children were satisfied.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;Dont check for eact value - in greedy problems we should look for threhold not exact value.&lt;/li&gt;
&lt;li&gt;At first used the 2 lists that stored the 2 arrays then remove the one by one from lists after going through the loop - The problem : If remove an element from index 2, then the element that was at index 3 will slides to index 2, will skip that element.&lt;/li&gt;
&lt;/ol&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: g = [1,2,3], s = [1,1]
Output: 1
Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. 
And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
You need to output 1.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: g = [1,2], s = [1,2,3]
Output: 2
Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. 
You have 3 cookies and their sizes are big enough to gratify all of the children, 
You need to output 2.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If s[i] ≥ g[i] then child satisfied.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If s[i]&amp;lt;g[i] then child not satisfied.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt;Sort the 2 arrays.&lt;/li&gt;
&lt;li&gt;Initialize 2 integers for child and cookies.&lt;/li&gt;
&lt;li&gt;While the child is less than g.length and cookies is less than s.length.&lt;/li&gt;
&lt;li&gt;If s[cookies] is greater than or equal to g[child].&lt;/li&gt;
&lt;li&gt;Then child++.&lt;/li&gt;
&lt;li&gt;End if.&lt;/li&gt;
&lt;li&gt;Cookies++.&lt;/li&gt;
&lt;li&gt;End for loop.&lt;/li&gt;
&lt;li&gt;return child.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;First, sort the 2 arrays so that we can we can match smallest available cookies with smallest greed factor.&lt;br&gt;
Initialize 2 int pointers for child and cookies, then we will iterate and check if the current cookies can satify the current child if yes then we will move on to next child and next cookies, if not then we will move to next cookies . Return the child pointer with how many children are satisfied.&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="nf"&gt;findContentChildren&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;g&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;s&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&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;child&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;cookies&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="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;child&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;g&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;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;cookies&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;s&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="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;cookies&lt;/span&gt;&lt;span class="o"&gt;]&amp;gt;=&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;child&lt;/span&gt;&lt;span class="o"&gt;]){&lt;/span&gt;
                &lt;span class="n"&gt;child&lt;/span&gt;&lt;span class="o"&gt;++;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;cookies&lt;/span&gt;&lt;span class="o"&gt;++;&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;child&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 logn + m logm)&lt;br&gt;
Space Complexity: O(logn + logm)&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>java</category>
      <category>programming</category>
      <category>challenge</category>
    </item>
    <item>
      <title>LeetCode 387: Finding the First Unique Character - Frequency Mapping in Java</title>
      <dc:creator>Jerin</dc:creator>
      <pubDate>Wed, 06 May 2026 09:15:32 +0000</pubDate>
      <link>https://dev.to/jerin_babu/leetcode-387-first-unique-character-in-a-string-simple-explanation-with-carry-logic-java-4fem</link>
      <guid>https://dev.to/jerin_babu/leetcode-387-first-unique-character-in-a-string-simple-explanation-with-carry-logic-java-4fem</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;: Hash Table, String, Queue, Counting&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;Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.&lt;/p&gt;

&lt;p&gt;Problem Statement Simplified&lt;br&gt;
Get the index of the first non-repeating character, else return -1.&lt;/p&gt;

&lt;p&gt;Mistakes and Learning&lt;br&gt;
Do not blindly loop thorugh.&lt;br&gt;
Do not just delete the duplicates, remove the duplicated character also.&lt;/p&gt;

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

&lt;p&gt;`Input: s = "leetcode"&lt;/p&gt;

&lt;p&gt;Output: 0&lt;/p&gt;

&lt;p&gt;Explanation:&lt;/p&gt;

&lt;p&gt;The character 'l' at index 0 is the first character that does not occur at any other index.`&lt;/p&gt;

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

&lt;p&gt;`Input: s = "loveleetcode"&lt;/p&gt;

&lt;p&gt;Output: 2`&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Remove all the duplicating characters&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then get the first character&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Get the first character index.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If no unique then -1.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ol&gt;
&lt;li&gt;Convert the string to a char array.&lt;/li&gt;
&lt;li&gt;Intitialize a HashMap to store the character and the repeating counts.&lt;/li&gt;
&lt;li&gt;Intitialize a for loop&lt;/li&gt;
&lt;li&gt;Put the character in the HashMap with getOrDefault to check if the character already preset or not, if yes then increment by 1.&lt;/li&gt;
&lt;li&gt;End for loop&lt;/li&gt;
&lt;li&gt;Intitialize a for loop&lt;/li&gt;
&lt;li&gt;Check if the first value is 1 if yes then return the for loop index else continue the for loop&lt;/li&gt;
&lt;li&gt;End for loop&lt;/li&gt;
&lt;li&gt;return -1&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;First, convert the string to a char array. Then initialize a HashMap so that we can store each character with its repeating counts.&lt;br&gt;
Then initialize a for loop to add each character from character array to HashMap with getOrDefault ( if the value of preseent the increment b 1 else it will be 0 ), end of for loop. Then initialize another for loop to check the first character with value 1 from HashMap. if found then return the loop index else it will end the loop and return -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="nf"&gt;firstUniqChar&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;character&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toCharArray&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;Map&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Character&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;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="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;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;character&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="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++){&lt;/span&gt;
            &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;character&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="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getOrDefault&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;character&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="mi"&gt;0&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="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="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;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;character&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="n"&gt;i&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;map&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;character&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="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;i&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;span class="k"&gt;return&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="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(n)&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>java</category>
      <category>hashmap</category>
      <category>programming</category>
    </item>
    <item>
      <title>LeetCode 819: Finding the Most Common Word - String Cleaning and Counting in Java</title>
      <dc:creator>Jerin</dc:creator>
      <pubDate>Wed, 06 May 2026 07:05:19 +0000</pubDate>
      <link>https://dev.to/jerin_babu/leetcode-819-most-common-word-simple-explanation-with-carry-logic-java-190e</link>
      <guid>https://dev.to/jerin_babu/leetcode-819-most-common-word-simple-explanation-with-carry-logic-java-190e</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, Hash Table, String, Counting&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;Given a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer is unique.&lt;/p&gt;

&lt;p&gt;The words in paragraph are case-insensitive and the answer should be returned in lowercase.&lt;/p&gt;

&lt;p&gt;Note that words can not contain punctuation symbols.&lt;/p&gt;

&lt;p&gt;Problem Statement Simplified&lt;br&gt;
If in the paragraph string have the word in banned array, then discard it. If the paragraph string have ‘.’ or ‘ ’ then remove it. Make everything lowercase. Then give back the word that is most frequent.&lt;/p&gt;

&lt;p&gt;Mistakes and Learning&lt;br&gt;
Forgetting about removing ‘.’ or ‘ ’.&lt;br&gt;
Look out for rare cases (ex:[a.])&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.", banned = ["hit"]
Output: "ball"
Explanation: 
"hit" occurs 3 times, but it is a banned word.
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. 
Note that words in the paragraph are not case sensitive,
that punctuation is ignored (even if adjacent to words, such as "ball,"), 
and that "hit" isn't the answer even though it occurs more because it is banned.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: paragraph = "a.", banned = []
Output: "a"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Initialize a HashMap and HashSet for paragraph String and banned array.&lt;br&gt;
If not a word from banned array then add to HashMap and increment the counter&lt;br&gt;
If a word from banned array then move to next word.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;Remove everything from string other than [a-z] and make it lowercase.&lt;/li&gt;
&lt;li&gt;Initialize a String array and add the string words to it using split function.&lt;/li&gt;
&lt;li&gt;Initialize HashMap and HashSet for string array and banned word array respectively.&lt;/li&gt;
&lt;li&gt;Initialize an int max and string maxWord to check and return the max repeating word.&lt;/li&gt;
&lt;li&gt;Initialize a for loop and add the banned array to HashSet.
end for loop&lt;/li&gt;
&lt;li&gt;Initialize a for loop.&lt;/li&gt;
&lt;li&gt;Store the current word in String array to a string.&lt;/li&gt;
&lt;li&gt;If not a word in HashSet.&lt;/li&gt;
&lt;li&gt;Put in HashMap and increment the value if not already present.&lt;/li&gt;
&lt;li&gt;If value is greater than max.&lt;/li&gt;
&lt;li&gt;Assign the value to max.&lt;/li&gt;
&lt;li&gt;Assign word to maxWord.&lt;/li&gt;
&lt;li&gt;end if.&lt;/li&gt;
&lt;li&gt;end if&lt;/li&gt;
&lt;li&gt;end for loop.&lt;/li&gt;
&lt;li&gt;return max word.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;First, remove everything from string other than [a-z] and make it lowercase. Then split the string and store it in an array.&lt;br&gt;
Initialize a HashMap to store the string and integer - so that we can just look at this hashmap and check how many times the string repeated. everytime we loop through the string array (we created earlier), we will store the words in hashmap and assign a value to it which will increment when the word is repeated in the array.&lt;/p&gt;

&lt;p&gt;Initialize a HashSet to store the string — so that the banned word array can be stored and compared with hashmap.&lt;/p&gt;

&lt;p&gt;Initialize an int max and string maxWord to check and return the max repeating word.&lt;/p&gt;

&lt;p&gt;Initialize a for loop which will go thorugh the banned words array and add them to HashSet.&lt;/p&gt;

&lt;p&gt;Then we will loop through the string array and check the HashSet and current word from String array. If same then we will move to next word from String array. If not then we will add it to HashMap and if the word already exists then we will increment the value of it ( using map.getOrDefault(word,0) it will return the current value, if the word doesnt exists then it wil return 0 otherwise the current value of that word from HashMap ).&lt;/p&gt;

&lt;p&gt;Then we will check of this value is greater than max, if yes then we will set this value as new max and this word as new maxWord.&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="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;mostCommonWord&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;paragraph&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;banned&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;paragraph&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;paragraph&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toLowerCase&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;replaceAll&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"[^a-z]"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;paragraph&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;split&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"\\s+"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bannedWord&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashSet&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;counter&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;max&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;maxWord&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&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="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;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;banned&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="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++){&lt;/span&gt;
            &lt;span class="n"&gt;bannedWord&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;banned&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="na"&gt;toLowerCase&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="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;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;words&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="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++){&lt;/span&gt;
             &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;words&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;if&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;bannedWord&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;)){&lt;/span&gt;
            &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getOrDefault&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&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="mi"&gt;1&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;map&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;)&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
                &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;maxWord&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;word&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;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;maxWord&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+m)&lt;/p&gt;

&lt;p&gt;Space Complexity: O(n+m)&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>hashmap</category>
      <category>java</category>
      <category>programming</category>
    </item>
    <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>
