<?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: Dev Nirwal</title>
    <description>The latest articles on DEV Community by Dev Nirwal (@devn913).</description>
    <link>https://dev.to/devn913</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%2F704605%2F501c84b7-f987-4d41-8d17-fbedf06a12be.jpg</url>
      <title>DEV Community: Dev Nirwal</title>
      <link>https://dev.to/devn913</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devn913"/>
    <language>en</language>
    <item>
      <title>Leetcode 2351. First Letter to Appear Twice</title>
      <dc:creator>Dev Nirwal</dc:creator>
      <pubDate>Thu, 27 Feb 2025 17:23:25 +0000</pubDate>
      <link>https://dev.to/devn913/leetcode-2351-first-letter-to-appear-twice-3a7i</link>
      <guid>https://dev.to/devn913/leetcode-2351-first-letter-to-appear-twice-3a7i</guid>
      <description>&lt;p&gt;Problem Link:  &lt;a href="https://leetcode.com/problems/first-letter-to-appear-twice/description/" rel="noopener noreferrer"&gt;2351: Leetcode&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GitHub repo for more solutions: &lt;a href="//github.com/devn913/leetCode"&gt;Git&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Leetcode profile: &lt;a href="https://leetcode.com/u/devn007/" rel="noopener noreferrer"&gt;Leetcode: devn007&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Geeks for geeks profile: &lt;a href="https://www.geeksforgeeks.org/user/devnirwal16/" rel="noopener noreferrer"&gt;GFG: devnirwal16&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Intuition
&lt;/h1&gt;

&lt;p&gt;Approach 1: Using frequency array&lt;/p&gt;

&lt;h1&gt;
  
  
  Approach
&lt;/h1&gt;

&lt;p&gt;Take a boolean array of 26 elements.&lt;/p&gt;

&lt;p&gt;Map each character to index such that a - 0, b - 1,...,z-25.&lt;/p&gt;

&lt;p&gt;Iterate through the string and set each index to true.&lt;/p&gt;

&lt;p&gt;Before setting any index to be true, check if its true already, if yes return that character.&lt;/p&gt;

&lt;p&gt;If we reach at the end of the loop return -1 that means our string only contains unique elements.&lt;/p&gt;

