<?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: Abhi Agarwal</title>
    <description>The latest articles on DEV Community by Abhi Agarwal (@abhiagarwal192).</description>
    <link>https://dev.to/abhiagarwal192</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%2F456063%2F515426de-c091-409a-98b7-5ebff63bffa2.jpeg</url>
      <title>DEV Community: Abhi Agarwal</title>
      <link>https://dev.to/abhiagarwal192</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhiagarwal192"/>
    <language>en</language>
    <item>
      <title>Longest Common Prefix</title>
      <dc:creator>Abhi Agarwal</dc:creator>
      <pubDate>Sat, 22 Aug 2020 04:50:26 +0000</pubDate>
      <link>https://dev.to/abhiagarwal192/longest-common-prefix-3a00</link>
      <guid>https://dev.to/abhiagarwal192/longest-common-prefix-3a00</guid>
      <description>&lt;p&gt;Write a function to find the longest common prefix string amongst an array of strings.&lt;/p&gt;

&lt;p&gt;If there is no common prefix, return an empty string "".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Input: ["flower","flow","flight"]&lt;br&gt;
Output: "fl"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Input: ["dog","racecar","car"]&lt;br&gt;
Output: ""&lt;br&gt;
Explanation: There is no common prefix among the input strings.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;blockquote&gt;
&lt;p&gt;All given inputs are in lowercase letters a-z.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;The requirement here is to find longest common prefix amongst an array of strings. &lt;/p&gt;

&lt;p&gt;There are many approaches to this problem but the one that I am going to discuss here uses &lt;strong&gt;Trie&lt;/strong&gt; to store the array of strings and then scan the trie to get the longest common prefix.&lt;/p&gt;

&lt;p&gt;To know more about Trie, you can visit &lt;a href="https://www.geeksforgeeks.org/trie-insert-and-search/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Algorithm:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1) Store each string from the array of strings in &lt;strong&gt;Trie&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;2) Get the deepest Trie Node such that all the nodes from the root of the trie to that node has only one children.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://chashsolution.blogspot.com/2020/08/longest-common-prefix.html"&gt;Link to Code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;: O(n) where n is the number of unique characters in the array of string.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: O(n) for building the Trie (n is the number of unique characters in the array of string) and O(m) for finding the prefix (where m is the length of the prefix).&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Remove Duplicates From Sorted Array</title>
      <dc:creator>Abhi Agarwal</dc:creator>
      <pubDate>Fri, 21 Aug 2020 03:59:11 +0000</pubDate>
      <link>https://dev.to/abhiagarwal192/remove-duplicates-from-sorted-array-417l</link>
      <guid>https://dev.to/abhiagarwal192/remove-duplicates-from-sorted-array-417l</guid>
      <description>&lt;p&gt;Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.&lt;/p&gt;

&lt;p&gt;Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example 1:&lt;/p&gt;

&lt;p&gt;Given nums = [1,1,2],&lt;/p&gt;

&lt;p&gt;Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.&lt;/p&gt;

&lt;p&gt;It doesn't matter what you leave beyond the returned length.&lt;br&gt;
Example 2:&lt;/p&gt;

&lt;p&gt;Given nums = [0,0,1,1,1,2,2,3,3,4],&lt;/p&gt;

&lt;p&gt;Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.&lt;/p&gt;

&lt;p&gt;It doesn't matter what values are set beyond the returned length.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Clarification:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Confused why the returned value is an integer but your answer is an array?&lt;/p&gt;

&lt;p&gt;Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Internally you can think of this:&lt;/p&gt;

&lt;p&gt;// nums is passed in by reference. (i.e., without making a copy)&lt;br&gt;
int len = removeDuplicates(nums);&lt;/p&gt;

&lt;p&gt;// any modification to nums in your function would be known by &amp;gt;the caller.&lt;br&gt;
// using the length returned by your function, it prints the &amp;gt;first len elements.&lt;br&gt;
for (int i = 0; i &amp;lt; len; i++) {&lt;br&gt;
   print(nums[i]);&lt;br&gt;
}&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;The requirement here is to remove all the duplicates from the array such that each element occurs only once. &lt;/p&gt;

&lt;p&gt;To solve this problem, we can use two-pointer approach here.  We will have a fast pointer called 'i' and a slow pointer called 'j'. We will compare each element with the element a location 'j'. If we encounter an element which is greater than the element at 'j' then we will add that element to position 'j+1' and update 'j'.&lt;/p&gt;

&lt;p&gt;Github Link &lt;a href="https://gist.github.com/AbhiAgarwal192/78e9ab59d4f4bb099b6b510b2ff81bfb"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Blog Link &lt;a href="https://chashsolution.blogspot.com/2020/08/remove-duplicates-easy.html"&gt;Link&lt;/a&gt;&lt;/p&gt;

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

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

</description>
      <category>leetcode</category>
      <category>csharp</category>
    </item>
    <item>
      <title>Two Sum</title>
      <dc:creator>Abhi Agarwal</dc:creator>
      <pubDate>Fri, 21 Aug 2020 00:34:51 +0000</pubDate>
      <link>https://dev.to/abhiagarwal192/two-sum-2fh0</link>
      <guid>https://dev.to/abhiagarwal192/two-sum-2fh0</guid>
      <description>&lt;p&gt;Given an array of integers, return indices of the two numbers such that they add up to a specific target.&lt;/p&gt;

&lt;p&gt;You may assume that each input would have exactly one solution, and you may not use the same element twice.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;Given nums = [2, 7, 11, 15], target = 9,&lt;/p&gt;

&lt;p&gt;Because nums[0] + nums[1] = 2 + 7 = 9,&lt;br&gt;
return [0, 1].&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;Since for each element 'x' present in the array, we will have to check whether a number 'y' exists in the array such that sum of two numbers is target and return indices of the number. &lt;/p&gt;

&lt;p&gt;We can also say that, for any element 'x' present in the array , find a number 'target-x' which is also present in the array.&lt;/p&gt;

&lt;p&gt;One approach to the problem is to scan the complete array for finding number 'target-x' for each element 'x'. &lt;/p&gt;

&lt;p&gt;Another approach is to use dictionaries. For each element 'x' in the array, check whether the dictionary contains element 'target-x'.&lt;/p&gt;

&lt;p&gt;So here, first we will scan the complete array to store the elements of the array in the dictionary with element as the key and index of the element as the value.&lt;/p&gt;

&lt;p&gt;Here is the link to my github repo that contains the solution &lt;a href="https://gist.github.com/AbhiAgarwal192/cff07d1e941b014ffd38f8717d4c4f87.js"&gt;Link&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;We are checking for map[nums[i]]&amp;gt;=2 to avoid fetching the same element twice.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For Example: &lt;/p&gt;

&lt;p&gt;Given nums = [2, 7, 11, 15], target = 4,&lt;/p&gt;

&lt;p&gt;If we are trying to find complement of 2 which is 2 for target 4 &amp;gt;then dictionary will still return true since the element is &amp;gt;present but it's not an actual solution to the problem.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Link to my blog: &lt;a href="https://chashsolution.blogspot.com/2020/08/two-sum.html"&gt;Link&lt;/a&gt;&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>csharp</category>
    </item>
  </channel>
</rss>
