<?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 Yuvraj singh on Medium]]></title>
        <description><![CDATA[Stories by Yuvraj singh on Medium]]></description>
        <link>https://medium.com/@singhyuvraj8000?source=rss-0445aa0473a9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*2JaYI92l-Vg2CqDy</url>
            <title>Stories by Yuvraj singh on Medium</title>
            <link>https://medium.com/@singhyuvraj8000?source=rss-0445aa0473a9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 06:39:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@singhyuvraj8000/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[ Day 41: Hamming Weight — Counting 1 Bits in an Integer]]></title>
            <link>https://medium.com/@singhyuvraj8000/day-41-hamming-weight-counting-1-bits-in-an-integer-a640bc487136?source=rss-0445aa0473a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/a640bc487136</guid>
            <category><![CDATA[hamming-code]]></category>
            <category><![CDATA[bit-manipulation]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data-structure-algorithm]]></category>
            <dc:creator><![CDATA[Yuvraj singh]]></dc:creator>
            <pubDate>Fri, 19 Sep 2025 06:24:05 GMT</pubDate>
            <atom:updated>2025-09-19T06:24:05.951Z</atom:updated>
            <content:encoded><![CDATA[<h3>🚀 Day 41: Hamming Weight — Counting 1 Bits in an Integer</h3><p>Today’s challenge was about <strong>finding the number of 1s in the binary representation of a number</strong>. This is also known as the <strong>Hamming Weight</strong>.</p><h3>🔑 Problem Statement</h3><p>Given an integer, count how many bits are set to 1 in its 32-bit binary representation.</p><h3>🧠 Approach</h3><ol><li>Loop through all <strong>32 bits</strong> of the integer.</li><li>Use <strong>bitwise AND (</strong><strong>n &amp; 1)</strong> to check if the last bit is 1.</li><li>If yes, increment count.</li><li>Right shift n (n &gt;&gt;= 1) to check the next bit.</li></ol><h3>💻 Java Implementation</h3><pre>public class Solution {<br>    // you need to treat n as an unsigned value<br>    public int hammingWeight(int n) {<br>        int count = 0;<br>        for(int i = 0; i &lt; 32; i++) {<br>            if((n &amp; 1) == 1) {<br>                count++;<br>            }<br>            n &gt;&gt;= 1; // shift bits to the right<br>        }<br>        return count;<br>    }<br>}</pre><h3>⏱️ Complexity Analysis</h3><ul><li><strong>Time Complexity:</strong> O(32) → O(1) (since integer has fixed 32 bits)</li><li><strong>Space Complexity:</strong> O(1)</li></ul><h3>📌 Key Takeaway</h3><p>This problem highlights the power of <strong>bit manipulation</strong> in solving low-level problems efficiently.</p><p>Another step closer to mastering problem-solving 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a640bc487136" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Day 40: Detecting a Cycle in a Linked List (Floyd’s Algorithm)]]></title>
            <link>https://medium.com/@singhyuvraj8000/day-40-detecting-a-cycle-in-a-linked-list-floyds-algorithm-ecbfcda385ed?source=rss-0445aa0473a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/ecbfcda385ed</guid>
            <category><![CDATA[linked-lists]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[interview-questions]]></category>
            <category><![CDATA[data-structure-algorithm]]></category>
            <dc:creator><![CDATA[Yuvraj singh]]></dc:creator>
            <pubDate>Sat, 13 Sep 2025 07:01:14 GMT</pubDate>
            <atom:updated>2025-09-13T07:01:14.628Z</atom:updated>
            <content:encoded><![CDATA[<p>In today’s challenge, I tackled the <strong>Linked List Cycle Detection problem</strong> — a common interview question that tests understanding of linked lists and pointer manipulation.</p><h3>🔑 Problem Statement</h3><p>Given the head of a linked list, determine if the list contains a cycle. A cycle occurs when a node’s next pointer points back to a previous node instead of null.</p><h3>Approach: Floyd’s Tortoise and Hare Algorithm</h3><p>We use <strong>two pointers</strong>:</p><ul><li><strong>slow pointer</strong> → moves one step at a time</li><li><strong>fast pointer</strong> → moves two steps at a time</li></ul><p>If there is a cycle, the two pointers will eventually meet.<br> If fast reaches null, there is no cycle.</p><h3>💻 Java Implementation</h3><pre>public class Solution {<br>    public boolean hasCycle(ListNode head) {<br>        ListNode fast = head;<br>        ListNode slow = head;</pre><pre>        while (fast != null &amp;&amp; fast.next != null) {<br>            fast = fast.next.next;<br>            slow = slow.next;<br>            if (fast == slow) {<br>                return true; // cycle found<br>            }<br>        }<br>        return false; // no cycle<br>    }<br>}</pre><h3>⏱️ Complexity Analysis</h3><ul><li><strong>Time Complexity:</strong> O(n), since both pointers traverse the list at most once.</li><li><strong>Space Complexity:</strong> O(1), as no extra data structures are used.</li></ul><h3>📌 Key Takeaway</h3><p>This algorithm is a classic example of using pointer techniques to solve problems in an efficient and elegant way.</p><p>On to the next day of coding! 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ecbfcda385ed" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 39 — Next Permutation (LeetCode — Medium)]]></title>
            <link>https://medium.com/@singhyuvraj8000/day-39-next-permutation-leetcode-medium-eb3ed5657426?source=rss-0445aa0473a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb3ed5657426</guid>
            <category><![CDATA[data-structure-algorithm]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[interview-preparation]]></category>
            <category><![CDATA[problem-solving]]></category>
            <dc:creator><![CDATA[Yuvraj singh]]></dc:creator>
            <pubDate>Fri, 12 Sep 2025 07:07:57 GMT</pubDate>
            <atom:updated>2025-09-12T07:07:57.328Z</atom:updated>
            <content:encoded><![CDATA[<h3>Day 39 — Next Permutation (LeetCode — Medium)</h3><p><strong>Problem Statement:</strong><br> Implement the next permutation of a sequence of numbers in-place.</p><ul><li>If possible, rearrange to the next greater lexicographic order.</li><li>If not, rearrange into the smallest permutation.</li></ul><h3>🔹 Solution Idea</h3><ul><li>Traverse from right → left to find the first <strong>descending element</strong> (pivot).</li><li>If no pivot → reverse entire array (last permutation case).</li><li>Otherwise:</li><li>Find the <strong>smallest number greater than pivot</strong> from the right.</li><li>Swap them.</li><li>Reverse suffix for smallest lexicographic order</li></ul><h3>🔹 Java Solution</h3><pre>class Solution {<br>    public void nextPermutation(int[] nums) {<br>        int n = nums.length;<br>        int i = n - 2;</pre><pre>        while (i &gt;= 0 &amp;&amp; nums[i] &gt;= nums[i + 1]) i--;</pre><pre>        if (i &lt; 0) {<br>            reverse(nums, 0, n - 1);<br>        } else {<br>            int j = n - 1;<br>            while (nums[j] &lt;= nums[i]) j--;<br>            swap(nums, i, j);<br>            reverse(nums, i + 1, n - 1);<br>        }<br>    }</pre><pre>    private void swap(int[] nums, int i, int j) {<br>        int temp = nums[i];<br>        nums[i] = nums[j];<br>        nums[j] = temp;<br>    }</pre><pre>    private void reverse(int[] nums, int i, int j) {<br>        while (i &lt; j) swap(nums, i++, j--);<br>    }<br>}</pre><h3>🔹 Complexity</h3><ul><li><strong>Time Complexity:</strong> O(n)</li><li><strong>Space Complexity:</strong> O(1), in-place manipulation</li></ul><h3>🔹 Key Takeaways</h3><ul><li>Mastering this helps in <strong>permutation generation</strong> problems.</li><li>It’s a good application of <strong>greedy + array reversal</strong> tricks.</li><li>Classic interview problem for <strong>FAANG-level companies</strong>.</li></ul><p>🚀 That’s Day 39! Stepping closer to the big 4–0 in my coding streak.</p><p>👉 Have you tried implementing permutations recursively before?</p><p>#100DaysOfCode #LeetCode #Java #CodingInterview #Microsoft #Amazon #ProblemSolving</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb3ed5657426" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Day 38 — Number of Substrings With Only 1s (LeetCode — Medium)]]></title>
            <link>https://medium.com/@singhyuvraj8000/day-38-number-of-substrings-with-only-1s-leetcode-medium-a4754cf1b568?source=rss-0445aa0473a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/a4754cf1b568</guid>
            <category><![CDATA[interview-preparation]]></category>
            <category><![CDATA[data-structure-algorithm]]></category>
            <category><![CDATA[string]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Yuvraj singh]]></dc:creator>
            <pubDate>Wed, 10 Sep 2025 05:14:06 GMT</pubDate>
            <atom:updated>2025-09-10T05:14:06.190Z</atom:updated>
            <content:encoded><![CDATA[<h3>📝 Day 38 — Number of Substrings With Only 1s (LeetCode — Medium)</h3><p><strong>Problem Statement:</strong><br> You are given a binary string s. Return the number of substrings that consist only of 1s. Since the number may be large, return it modulo 1e9+7.</p><h3>🔹 Approach</h3><ol><li>Traverse the string character by character.</li><li>Maintain a counter count for consecutive 1s.</li><li>When a 0 is encountered →</li></ol><ul><li>Calculate substrings from the streak using formula:</li><li>substrings=count×(count+1)2\text{substrings} = \frac{count \times (count+1)}{2}substrings=2count×(count+1)​</li><li>Add to answer modulo 1e9+7.</li><li>Reset count.</li></ul><ol><li>At the end, handle the last streak.</li></ol><h3>🔹 Java Solution</h3><pre>class Solution {<br>    public int numSub(String s) {<br>        int mod = 1000000007;<br>        long count = 0, ans = 0;</pre><pre>        for(int i = 0; i &lt; s.length(); i++) {<br>            if(s.charAt(i) == &#39;1&#39;) {<br>                count++;<br>            } else {<br>                long add = (count * (count + 1)) / 2;<br>                ans = (ans + add) % mod;<br>                count = 0;<br>            }<br>        }</pre><pre>        // Handle last streak<br>        long add = (count * (count + 1)) / 2;<br>        ans = (ans + add) % mod;</pre><pre>        return (int) ans;<br>    }<br>}</pre><h3>🔹 Complexity Analysis</h3><ul><li><strong>Time Complexity:</strong> O(n), single pass over the string.</li><li><strong>Space Complexity:</strong> O(1), only counters used.</li></ul><h3>🔹 Key Takeaways</h3><ul><li>Efficient substring counting relies on <strong>mathematical formulas</strong>.</li><li>Modulo handling is crucial in problems with large outputs.</li><li>Common interview topic for <strong>Microsoft &amp; Google</strong>.</li></ul><p>🚀 That’s Day 38! Slowly building mastery, one problem at a time.<br> 👉 What’s your favorite trick for handling large numbers in coding interviews?</p><p>#100DaysOfCode #LeetCode #Java #CodingInterview #ProblemSolving</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a4754cf1b568" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 37 of 100 Days of DSA — Defanging an IP Address]]></title>
            <link>https://medium.com/@singhyuvraj8000/day-37-of-100-days-of-dsa-defanging-an-ip-address-0f2e0bccf98b?source=rss-0445aa0473a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/0f2e0bccf98b</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data-structure-algorithm]]></category>
            <dc:creator><![CDATA[Yuvraj singh]]></dc:creator>
            <pubDate>Wed, 03 Sep 2025 13:45:22 GMT</pubDate>
            <atom:updated>2025-09-03T13:45:22.108Z</atom:updated>
            <content:encoded><![CDATA[<h3>Day 37 of 100 Days of DSA — Defanging an IP Address</h3><p>Today’s problem is a <strong>string manipulation exercise</strong> from LeetCode that highlights efficient handling of string transformations in Java.</p><h3>Problem Statement</h3><p>Given a valid IP address, return a defanged version where every . is replaced with [.].</p><p>Examples:</p><ul><li>Input: &quot;1.1.1.1&quot; → Output: &quot;1[.]1[.]1[.]1&quot;</li><li>Input: &quot;255.100.50.0&quot; → Output: &quot;255[.]100[.]50[.]0&quot;</li></ul><h3>Approach 1: StringBuilder (Efficient)</h3><ul><li>Initialize an empty StringBuilder.</li><li>Traverse the input string.</li><li>For each character:</li><li>If it’s ., append [.].</li><li>Else, append the character itself.</li><li>Return the built string.</li></ul><h3>Code (Java)</h3><pre>class Solution {<br>    public String defangIPaddr(String address) {<br>        StringBuilder str = new StringBuilder();<br>        for (int i = 0; i &lt; address.length(); i++) {<br>            if (address.charAt(i) == &#39;.&#39;) {<br>                str.append(&quot;[.]&quot;);<br>            } else {<br>                str.append(address.charAt(i));<br>            }<br>        }<br>        return str.toString();<br>    }<br>}</pre><h3>Complexity Analysis</h3><ul><li><strong>Time Complexity:</strong> O(N), where N is the length of the string.</li><li><strong>Space Complexity:</strong> O(N), since we are building a new string.</li></ul><h3>Alternative Approach: Using replace()</h3><p>Java provides a direct method:</p><pre>return address.replace(&quot;.&quot;, &quot;[.]&quot;);</pre><p>👉 This is shorter but slightly less efficient because it internally uses regex.</p><h3>Key Takeaway</h3><p>While this problem looks simple, it highlights the difference between <strong>string immutability</strong> and <strong>efficient string handling</strong> in Java.<br> Using <strong>StringBuilder</strong> is a best practice when multiple concatenations are involved.</p><p>💡 Which approach do you prefer — <strong>manual traversal with StringBuilder</strong> or a one-liner replace()?</p><p><strong>Tags:</strong><br> #100DaysOfCode #DSA #LeetCode #ProblemSolving #Java #InterviewPrep #CodingChallenge</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0f2e0bccf98b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 36 of 100 Days of DSA — Valid Palindrome]]></title>
            <link>https://medium.com/@singhyuvraj8000/day-36-of-100-days-of-dsa-valid-palindrome-58386602253c?source=rss-0445aa0473a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/58386602253c</guid>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[data-structure-algorithm]]></category>
            <category><![CDATA[valid-palindrome]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[arrays]]></category>
            <dc:creator><![CDATA[Yuvraj singh]]></dc:creator>
            <pubDate>Sat, 30 Aug 2025 14:46:09 GMT</pubDate>
            <atom:updated>2025-08-30T14:46:09.679Z</atom:updated>
            <content:encoded><![CDATA[<h3>Day 36 of 100 Days of DSA — Valid Palindrome</h3><p>Today’s problem is another <strong>LeetCode Easy</strong> question that tests <strong>string manipulation</strong> and <strong>two-pointer technique</strong>.</p><h3>Problem Statement</h3><p>Given a string s, return true if it is a palindrome, ignoring non-alphanumeric characters and case differences.</p><p>Examples:</p><ul><li>Input: &quot;A man, a plan, a canal: Panama&quot; → Output: true</li><li>Input: &quot;race a car&quot; → Output: false</li></ul><h3>Approach 1: Two Pointers (Optimal)</h3><ol><li>Initialize start = 0, end = s.length()-1.</li><li>While start &lt; end:</li></ol><ul><li>Skip non-alphanumeric characters.</li><li>Compare lowercase versions of characters.</li><li>If mismatch → return false.</li></ul><ol><li>If loop completes → return true.</li></ol><h3>Code (Java)</h3><pre>public boolean isPalindrome(String s) {<br>    int start = 0, end = s.length() - 1;<br>    while (start &lt;= end) {<br>        if (!Character.isLetterOrDigit(s.charAt(start))) {<br>            start++;<br>        } else if (!Character.isLetterOrDigit(s.charAt(end))) {<br>            end--;<br>        } else if (Character.toLowerCase(s.charAt(start)) != <br>                   Character.toLowerCase(s.charAt(end))) {<br>            return false;<br>        } else {<br>            start++;<br>            end--;<br>        }<br>    }<br>    return true;<br>}</pre><h3>Complexity</h3><ul><li><strong>Time Complexity:</strong> O(N), where N = length of string</li><li><strong>Space Complexity:</strong> O(1)</li></ul><h3>Alternative Approach: Regex Preprocessing</h3><ul><li>Clean the string using regex: s.replaceAll(&quot;[^a-zA-Z0-9]&quot;, &quot;&quot;).toLowerCase().</li><li>Then check palindrome with two pointers.</li></ul><p>👉 While simpler, regex has extra overhead compared to direct two-pointer traversal.</p><h3>Key Takeaway</h3><p>This problem shows the <strong>power of two pointers</strong> for string problems.<br> Efficiently handling <strong>edge cases like spaces, punctuation, and case sensitivity</strong> is the main challenge.</p><p>💡 Which method would you pick in an interview — <strong>regex cleanup</strong> or <strong>in-place two pointers</strong>?</p><p><strong>Tags:</strong><br> #100DaysOfCode #DSA #LeetCode #ProblemSolving #Java #InterviewPrep #CodingChallenge</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58386602253c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 35 of 100 Days of DSA — Palindrome Number]]></title>
            <link>https://medium.com/@singhyuvraj8000/day-35-of-100-days-of-dsa-palindrome-number-5564a69c8f3b?source=rss-0445aa0473a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/5564a69c8f3b</guid>
            <category><![CDATA[data-structure-algorithm]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[problem-solving]]></category>
            <dc:creator><![CDATA[Yuvraj singh]]></dc:creator>
            <pubDate>Fri, 29 Aug 2025 15:51:27 GMT</pubDate>
            <atom:updated>2025-08-29T15:51:27.664Z</atom:updated>
            <content:encoded><![CDATA[<h3>Day 35 of 100 Days of DSA — Palindrome Number</h3><p>Today’s problem is a <strong>LeetCode Easy</strong> question often asked in coding interviews.</p><h3>Problem Statement</h3><p>Given an integer x, return true if x is a palindrome, and false otherwise.</p><p>A palindrome number reads the same backward as forward.</p><p>Examples:</p><ul><li>Input: 121 → Output: true</li><li>Input: -121 → Output: false</li><li>Input: 10 → Output: false</li></ul><h3>Approach 1: Math-based Reversal</h3><ol><li>If x &lt; 0 → immediately return false.</li><li>Reverse the integer using modulo (%) and division (/).</li><li>Compare reversed number with the original.</li></ol><h3>Code (Java)</h3><pre>public boolean isPalindrome(int x) {<br>    if (x &lt; 0) return false;</pre><pre>    int rev = 0, temp = x;<br>    while (temp != 0) {<br>        int digit = temp % 10;<br>        rev = rev * 10 + digit;<br>        temp /= 10;<br>    }</pre><pre>    return rev == x;<br>}</pre><h3>Complexity</h3><ul><li><strong>Time Complexity:</strong> O(N), where N is the number of digits.</li><li><strong>Space Complexity:</strong> O(1).</li></ul><h3>Alternative Approach: String Conversion</h3><p>Convert the number to string and check with two pointers.</p><ul><li>Easier to implement</li><li>But less efficient in terms of memory</li></ul><h3>Key Takeaway</h3><p>This problem demonstrates how <strong>basic arithmetic operations</strong> can eliminate the need for extra data structures. Always think about whether string conversion is necessary, or if the problem can be solved mathematically.</p><p>💡 Which method do you personally prefer — <strong>string conversion</strong> or <strong>pure math-based reversal</strong>?</p><p><strong>Tags:</strong><br> #100DaysOfCode #DSA #Java #LeetCode #ProblemSolving #InterviewPrep #CodingChallenge</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5564a69c8f3b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 34 of 100 Days of DSA — Microsoft Easy Level: Sign of the Product of an Array]]></title>
            <link>https://medium.com/@singhyuvraj8000/day-34-of-100-days-of-dsa-microsoft-easy-level-sign-of-the-product-of-an-array-7ea8bf7cadd8?source=rss-0445aa0473a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ea8bf7cadd8</guid>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[data-structure-algorithm]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[problem-solving]]></category>
            <dc:creator><![CDATA[Yuvraj singh]]></dc:creator>
            <pubDate>Tue, 26 Aug 2025 13:02:24 GMT</pubDate>
            <atom:updated>2025-08-26T13:02:24.410Z</atom:updated>
            <content:encoded><![CDATA[<h3>Day 34 of 100 Days of DSA — Microsoft Easy Level: Sign of the Product of an Array</h3><p>Today’s problem was a Microsoft <strong>Easy Level</strong> interview question:</p><h3>Problem Statement</h3><p>Given an integer array nums, return the sign of the product of all elements:</p><ul><li>1 if product &gt; 0</li><li>-1 if product &lt; 0</li><li>0 if product = 0</li></ul><h3>Naive Approach</h3><ul><li>Multiply all elements one by one.</li><li>Check final product’s sign.</li></ul><p>❌ Problem: Risk of <strong>integer overflow</strong> with large arrays.</p><h3>Optimized Approach</h3><p>Instead of multiplying:</p><ol><li>Traverse the array.</li><li>If any element is 0 → return 0.</li><li>Count number of negative numbers.</li></ol><ul><li>If odd → return -1.</li><li>If even → return 1.</li></ul><h3>Code (Java)</h3><pre>public int arraySign(int[] nums) {<br>    int count = 0;<br>    int n = nums.length;</pre><pre>    for (int i = 0; i &lt; n; i++) {<br>        if (nums[i] == 0) return 0;<br>        else if (nums[i] &lt; 0) count++;<br>    }</pre><pre>    if (count % 2 == 1) return -1;<br>    return 1;<br>}</pre><h3>Complexity</h3><ul><li><strong>Time Complexity:</strong> O(N) (single pass through array)</li><li><strong>Space Complexity:</strong> O(1) (only counter used)</li></ul><h3>Key Takeaway</h3><p>This problem shows how <strong>analyzing mathematical properties</strong> can avoid unnecessary computation and overflow issues. A simple count of negatives is enough!</p><p>💬 Do you think this can be solved <strong>without even using a counter variable</strong>? (Hint: flip sign while iterating 😉)</p><p><strong>Tags:</strong><br> #100DaysOfCode #DSA #Microsoft #CodingChallenge #Java #Arrays #ProblemSolving #InterviewPrep</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ea8bf7cadd8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 33 of 100 Days of DSA — Longest Valid Parentheses (Stack Approach)]]></title>
            <link>https://medium.com/@singhyuvraj8000/day-33-of-100-days-of-dsa-longest-valid-parentheses-stack-approach-b31ad8917918?source=rss-0445aa0473a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/b31ad8917918</guid>
            <category><![CDATA[data-structure-algorithm]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[interview-preparation]]></category>
            <category><![CDATA[problem-solving]]></category>
            <dc:creator><![CDATA[Yuvraj singh]]></dc:creator>
            <pubDate>Mon, 25 Aug 2025 10:17:36 GMT</pubDate>
            <atom:updated>2025-08-25T10:17:36.248Z</atom:updated>
            <content:encoded><![CDATA[<h3>Day 33 of 100 Days of DSA — Longest Valid Parentheses (Stack Approach)</h3><p>Today I worked on the classic <strong>Longest Valid Parentheses</strong> problem — a must-know interview question for FAANG and top product companies.</p><h3>Problem Statement</h3><p>Given a string consisting of only &#39;(&#39; and &#39;)&#39;, return the length of the <strong>longest valid parentheses substring</strong>.</p><h3>Approach 1: Stack Method</h3><h3>Steps:</h3><ol><li>Initialize a stack and push -1.</li><li>Traverse each character:</li></ol><ul><li>If &#39;(&#39; → push index.</li><li>If &#39;)&#39; → pop from stack.</li><li>If stack is not empty → calculate length (i - stack.peek()) and update answer.</li><li>Else → push current index as new base.</li></ul><h3>Code (Java)</h3><pre>static int findMaxLen(String str) {<br>    int n = str.length();<br>    Stack&lt;Integer&gt; st = new Stack&lt;&gt;();<br>    st.push(-1);<br>    int ans = 0;</pre><pre>    for (int i = 0; i &lt; n; i++) {<br>        if (str.charAt(i) == &#39;(&#39;) {<br>            st.push(i);<br>        } else {<br>            if (!st.empty()) st.pop();<br>            if (!st.empty()) {<br>                ans = Math.max(ans, i - st.peek());<br>            } else {<br>                st.push(i);<br>            }<br>        }<br>    }<br>    return ans;<br>}</pre><h3>Approach 2: Dynamic Programming</h3><ul><li>Maintain a DP array where dp[i] = longest valid substring ending at index i.</li><li>Update based on matching parentheses.</li><li>Achieves same <strong>O(N)</strong> time but uses an array instead of a stack.</li></ul><h3>Complexity Analysis</h3><ul><li><strong>Time:</strong> O(N)</li><li><strong>Space:</strong> O(N) (stack) or O(N) (dp array).</li></ul><h3>Key Takeaways</h3><ul><li>Stack-based solution is intuitive and easy to code.</li><li>DP solution provides another perspective.</li><li>Great problem to test <strong>string traversal + stack usage</strong>.</li></ul><p>💬 Have you solved this problem before? Which method did you find easier — <strong>Stack</strong> or <strong>DP</strong>?</p><p><strong>Tags:</strong><br> #100DaysOfCode #DSA #Java #Stack #DynamicProgramming #Strings #LeetCode #CodingChallenge #InterviewPrep</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b31ad8917918" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 32 of 100 Days of DSA — Race Car (Dynamic Programming)]]></title>
            <link>https://medium.com/@singhyuvraj8000/day-32-of-100-days-of-dsa-race-car-dynamic-programming-da61eb83b28f?source=rss-0445aa0473a9------2</link>
            <guid isPermaLink="false">https://medium.com/p/da61eb83b28f</guid>
            <category><![CDATA[data-structure-algorithm]]></category>
            <category><![CDATA[interview-preparation]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[problem-solving]]></category>
            <dc:creator><![CDATA[Yuvraj singh]]></dc:creator>
            <pubDate>Sun, 24 Aug 2025 12:27:41 GMT</pubDate>
            <atom:updated>2025-08-24T12:27:41.874Z</atom:updated>
            <content:encoded><![CDATA[<h3>Day 32 of 100 Days of DSA — Race Car (Dynamic Programming)</h3><p>Today’s problem is one of the <strong>trickiest dynamic programming challenges</strong> on LeetCode — <strong>Race Car</strong>.</p><h3>Problem Statement</h3><p>We start at position 0 with speed 1. At each step:</p><ul><li><strong>A (Accelerate):</strong> position += speed, speed *= 2</li><li><strong>R (Reverse):</strong> speed = (speed &gt; 0 ? -1 : 1)</li></ul><p>Find the <strong>minimum number of steps</strong> to reach a given target.</p><h3>Naive Approach — BFS/DFS</h3><p>We could try <strong>BFS</strong> by simulating every possible sequence of instructions until reaching the target.</p><ul><li>While correct, it’s <strong>too slow</strong> for large targets.</li></ul><h3>Optimized Approach — Dynamic Programming</h3><h3>Key Observations:</h3><ul><li>After n accelerations, car reaches 2ⁿ - 1.</li><li>If target equals this exact position → done in n moves.</li><li>Otherwise, either:</li></ul><ol><li><strong>Overshoot and reverse</strong> → move back to target.</li><li><strong>Stop earlier and reverse</strong> → then approach target again.</li></ol><p>We use <strong>DP</strong> to compute minimum instructions for all positions ≤ target.</p><h3>Code (Java)</h3><pre>public static int racecar(int target) {<br>    int[] dp = new int[target + 1];<br>    int n = 1;</pre><pre>    for (int i = 1; i &lt;= target; i++) {<br>        if (i == (int)Math.pow(2, n) - 1) {<br>            dp[i] = n;<br>            n++;<br>        } else {<br>            dp[i] = Integer.MAX_VALUE;<br>            for (int j = 0; j &lt; n - 1; j++) {<br>                dp[i] = Math.min(dp[i], n + j + 1 + dp[i - (int)Math.pow(2, n - 1) + (int)Math.pow(2, j)]);<br>            }<br>            dp[i] = Math.min(dp[i], n + 1 + dp[(int)Math.pow(2, n) - 1 - i]);<br>        }<br>    }<br>    return dp[target];<br>}</pre><h3>Complexity Analysis</h3><ul><li><strong>Time:</strong> O(N log N)</li><li><strong>Space:</strong> O(N)</li></ul><h3>Key Takeaways</h3><ul><li>This problem is a great example of <strong>combining DP with mathematical patterns</strong>.</li><li>It highlights how overshooting and reversing can actually minimize moves.</li><li>BFS gives intuition, but DP gives performance.</li></ul><p>💬 If you’ve solved this one before, which approach did you prefer — <strong>BFS</strong> or <strong>DP</strong>?</p><p><strong>Tags:</strong><br> #100DaysOfCode #DSA #DynamicProgramming #Java #RaceCar #LeetCode #CodingChallenge #FAANG</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da61eb83b28f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>