<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ved Asole on Medium]]></title>
        <description><![CDATA[Stories by Ved Asole on Medium]]></description>
        <link>https://medium.com/@vedasole?source=rss-ae7c401dc216------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*WmYbq6fxAlqaZ4UI-mEQSg.png</url>
            <title>Stories by Ved Asole on Medium</title>
            <link>https://medium.com/@vedasole?source=rss-ae7c401dc216------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 05 Jun 2026 14:55:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vedasole/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Leetcode 268— Missing Number]]></title>
            <link>https://medium.com/@vedasole/leetcode-268-missing-number-0dadc1ec38d3?source=rss-ae7c401dc216------2</link>
            <guid isPermaLink="false">https://medium.com/p/0dadc1ec38d3</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[missing-number]]></category>
            <category><![CDATA[leetcode-easy]]></category>
            <category><![CDATA[dsa-training]]></category>
            <dc:creator><![CDATA[Ved Asole]]></dc:creator>
            <pubDate>Sun, 15 Sep 2024 21:29:55 GMT</pubDate>
            <atom:updated>2024-09-15T21:31:11.013Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*oB01aaATYi6Uugufc-It_Q.jpeg" /><figcaption>Find the Missing Number?</figcaption></figure><p>Question Link : <a href="https://leetcode.com/problems/missing-number">Missing Number</a></p><h4><strong>Problem Statement</strong>:</h4><p>You are given an array containing n distinct numbers taken from the range 0 to n. Find the one number that is missing from the array.</p><p><strong>Example 1</strong>:</p><ul><li><strong>Input</strong>: nums = [3, 0, 1]</li><li><strong>Output</strong>: 2</li><li><strong>Explanation</strong>: The numbers in the range are {0, 1, 2, 3}, and the missing number is 2.</li></ul><p><strong>Example 2</strong>:</p><ul><li><strong>Input</strong>: nums = [0, 1]</li><li><strong>Output</strong>: 2</li><li><strong>Explanation</strong>: The numbers in the range are {0, 1, 2}, and the missing number is 2.</li></ul><p><strong>Constraints</strong>:</p><ul><li>n == nums.length</li><li>1 &lt;= n &lt;= 10^4</li><li>0 &lt;= nums[i] &lt;= n</li><li>All the numbers of nums are unique.</li></ul><h3><strong>Solution</strong>:</h3><h4><strong>Approach</strong>:</h4><p>The problem asks for the missing number from the array of natural numbers. A mathematical formula can help us solve it efficiently.</p><ol><li>We use the well-known formula for the sum of the first N natural numbers:</li></ol><p>Sum of N natural numbers= n(n+1)/2</p><ol><li>By calculating the sum of the actual numbers in the array and comparing it to the expected sum from the formula, we can find the missing number as the difference between these two sums.</li></ol><p><strong>Example Walkthrough</strong>:<br>Let’s walk through an example with the array <strong>[3, 0, 1]</strong>:</p><ul><li>Using the formula:</li></ul><p>Sum=3×(3+1)/2=6</p><ul><li>Calculating the sum of the array values:</li></ul><p>Array Sum=3+0+1=4</p><ul><li>Therefore, the missing number is: 6−4 = 2</li></ul><p><strong>Performance</strong>:</p><ul><li><strong>Time Complexity</strong>: O(N) — We iterate over the array once to calculate the sum.</li><li><strong>Space Complexity</strong>: O(1) — No extra space is used.</li></ul><p><strong>Code</strong>:</p><pre>class Solution {<br>    public int missingNumber(int[] nums) {<br>        int sum = 0;<br>        for (int i : nums) sum += i;<br>        return (nums.length * (nums.length + 1) / 2) - sum;<br>    }<br>}</pre><p>The above approach ensures that we can solve the problem in linear time with constant space, making it highly efficient for large datasets.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0dadc1ec38d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 13 — Roman to Integer]]></title>
            <link>https://medium.com/@vedasole/leetcode-13-roman-to-integer-98fcf865c6ff?source=rss-ae7c401dc216------2</link>
            <guid isPermaLink="false">https://medium.com/p/98fcf865c6ff</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[leetcode-13]]></category>
            <category><![CDATA[leetcode-easy]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Ved Asole]]></dc:creator>
            <pubDate>Thu, 07 Sep 2023 21:10:25 GMT</pubDate>
            <atom:updated>2023-09-07T21:10:25.733Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*SNzYmEvaxDc-n9Xy4ffb5g.jpeg" /></figure><h3>Leetcode 13 — <a href="https://leetcode.com/problems/roman-to-integer">Roman to Integer</a></h3><p>Question Link : <a href="https://leetcode.com/problems/roman-to-integer">Roman to Integer</a></p><h3>Problem Statement :</h3><p>Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.</p><pre>Symbol       Value<br>I             1<br>V             5<br>X             10<br>L             50<br>C             100<br>D             500<br>M             1000</pre><p>For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.</p><p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:</p><ul><li>I can be placed before V (5) and X (10) to make 4 and 9.</li><li>X can be placed before L (50) and C (100) to make 40 and 90.</li><li>C can be placed before D (500) and M (1000) to make 400 and 900.</li></ul><p>Given a roman numeral, convert it to an integer.</p><p><strong>Example 1:</strong></p><pre>Input: s = &quot;III&quot;<br>Output: 3<br>Explanation: III = 3.</pre><p><strong>Example 2:</strong></p><pre>Input: s = &quot;LVIII&quot;<br>Output: 58<br>Explanation: L = 50, V= 5, III = 3.</pre><p><strong>Example 3:</strong></p><pre>Input: s = &quot;MCMXCIV&quot;<br>Output: 1994<br>Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.</pre><p><strong>Constraints:</strong></p><ul><li>1 &lt;= s.length &lt;= 15</li><li>s contains only the characters (&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;).</li><li>It is <strong>guaranteed</strong> that s is a valid roman numeral in the range [1, 3999].</li></ul><h3><strong>Solution:</strong></h3><h4><strong>Approach : Comparing the roman numbers with the next one</strong></h4><p>Firstly, we will implement the switch statements to calculate and get the value of the roman numberals. Then we will take integer value of each roman numberal from the input string and compare it with the integer value of the next roman numeral. If the next roman numberal is greater than the previous one then we will substract it from the total sum otherwise we will add it to the sum.</p><h3><strong>Explanation :</strong></h3><p>Before running the logic for the program, we used the System.gc() command to run the garbage collector in java and release the memory.</p><p>Now, we will define four variables as below :</p><p>1. sum= total sum of the integer values of each roman numeral</p><p>2. num= the current roman numeral value in the iteration.</p><p>3. next= the next roman numeral value after the current number.</p><p>4. strLength= the length of the roman number.</p><blockquote><strong>Note</strong>: We are defining variable for the string length outside the for loop so that the program only checks the length 1 time for the string. If we check the string length inside the for loop codition then it will check the String length for every iteration.</blockquote><p>Now, we will use for loop to iterate over each character(roman numeral) in the roman number string. We will define a switch which will check the current index character and will assign it’s integral value to num variable. Similarly, the 2nd switch is defined for getting the integral value of the next character from the roman number string and store it in the next variable. Before the 2nd switch statement, we check if the current value of the iteration index is not the same as strLength— 1as it will cause an <strong>ArrayIndexOutOfBound</strong> Exception for the last iteration when there will be no next numeral.</p><p>Now, after getting the values of both the current and next roman numerals, we will check if the current number is less than next. If it’s less than next number then it means that we need to subtract that numeral value from the next roman number value or the sum . Else we can directly add the current number value to the sum variable.</p><p>Finally, at the end of the program we will return the total sum.</p><h3><strong>Performance :</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/765/1*wLvGxMIlxHP0hTnaK0l-rA.png" /></figure><h3><strong>Code :</strong></h3><pre>class Solution {<br><br>    static {<br>        System.gc();<br>    }<br>    <br>    public int romanToInt(String s) {<br><br>        int sum=0;<br>        int num=0;<br>        int next=0;<br>        int strLength=s.length();<br><br>        for(int i=0; i&lt;strLength;i++){<br><br>            switch(s.charAt(i)){<br>                case &#39;I&#39; -&gt; num = 1;<br>                case &#39;V&#39; -&gt; num = 5;<br>                case &#39;X&#39; -&gt; num = 10;<br>                case &#39;L&#39; -&gt; num = 50;<br>                case &#39;C&#39; -&gt; num = 100;<br>                case &#39;D&#39; -&gt; num = 500;<br>                case &#39;M&#39; -&gt; num = 1000;<br>            }<br>            if(i&lt;strLength-1){<br>                switch(s.charAt(i+1)){<br>                    case &#39;I&#39; -&gt; next = 1;<br>                    case &#39;V&#39; -&gt; next = 5;<br>                    case &#39;X&#39; -&gt; next = 10;<br>                    case &#39;L&#39; -&gt; next = 50;<br>                    case &#39;C&#39; -&gt; next = 100;<br>                    case &#39;D&#39; -&gt; next = 500;<br>                    case &#39;M&#39; -&gt; next = 1000;<br>                }<br>            }<br><br>            if(num &lt; next) sum-=num;<br>            else sum+=num;<br>        }<br><br>        return sum;<br>    }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=98fcf865c6ff" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode-9. Palindrome Numbers]]></title>
            <link>https://medium.com/@vedasole/leetcode-9-palindrome-number-1f2efc6d4292?source=rss-ae7c401dc216------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f2efc6d4292</guid>
            <category><![CDATA[leetcode-easy]]></category>
            <category><![CDATA[palindrome-number]]></category>
            <category><![CDATA[leetcode-9]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Ved Asole]]></dc:creator>
            <pubDate>Wed, 06 Sep 2023 14:35:24 GMT</pubDate>
            <atom:updated>2023-10-31T12:09:11.492Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Palindrome Numbers" src="https://cdn-images-1.medium.com/max/1024/1*p5fV1yU22ba6eIniOC2YWg.jpeg" /></figure><h3>Leetcode 9 — Palindrome Number Problem</h3><p>Question Link : <a href="https://leetcode.com/problems/palindrome-number/">Palindrome Number</a></p><h3><strong>Problem Statement :</strong></h3><p>Given an integer x, return true<em> if </em>x<em> is a </em><strong><em>palindrome</em></strong><em>, and </em>false<em> otherwise</em>.</p><p><strong>Example 1:</strong></p><pre>Input: x = 121<br>Output: true<br>Explanation: 121 reads as 121 from left to right and from right to left.</pre><p><strong>Example 2:</strong></p><pre>Input: x = -121<br>Output: false<br>Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.</pre><p><strong>Example 3:</strong></p><pre>Input: x = 10<br>Output: false<br>Explanation: Reads 01 from right to left. Therefore it is not a palindrome.</pre><p><strong>Constraints:</strong></p><ul><li>-231 &lt;= x &lt;= 231 - 1</li></ul><p><strong>Follow up:</strong> Could you solve it without converting the integer to a string?</p><h3><strong>Solution :</strong></h3><h4>Approach :Reversing Half of the Number</h4><p>Instead of reversing the entire number, we can reverse only the last half of the number. This approach is tricky because when we reverse the last half of the number, we don’t want the middle digit to be rev back to its original value. This can happen if the number has an odd number of digits. To resolve this, we can compare the first half of the number with the rev second half of the number.</p><h4>Explanation :</h4><p>We begin with an initial check to handle special cases:</p><p>If the input number x is negative, it cannot be a palindrome since palindromes are typically defined for positive numbers. In such cases, we immediately return false.<br>If x is non-zero and ends with a zero, it cannot be a palindrome because leading zeros are not allowed in palindromes. We return false for such cases.<br>We initialize two variables:</p><p>rev: This variable will store the rev second half of the digits of the number.<br>temp: This variable is a temporary placeholder to manipulate the input number without modifying the original value.<br>We enter a loop that continues until the first half of the digits (x) becomes less than or equal to the rev second half (rev):</p><p>Inside the loop, we extract the last digit of x using the modulo operator % and add it to the rev variable after multiplying it by 10 (shifting the existing digits to the left).<br>We then divide x by 10 to remove the last digit and move towards the center of the number.<br>Once the loop is completed, we have rev the second half of the digits. Now, we compare the first half of the digits (x) with the rev second half (rev) to determine if the number is a palindrome:</p><p>For an even number of digits, if x is equal to rev, then the number is a palindrome. We return true.<br>For an odd number of digits, if x is equal to rev / 10 (ignoring the middle digit), then the number is a palindrome. We return true.<br>If none of the above conditions are met, it means the number is not a palindrome, so we return false.<br>The code avoids the need for reversing the entire number by comparing only the necessary parts. This approach reduces both time complexity and memory usage, resulting in a more efficient solution.</p><h4>Performance :</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/764/0*OAzh_ZIWMWeA1nm6.png" /></figure><h4>Code :</h4><pre>class Solution {<br> public boolean isPalindrome(int x) {<br> if (x &lt; 0 || (x != 0 &amp;&amp; x % 10 == 0))return false;<br> int rev=0;<br> int original=x;<br> while(x&gt;rev) {<br> rev = rev*10+(x%10);<br> x/=10;<br> }<br> return (x==rev) || (x==rev/10);<br> }<br>}<br></pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f2efc6d4292" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>