&lt;h1&gt;
  
  
  Complexity
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Time complexity: O(N)&lt;/p&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Space complexity: O(1)&lt;/p&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Approach 1: Using Frequency array
class Solution {
    public char repeatedCharacter(String s) {
        boolean[] freq = new boolean[26];
        for(char c: s.toCharArray()){
            int index = c - 'a';
            if(freq[index] == true) return truec;
            freq[index] = !freq[index];
        }
        return '-1';
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Intuition
&lt;/h1&gt;

&lt;p&gt;Approach 2: Using bit masking&lt;/p&gt;

&lt;h1&gt;
  
  
  Approach
&lt;/h1&gt;

&lt;p&gt;Using a 32-bit number to track 26 alphabet characters.&lt;/p&gt;

&lt;p&gt;We are mapping each bit for each character since a number has 32 bit.&lt;/p&gt;

&lt;p&gt;If any bit is already set to 1 we return that character&lt;/p&gt;

&lt;h1&gt;
  
  
  Complexity
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Time complexity: O(N)&lt;/p&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Space complexity: O(1)&lt;/p&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    // Approach 2: Bit-wise approach.

    public int setKBit(int num, int k) { // Setting the k-bit to 1 in the number num
        return (1 &amp;lt;&amp;lt; k) | num;
    }

    public boolean checkKBit(int num, int k) { // checking if kth bit is already one if yes return true
        return ((1 &amp;lt;&amp;lt; k) &amp;amp; num) != 0;
    }

    public char repeatedCharacter(String s) {
        int value = 0;
        for (char c : s.toCharArray()) {
            int k = c - 'a'; // mapping a - 0, b-1, c-2 ... z-25 index
            if (checkKBit(value, k))
                return c; // checking for k bit in the number if set to 1 or not
            value = setKBit(value, k); // setting kth bit to 1 for any character
        }
        return '-1';
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>dsa</category>
      <category>programming</category>
      <category>competativeprogramming</category>
    </item>
    <item>
      <title>Maximum Absolute Sum of Any Subarray LC - 1749</title>
      <dc:creator>Dev Nirwal</dc:creator>
      <pubDate>Wed, 26 Feb 2025 06:12:30 +0000</pubDate>
      <link>https://dev.to/devn913/maximum-absolute-sum-of-any-subarray-lc-1749-54o0</link>
      <guid>https://dev.to/devn913/maximum-absolute-sum-of-any-subarray-lc-1749-54o0</guid>
      <description>&lt;p&gt;Question link on leetcode: &lt;a href="https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/description/" rel="noopener noreferrer"&gt;Leetcode&lt;/a&gt; &lt;/p&gt;

&lt;h1&gt;
  
  
  Intuition
&lt;/h1&gt;

&lt;p&gt;Maximum subarray sum: &lt;a href="https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/" rel="noopener noreferrer"&gt;Kadane's algorithm&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Approach
&lt;/h1&gt;

&lt;p&gt;Take two sums, Max subarray sum and Min subarray sum.&lt;br&gt;
Use Kadane's algorithm to find both.&lt;br&gt;
In the last, compare the max sum and min absolute sum and return the max between them.&lt;/p&gt;

&lt;h1&gt;
  
  
  Complexity
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Time complexity: O(N)&lt;/p&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Space complexity: O(1)&lt;/p&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int maxAbsoluteSum(int[] nums) {
        int maxSum = Integer.MIN_VALUE;
        int minSum = Integer.MAX_VALUE;
        int currMax = 0;
        int currMin = 0;
        for(int num: nums){
            currMax +=num;
            currMin +=num;
            maxSum = Math.max(maxSum,currMax);
            minSum = Math.min(minSum,currMin);
            if(currMax&amp;lt;0) currMax = 0;
            if(currMin&amp;gt;0) currMin = 0;
        }
        return Math.max(maxSum,Math.abs(minSum));
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GitHub repo for more solutions: &lt;a href="//github.com/devn913/leetCode"&gt;Git&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Leetcode profile: &lt;a href="https://leetcode.com/u/devn007/" rel="noopener noreferrer"&gt;Leetcode: devn007&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Geeks for geeks profile: &lt;a href="https://www.geeksforgeeks.org/user/devnirwal16/" rel="noopener noreferrer"&gt;GFG: devnirwal16&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>java</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Reorder List: LC 143 medium, GFG hard</title>
      <dc:creator>Dev Nirwal</dc:creator>
      <pubDate>Mon, 27 Jan 2025 14:52:30 +0000</pubDate>
      <link>https://dev.to/devn913/reorder-list-lc-143-medium-gfg-hard-27e9</link>
      <guid>https://dev.to/devn913/reorder-list-lc-143-medium-gfg-hard-27e9</guid>
      <description>&lt;p&gt;Problem Link: &lt;a href="https://leetcode.com/problems/reorder-list/description/" rel="noopener noreferrer"&gt;Leetcode&lt;/a&gt;, &lt;a href="https://www.geeksforgeeks.org/problems/reorder-list/1" rel="noopener noreferrer"&gt;Geeks for geeks&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Intuition
&lt;/h1&gt;

&lt;p&gt;We need to divide two pointers one at start and one at bottom&lt;/p&gt;

&lt;h1&gt;
  
  
  Approach
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; first find the mid point of the linked list using slow-fast pointer approach.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Divide the linked list into two two by two pointers &lt;code&gt;firstHalf&lt;/code&gt; and &lt;code&gt;secondHalf&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Use &lt;code&gt;reverse()&lt;/code&gt; to reverse the second half of the list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; For the final step combined the reversed second half and first half to get the final result.&lt;/p&gt;

&lt;h1&gt;
  
  
  Complexity
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Time complexity: O(N)&lt;/p&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Space complexity: O(1)&lt;/p&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverse(ListNode head){
        ListNode prev = null;
        ListNode curr = head;
        ListNode next = head.next;
        while(next!=null){
            curr.next = prev;
            prev = curr;
            curr = next;
            next = next.next;
        }
        curr.next = prev;
        return curr;
    }
    public void reorderList(ListNode head) {
        if(head == null || head.next == null ) return;
        // use turtle rabbit method, slow-fast pointer to find middle of the linkedlist 
        ListNode slow = head;
        ListNode fast = head.next;
        while(fast!=null &amp;amp;&amp;amp; fast.next!=null){
            slow = slow.next; // move once
            fast = fast.next.next; // move twice faster 
        }
        // Divide the linkedList into two parts;
        ListNode secondHalf = slow.next;
        // Set first half next to null to deattach the flist 
        slow.next = null;
        // reverse the second half
        secondHalf = reverse(secondHalf);
        ListNode firstHalf  = head;
        ListNode temp = secondHalf;
        // combinig both list
        while(secondHalf!=null){
            temp = temp.next;
            secondHalf.next = firstHalf.next;
            firstHalf.next  = secondHalf;
            firstHalf = secondHalf.next;
            secondHalf = temp;
        }
        return;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GitHub repo for more solutions: &lt;a href="//github.com/devn913/leetCode"&gt;Git&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Leetcode profile: &lt;a href="https://leetcode.com/u/devn007/" rel="noopener noreferrer"&gt;Leetcode: devn007&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Geeks for geeks profile: &lt;a href="https://www.geeksforgeeks.org/user/devnirwal16/" rel="noopener noreferrer"&gt;GFG: devnirwal16&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>leetcode</category>
      <category>java</category>
      <category>competativeprogramming</category>
    </item>
    <item>
      <title>Leetcode 901. Online Stock Span</title>
      <dc:creator>Dev Nirwal</dc:creator>
      <pubDate>Sun, 19 Jan 2025 07:22:26 +0000</pubDate>
      <link>https://dev.to/devn913/leetcode-901-online-stock-span-p4m</link>
      <guid>https://dev.to/devn913/leetcode-901-online-stock-span-p4m</guid>
      <description>&lt;h1&gt;
  
  
  Intuition
&lt;/h1&gt;

&lt;p&gt;Could you use the answer of previous spans?&lt;/p&gt;

&lt;h1&gt;
  
  
  Approach
&lt;/h1&gt;

&lt;p&gt;Save the price and its span in the array.&lt;/p&gt;

&lt;p&gt;Whenever the last value is less than the current day value, jump to the day of the last day span.&lt;/p&gt;

&lt;h1&gt;
  
  
  Complexity
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Time complexity: O(n)&lt;/p&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Space complexity: O(n)&lt;/p&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class StockSpanner {
    ArrayList&amp;lt;Pair&amp;lt;Integer,Integer&amp;gt;&amp;gt; list;

    public StockSpanner() {
        list = new ArrayList&amp;lt;&amp;gt;();
    }

    public int next(int price) {
        int index = list.size() - 1;
        int ans = 1;
        while(index!=-1){
            if(list.get(index).getKey()&amp;gt;price) break;
            int span = list.get(index).getValue();
            ans+=span;
            index-=span;
        }
        list.add(new Pair(price,ans));
        return ans;
    }
}


/**
 * Your StockSpanner object will be instantiated and called as such:
 * StockSpanner obj = new StockSpanner();
 * int param_1 = obj.next(price);
 */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GitHub repo for more solutions: &lt;a href="//github.com/devn913/leetCode"&gt;Git&lt;/a&gt;&lt;br&gt;
Leetcode profile: &lt;a href="https://leetcode.com/u/devn007/" rel="noopener noreferrer"&gt;Leetcode: devn007&lt;/a&gt;&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>java</category>
      <category>design</category>
      <category>dsa</category>
    </item>
    <item>
      <title>Leetcode 75. Sort Colors</title>
      <dc:creator>Dev Nirwal</dc:creator>
      <pubDate>Sat, 11 Jan 2025 08:07:57 +0000</pubDate>
      <link>https://dev.to/devn913/leetcode-75-sort-colors-3imp</link>
      <guid>https://dev.to/devn913/leetcode-75-sort-colors-3imp</guid>
      <description>&lt;h1&gt;
  
  
  Intuition
&lt;/h1&gt;

&lt;p&gt;The basic intuition comes from sorting.&lt;/p&gt;

&lt;h1&gt;
  
  
  Approach
&lt;/h1&gt;

&lt;p&gt;In the naive approach, we can sort the array using inbuilt sorting function. The time complexity will be &lt;code&gt;O(N*log(N))&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Optimize&lt;/strong&gt;: 
Since we are sorting only three numbers, we can use the concept of counting sort. Keep track of number of zeros and number of ones in the array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Complexity
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Time complexity: &lt;code&gt;O(N)&lt;/code&gt;&lt;/p&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Space complexity: &lt;code&gt;O(1)&lt;/code&gt; &lt;/p&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public void sortColors(int[] nums) {
        int countZero = 0;
        int countOne  =  0;
        for(int num: nums){
            switch(num){
                case 0:
                    countZero++;
                    break;
                case 1:
                    countOne++;
            }
        }
        int currentIndex = -1;
        while(0&amp;lt;countZero--){
            nums[++currentIndex] = 0;
            // countZero--;
        }
        while(0&amp;lt;countOne--){
            nums[++currentIndex] = 1;
            // countOne--;
        }
        while(currentIndex&amp;lt;nums.length-1){
            nums[++currentIndex] = 2;
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GitHub repo for more solutions: &lt;a href="//github.com/devn913/leetCode"&gt;Git&lt;/a&gt;&lt;br&gt;
Leetcode profile: &lt;a href="https://leetcode.com/u/devn007/" rel="noopener noreferrer"&gt;Leetcode: devn007&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>sorting</category>
      <category>algorithms</category>
      <category>leetcode</category>
    </item>
    <item>
      <title>Kadane's Algorithm: Leetcode 53 Maximum subarray</title>
      <dc:creator>Dev Nirwal</dc:creator>
      <pubDate>Fri, 10 Jan 2025 18:05:39 +0000</pubDate>
      <link>https://dev.to/devn913/kadanes-algorithm-leetcode-53-maximum-subarray-17c8</link>
      <guid>https://dev.to/devn913/kadanes-algorithm-leetcode-53-maximum-subarray-17c8</guid>
      <description>&lt;h1&gt;
  
  
  Intuition
&lt;/h1&gt;

&lt;p&gt;We can build the intuition based on the two-point approach.&lt;/p&gt;

&lt;h1&gt;
  
  
  Approach
&lt;/h1&gt;

&lt;p&gt;We will start with two variables &lt;code&gt;maxSum&lt;/code&gt; and &lt;code&gt;maxTillNow&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The first variable stores the max sum we have attained overall in the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second variable stores the value of the maximum sum attained till the current index. Since the array has a negative value, this value will fluctuate, but whenever we get &lt;code&gt;maxSum &amp;lt; maxTillNow&lt;/code&gt;, we update the &lt;code&gt;maxSum&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The final case we have to handle will be if the maximum sum till any index reaches zero, i.e., &lt;code&gt;maxTillNow &amp;lt; 0&lt;/code&gt;, reset the &lt;code&gt;maxTillNow = 0&lt;/code&gt; at the end of loop.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Complexity
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Time complexity: O(N)&lt;/p&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Space complexity: O(1)&lt;/p&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int maxSubArray(int[] nums) {
        int maxSum = Integer.MIN_VALUE;
        int maxTillNow = 0;
        for(int i =0;i&amp;lt;nums.length;i++){
            maxTillNow+=nums[i];
            maxSum = Math.max(maxTillNow,maxSum);
            if(maxTillNow&amp;lt;0) maxTillNow = 0;
        }
        return maxSum;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GitHub repo for more solutions: &lt;a href="//github.com/devn913/leetCode"&gt;Git&lt;/a&gt;&lt;br&gt;
Leetcode profile: &lt;a href="https://leetcode.com/u/devn007/" rel="noopener noreferrer"&gt;Leetcode: devn007&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>algorithms</category>
      <category>leetcode</category>
      <category>datastructures</category>
    </item>
    <item>
      <title>Leetcode: 73 Set Matrix Zeroes</title>
      <dc:creator>Dev Nirwal</dc:creator>
      <pubDate>Fri, 10 Jan 2025 08:24:26 +0000</pubDate>
      <link>https://dev.to/devn913/leetcode-73-set-matrix-zeroes-dfj</link>
      <guid>https://dev.to/devn913/leetcode-73-set-matrix-zeroes-dfj</guid>
      <description>&lt;h1&gt;
  
  
  Intuition
&lt;/h1&gt;

&lt;p&gt;In a naive solution, we can keep track of all the rows and columns to be made zero.&lt;/p&gt;

&lt;h1&gt;
  
  
  Approach
&lt;/h1&gt;

&lt;p&gt;In the naive solution, we can use &lt;strong&gt;hash sets or sets&lt;/strong&gt; to keep track of rows and columns to make zero using one iteration. In the second phase, we can iterate through our sets to set the saved rows and columns to zero.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Space complexity for Naive Solution: O(M+N)&lt;/strong&gt;&lt;br&gt;
Naive solution code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public void setRowZero(int[][] matrix, int row){
        for(int i = 0;i&amp;lt;matrix[0].length;i++){
            matrix[row][i] = 0;
        }
   }
    public void setColZero(int[][] matrix, int col){
        for(int i = 0;i&amp;lt;matrix.length;i++){
            matrix[i][col] = 0;
        }
    }
    public void setZeroes(int[][] matrix) {
        Set&amp;lt;Integer&amp;gt; rowSet = new HashSet&amp;lt;&amp;gt;();
        Set&amp;lt;Integer&amp;gt; colSet = new HashSet&amp;lt;&amp;gt;();
        for(int i =0;i&amp;lt;matrix.length;i++){
            for(int j = 0;j&amp;lt;matrix[i].length;j++){
                if(matrix[i][j] == 0){
                    rowSet.add(i);
                    colSet.add(j);
                }
            }
        }
        for(int ele: rowSet){
            setRowZero(matrix,ele);
        }
        for(int ele: colSet){
            setColZero(matrix,ele);
        }     
    }
}



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

&lt;/div&gt;



&lt;p&gt;In the below solution which is optimized, I have marked the first row and first column if we encounter any zero in the submatrix &lt;/p&gt;

&lt;h1&gt;
  
  
  [  (1, rows): (1, cols) ]
&lt;/h1&gt;

&lt;p&gt;We can use two boolean variables to keep track of the first row and first column for any zero.&lt;/p&gt;

&lt;h1&gt;
  
  
  Complexity
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Time complexity: O(M * N)&lt;/p&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Space complexity: O(1)&lt;/p&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public void setZeroes(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;

        boolean first_row_has_zero = false;
        boolean first_col_has_zero = false;

        // check if first row has any zero
        for(int c = 0;c&amp;lt;cols;c++){
            if(matrix[0][c] == 0){
                first_row_has_zero = true;
                break;
            }
        }

        // check if first col has any zero
        for(int r = 0;r&amp;lt;rows;r++){
            if(matrix[r][0] == 0){
                first_col_has_zero = true;
                break;
            }
        }
        // checking for zero elsewhere and keeping track of it in the first row and first column
        for(int r = 1;r&amp;lt;rows;r++){
            for(int c = 1;c&amp;lt;cols;c++){
                if(matrix[r][c] == 0){
                    matrix[r][0] = 0;
                    matrix[0][c] = 0;
                }
            }
        }

        // checking and setting respcted row to zero
        for(int c = 1;c&amp;lt;cols; c++){
            if(matrix[0][c] == 0){
                for(int r = 0;r&amp;lt;rows;r++){
                    matrix[r][c] = 0;
                }
            }
        }

        // checking and setting respcted cols to zero
        for(int r = 1;r&amp;lt;rows; r++){
            if(matrix[r][0] == 0){
                for(int c = 0;c&amp;lt;cols;c++){
                    matrix[r][c] = 0;
                }
            }
        }

        if(first_row_has_zero == true){
            for(int c = 0;c&amp;lt;cols;c++){
                matrix[0][c] = 0;
            }
        }

        if(first_col_has_zero == true){
            for(int r = 0;r&amp;lt;rows;r++){
                matrix[r][0] = 0;
            }
        }

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

&lt;/div&gt;



&lt;p&gt;GitHub repo for more solutions: &lt;a href="//github.com/devn913/leetCode"&gt;Git&lt;/a&gt;&lt;br&gt;
Leetcode profile: &lt;a href="https://leetcode.com/u/devn007/" rel="noopener noreferrer"&gt;Leetcode: devn007&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>programming</category>
      <category>leetcode</category>
    </item>
    <item>
      <title>Leetcode Solution - Github automation</title>
      <dc:creator>Dev Nirwal</dc:creator>
      <pubDate>Sat, 08 Jan 2022 06:35:45 +0000</pubDate>
      <link>https://dev.to/devn913/leetcode-solution-github-automation-6o8</link>
      <guid>https://dev.to/devn913/leetcode-solution-github-automation-6o8</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqg3vtc8skxl9yrxbnnda.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqg3vtc8skxl9yrxbnnda.png" alt="Image description" width="343" height="166"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hello everyone, I have found a awesome tool which automatically sync your leetcode solution to your github which I am personally using from many months.&lt;/p&gt;

&lt;p&gt;Cons - Only available for chromium based browser.&lt;/p&gt;

&lt;p&gt;Extension link - &lt;a href="https://chrome.google.com/webstore/detail/leethub/aciombdipochlnkbpcbgdpjffcfdbggi" rel="noopener noreferrer"&gt;https://chrome.google.com/webstore/detail/leethub/aciombdipochlnkbpcbgdpjffcfdbggi&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My leetcode profile - &lt;a href="https://leetcode.com/devn007" rel="noopener noreferrer"&gt;https://leetcode.com/devn007&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My github repo to leetcode solutions (java) -  &lt;a href="https://github.com/devn913/leetcode" rel="noopener noreferrer"&gt;https://github.com/devn913/leetcode&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My github profile - &lt;a href="https://github.com/devn913" rel="noopener noreferrer"&gt;https://github.com/devn913&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc0sbn80fm3y5af3eseu4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc0sbn80fm3y5af3eseu4.png" alt="Image description" width="800" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>github</category>
      <category>java</category>
      <category>programming</category>
      <category>leetcode</category>
    </item>
    <item>
      <title>SMS Bomber App - Flutter</title>
      <dc:creator>Dev Nirwal</dc:creator>
      <pubDate>Sun, 21 Nov 2021 12:26:09 +0000</pubDate>
      <link>https://dev.to/devn913/sms-bomber-app-flutter-2lp7</link>
      <guid>https://dev.to/devn913/sms-bomber-app-flutter-2lp7</guid>
      <description>&lt;p&gt;Note:- This app is only for educational purpose only. Please don't abuse the app or the API. Use it for fun not for revenge.&lt;/p&gt;

&lt;p&gt;About this app:-  This is one of my small project to prank your friends which send bulk sms in a short period of time. Based on Flutter and Dart and for back-end I have used &lt;a href="https://github.com/devn913/bomber_api" rel="noopener noreferrer"&gt;https://github.com/devn913/bomber_api&lt;/a&gt; which is hosted on free Heroku tier.&lt;/p&gt;

&lt;p&gt;I am not a Flutter developer so if you have any ideas how to make this app better, I am open to contribution make sure to Fork it star it and follow me if you liked this project.&lt;/p&gt;

&lt;p&gt;If you want me to add more API contact me by mail - &lt;a href="mailto:devnirwal16@gmail.com"&gt;devnirwal16@gmail.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to add more API yourself use this repo - &lt;a href="https://github.com/devn913/bomber_api" rel="noopener noreferrer"&gt;https://github.com/devn913/bomber_api&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Project GitHub Link - &lt;a href="https://github.com/Devn913/bomber_app" rel="noopener noreferrer"&gt;https://github.com/Devn913/bomber_app&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;APK link  - &lt;a href="https://github.com/Devn913/bomber_app/blob/main/app-release.apk" rel="noopener noreferrer"&gt;https://github.com/Devn913/bomber_app/blob/main/app-release.apk&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My GitHub Profile &lt;a href="https://github.com/Devn913" rel="noopener noreferrer"&gt;https://github.com/Devn913&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Virus Total - &lt;a href="https://www.virustotal.com/gui/file/5f1c9a5eddddd14da717d0681d892665e946517f0b18c5a5ac7e6e2da4d6ffc1?nocache=1" rel="noopener noreferrer"&gt;https://www.virustotal.com/gui/file/5f1c9a5eddddd14da717d0681d892665e946517f0b18c5a5ac7e6e2da4d6ffc1?nocache=1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Star it, Follow me if you liked this project.&lt;/p&gt;

&lt;p&gt;Contact me if you are facing any issues.&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>python</category>
      <category>android</category>
    </item>
    <item>
      <title>Hacktoberfest - 2021</title>
      <dc:creator>Dev Nirwal</dc:creator>
      <pubDate>Mon, 25 Oct 2021 07:03:03 +0000</pubDate>
      <link>https://dev.to/devn913/hacktoberfest-2021-25lo</link>
      <guid>https://dev.to/devn913/hacktoberfest-2021-25lo</guid>
      <description>&lt;p&gt;I am very happy to share this was my first open-source contribution. I'm so happy to announce that. If you haven't submitted yet you should it really feels good. :D&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F63w7i05a9rrzvl253et5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F63w7i05a9rrzvl253et5.png" alt="Image description" width="800" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My github Profile - &lt;a href="https://github.com/devn913" rel="noopener noreferrer"&gt;https://github.com/devn913&lt;/a&gt;&lt;/p&gt;

</description>
      <category>hacktoberfest</category>
      <category>github</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Quoted Image App</title>
      <dc:creator>Dev Nirwal</dc:creator>
      <pubDate>Sun, 03 Oct 2021 20:30:27 +0000</pubDate>
      <link>https://dev.to/devn913/quoted-image-app-2fa7</link>
      <guid>https://dev.to/devn913/quoted-image-app-2fa7</guid>
      <description>&lt;p&gt;Well one more open-source mini-project by me.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fstecwz7bffqs190wp9nv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fstecwz7bffqs190wp9nv.png" alt="image" width="800" height="343"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What does it do?&lt;br&gt;
-&amp;gt; In very simple word, it takes a random image from Unsplash API, fetch a quote from quote API and combine them and generate a new Quote Image (like motivational Image)&lt;/p&gt;

&lt;p&gt;App link - &lt;a href="https://quotedimage.herokuapp.com/" rel="noopener noreferrer"&gt;https://quotedimage.herokuapp.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Source Code - &lt;a href="https://github.com/devn913/quoted_image_app" rel="noopener noreferrer"&gt;https://github.com/devn913/quoted_image_app&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Technology Used - Python-Django, Python-pillow, Unsplash-Image API, Quote API, IMGBB API, Tailwind CSS.&lt;/p&gt;

&lt;p&gt;Check out source code for more info.&lt;/p&gt;

&lt;p&gt;I'm open for suggestions &amp;amp; help.&lt;/p&gt;

&lt;p&gt;Star repo if you liked my work.&lt;/p&gt;

&lt;p&gt;Keep coding.&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
      <category>heroku</category>
      <category>tailwindcss</category>
    </item>
    <item>
      <title>Shorti - Opensource URL Shortener</title>
      <dc:creator>Dev Nirwal</dc:creator>
      <pubDate>Thu, 23 Sep 2021 09:04:04 +0000</pubDate>
      <link>https://dev.to/devn913/shorti-opensource-url-shortener-405h</link>
      <guid>https://dev.to/devn913/shorti-opensource-url-shortener-405h</guid>
      <description>&lt;p&gt;Shorti  - A free &amp;amp; Open-source URL shortener.&lt;/p&gt;

&lt;p&gt;Here's a screeshot of UI:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3zrsswc9asmc660t11ar.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3zrsswc9asmc660t11ar.png" alt="Alt Text" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is my first Django project. I got the idea of from many URL shortener which shows ads for money and bitly. I wanted to creare something like that which doesn't have any ads &amp;amp; anyone can understand how things work behind the back.&lt;/p&gt;

&lt;p&gt;I have used Python-Django framerwork &amp;amp; tailwind CSS in this small project.&lt;/p&gt;

&lt;p&gt;Github link - &lt;a href="https://github.com/devn913/shorti" rel="noopener noreferrer"&gt;https://github.com/devn913/shorti&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Heroku link  -  &lt;a href="https://shortii.herokuapp.com/" rel="noopener noreferrer"&gt;https://shortii.herokuapp.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to deploy your own just fork the repository and don't forget to change value of domain = "localhost:8000" in Shorti/url_short/views.py &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fho1aw1psmx4vk7lu7k2t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fho1aw1psmx4vk7lu7k2t.png" alt="Alt Text" width="759" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Make sure to star this if you found something interesting.&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
      <category>programming</category>
      <category>tailwindcss</category>
    </item>
  </channel>
</rss>
