<?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>Forem: Sumaiya Meem</title>
    <description>The latest articles on Forem by Sumaiya Meem (@stmeem).</description>
    <link>https://forem.com/stmeem</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%2F977797%2F2ffd9b9f-0547-4a4a-8ae5-1e3a02a59ecc.jpg</url>
      <title>Forem: Sumaiya Meem</title>
      <link>https://forem.com/stmeem</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/stmeem"/>
    <language>en</language>
    <item>
      <title>Leetcode problem#876: Middle of the Linked List</title>
      <dc:creator>Sumaiya Meem</dc:creator>
      <pubDate>Mon, 28 Nov 2022 08:29:45 +0000</pubDate>
      <link>https://forem.com/stmeem/leetcode-problem876-middle-of-the-linked-list-34dp</link>
      <guid>https://forem.com/stmeem/leetcode-problem876-middle-of-the-linked-list-34dp</guid>
      <description>&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;Given the head of a singly linked list, return the middle node of the linked list.&lt;/p&gt;

&lt;p&gt;If there are two middle nodes, return the second middle node.&lt;br&gt;
Example 1:&lt;br&gt;
&lt;code&gt;Input: head = [1,2,3,4,5]&lt;br&gt;
Output: [3,4,5]&lt;br&gt;
Explanation: The middle node of the list is node 3.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example 2:&lt;br&gt;
&lt;code&gt;Input: head = [1,2,3,4,5,6]&lt;br&gt;
Output: [4,5,6]&lt;br&gt;
Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.&lt;/code&gt;&lt;/p&gt;




&lt;p&gt;We solve this problem by using two pointers. We move the slow pointer by one step and the fast pointer by two steps. When the fast pointer is at the end of the Linked List after traversing, the slow pointer will be in the middle of the list.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Initially both pointers point to the first node (Head) of the Linked List.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B9unQsEn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1wyadbvm744ofc32l8g.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B9unQsEn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1wyadbvm744ofc32l8g.PNG" alt="Example-demo" width="556" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Move slow pointer by one step and fast pointer by two steps.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XS0-tUSZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wm8fw31lcc80f6l5glic.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XS0-tUSZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wm8fw31lcc80f6l5glic.PNG" alt="Example-demo" width="557" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Repeat the previous step.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ckWyWU_o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kvgxp3dng1w9ku74z13h.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ckWyWU_o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kvgxp3dng1w9ku74z13h.PNG" alt="example-demo" width="454" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are odd numbers of nodes in the first example. In this case, the fast pointer can't move when the fast pointer.next = null. So, we return the slow pointer, which is the middle element of the Linked List.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FfxzaDtv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dovz3oiot43owhax9mc0.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FfxzaDtv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dovz3oiot43owhax9mc0.PNG" alt="example-demo" width="474" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And in the second example, there are even numbers of nodes. When the fastPointer = null, it returns the slow pointer, which is the second middle element of the Linked List.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation (Java)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode fastPointer = head;
        ListNode slowPointer = head;

        while(fastPointer!= null &amp;amp;&amp;amp; fastPointer.next!= null){
            slowPointer = slowPointer.next;
            fastPointer = fastPointer.next.next;
        }
        return slowPointer;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Complexity Analysis
&lt;/h3&gt;

&lt;p&gt;Time complexity : O(n)&lt;br&gt;
The list has been iterated n times where n is the length of the Linked List.&lt;/p&gt;

&lt;p&gt;Space complexity: O(1)&lt;br&gt;
No extra space has been used here.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Leetcode problem#217: Contains Duplicate</title>
      <dc:creator>Sumaiya Meem</dc:creator>
      <pubDate>Fri, 25 Nov 2022 19:17:37 +0000</pubDate>
      <link>https://forem.com/stmeem/leetcode-problem217-contains-duplicate-3aaf</link>
      <guid>https://forem.com/stmeem/leetcode-problem217-contains-duplicate-3aaf</guid>
      <description>&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.&lt;/p&gt;

&lt;p&gt;Example: &lt;br&gt;
&lt;code&gt;Input: nums = [1,2,3,1]&lt;br&gt;
Output: true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Input: nums = [1,2,3,4]&lt;br&gt;
Output: false&lt;/code&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Sorting
&lt;/h3&gt;

&lt;p&gt;We can solve this problem by sorting the array. Time complexity for sorting is O(nlogn). After sorting, we loop through the array and match the consecutive numbers to find the duplicate values. &lt;/p&gt;

&lt;h4&gt;
  
  
  Implementation (Java)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public boolean containsDuplicate(int[] nums) {
        int pointer = 0;
        Arrays.sort(nums);

        while(pointer &amp;lt; nums.length-1){
            if(nums[pointer] == nums[pointer+1]){
                return true;
            }
            pointer++;
        }
       return false;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, this approach has a time complexity of O(nlogn). So we can follow a better way by using HashSet to optimize the time complexity. &lt;/p&gt;

&lt;h3&gt;
  
  
  HashSet
&lt;/h3&gt;

&lt;p&gt;Java HashSet class implements the Set interface. It stores the elements in the hashtable and only holds the distinct values. For checking duplicate numbers, we can use contains() method. &lt;/p&gt;

&lt;h4&gt;
  
  
  Implementation (Java)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public boolean containsDuplicate(int[] nums) {

        int pointer = 0;

        Set&amp;lt;Integer&amp;gt;checkNum = new HashSet&amp;lt;&amp;gt;();

        while(pointer &amp;lt; nums.length){
            if(checkNum.contains(nums[pointer])){
                return true;
            }
            checkNum.add(nums[pointer]);
            pointer++;
        }
        return false;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we get the time complexity of O(n).&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Leetcode Problem#283: Move Zeros</title>
      <dc:creator>Sumaiya Meem</dc:creator>
      <pubDate>Wed, 23 Nov 2022 20:45:32 +0000</pubDate>
      <link>https://forem.com/stmeem/leetcode-problem283-move-zeros-57fn</link>
      <guid>https://forem.com/stmeem/leetcode-problem283-move-zeros-57fn</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;&lt;br&gt;
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.&lt;/p&gt;

&lt;p&gt;Note that you must do this in-place without making a copy of the array.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;code&gt;Input: nums = [0,1,0,3,12]&lt;br&gt;
Output: [1,3,12,0,0]&lt;/code&gt;&lt;/p&gt;



&lt;p&gt;There are many ways to solve this problem. Here I am going to discuss about the optimal solution to tackle the challenge.&lt;/p&gt;

&lt;p&gt;Using two pointers approach, we can solve this problem. Pointer One will look for non-zero elements and Pointer Two will swap the elements with Pointer One. We initialize both pointers to 0. Eventually all zero elements will be moved to the end.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation (Java)&lt;/strong&gt;&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 moveZeroes(int[] nums) {

        int pointer=0;
        int n= nums.length;

        for(int i = 0; i &amp;lt; n; i++){
            if(nums[i]!= 0){
                int temp = nums[i];
                nums[i] = nums[pointer];
                nums[pointer] = temp;
                pointer++;
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Implementation (PHP)&lt;/strong&gt;&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 {

    /**
     * @param Integer[] $nums
     * @return NULL
     */
    function moveZeroes(&amp;amp;$nums) {
        $pointer = 0;
        $numsLength = count($nums);

        for($i=0; $i&amp;lt;$numsLength; $i++){
            if($nums[$i]!=0){
                $temp = $nums[$i];
                $nums[$i]= $nums[$pointer];
                $nums[$pointer] = $temp;
                $pointer++;
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Solution Analysis&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Time complexity = O(n)&lt;br&gt;
Space complexity = O(1)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Why are we not using Brute Force approach?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Through this approach, we traverse the whole array to find non-zero elements and store them in the starting indexes. And then we fill up the extra spaces with zero. Therefore, we get time complexity in the worst case.&lt;/p&gt;

&lt;p&gt;Time Complexity = Time Complexity of filling non-zero elements + Time Complexity of filling zero elements = O(n)+O(n)= O(n)&lt;/p&gt;

&lt;p&gt;Space Complexity = O(n) for extra space&lt;/p&gt;

</description>
      <category>html</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
