<?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: Manish Reddy Nandineni</title>
    <description>The latest articles on Forem by Manish Reddy Nandineni (@nmreddy1911).</description>
    <link>https://forem.com/nmreddy1911</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%2F406172%2Facb9e5d3-c927-4cbc-90f5-11c9e581523b.png</url>
      <title>Forem: Manish Reddy Nandineni</title>
      <link>https://forem.com/nmreddy1911</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nmreddy1911"/>
    <language>en</language>
    <item>
      <title>Day #8: Number Of Unival Trees Using Python</title>
      <dc:creator>Manish Reddy Nandineni</dc:creator>
      <pubDate>Fri, 19 Jun 2020 16:58:29 +0000</pubDate>
      <link>https://forem.com/nmreddy1911/day-8-number-of-unival-trees-using-python-5ha4</link>
      <guid>https://forem.com/nmreddy1911/day-8-number-of-unival-trees-using-python-5ha4</guid>
      <description>&lt;p&gt;Hello everyone.&lt;br&gt;
Today is the Day 8 of the #100DaysOfCodeChallenge.&lt;br&gt;
Received a problem previously asked by Google with an easy tag to it.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Question On Day #8:
&lt;/h2&gt;

&lt;p&gt;A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value.&lt;/p&gt;

&lt;p&gt;Given the root to a binary tree, count the number of unival subtrees.&lt;/p&gt;

&lt;p&gt;For example, the following tree has 5 unival subtrees:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   0
  / \
 1   0
    / \
   1   0
  / \
 1   1

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

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Before moving on to the main logic, let's discuss about the &lt;strong&gt;Brute Force&lt;/strong&gt; way to solve this:

&lt;ul&gt;
&lt;li&gt;Starting from the root, we check if the immediate children have the same value as the root node, and then the children of the immediate nodes and so on.&lt;/li&gt;
&lt;li&gt;We repeat this process for every subtree ending up in time taking process.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  Optimal Way
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;The optimal way to solve this problem is by using the bottom-up approach. We first start from the leaf nodes, and move to the upper levels and use the unary value of the child subtree to decide if the main tree is a unary tree or not.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Python Code&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;No. of unival subtrees= 5&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h1&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Visualiser&lt;br&gt;
&lt;/h1&gt;

&lt;p&gt;To understand the recursion behind this code, please click &lt;a href="http://pythontutor.com/visualize.html#code=class%20Node%3A%0A%20%20%20%20def%20__init__%28self,%20value,%20left%3DNone,%20right%3DNone%29%3A%0A%20%20%20%20%20%20%20%20self.val%20%3D%20value%0A%20%20%20%20%20%20%20%20self.left%20%3D%20left%0A%20%20%20%20%20%20%20%20self.right%20%3D%20right%0A%0A%0Adef%20countSubTree%28root,%20count%29%3A%0A%20%20%20%20if%20root%20is%20None%3A%0A%20%20%20%20%20%20%20%20return%20True%0A%20%20%20%20left%20%3D%20countSubTree%28root.left,%20count%29%0A%20%20%20%20right%20%3D%20countSubTree%28root.right,%20count%29%0A%20%20%20%20%23%20checking%20root%20value%20and%20children's%20values%0A%20%20%20%20if%20left%20%3D%3D%20False%20or%20right%20%3D%3D%20False%3A%0A%20%20%20%20%20%20%20%20return%20False%0A%20%20%20%20if%20root.left%20and%20root.val%20!%3D%20root.left.val%3A%0A%20%20%20%20%20%20%20%20return%20False%0A%20%20%20%20if%20root.right%20and%20root.val%20!%3D%20root.right.val%3A%0A%20%20%20%20%20%20%20%20return%20False%0A%20%20%20%20%23%20If%20all%20the%20above%20cases%20are%20not%20true,%20i.e.%20if%20the%20root%20value%20matches%20with%20both%20the%20children%0A%20%20%20%20%23%20then%20this%20case%20is%20reached,%20we%20increment%20the%20count%0A%20%20%20%20count%5B0%5D%20%2B%3D%201%0A%20%20%20%20return%20True%0A%0A%0Adef%20countTrees%28root%29%3A%0A%20%20%20%20count%20%3D%20%5B0%5D%0A%20%20%20%20%23%20calling%20a%20new%20function,%20to%20maintain%20the%20value%20of%20count%20in%20recursive%20calls%0A%20%20%20%20countSubTree%28root,%20count%29%0A%20%20%20%20return%20count%5B0%5D%0A%0A%0Aroot%20%3D%20Node%280,%20Node%281%29,%20Node%280,%20Node%281,%20Node%281%29,%20Node%281%29%29,%20Node%280%29%29%29%0Aprint%28%22No.%20of%20unival%20subtrees%3D%22,%20countTrees%28root%29%29&amp;amp;cumulative=false&amp;amp;curInstr=113&amp;amp;heapPrimitives=nevernest&amp;amp;mode=display&amp;amp;origin=opt-frontend.js&amp;amp;py=3&amp;amp;rawInputLstJSON=%5B%5D&amp;amp;textReferences=false"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Feel free to reach out for any query clearance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks and cheers:)&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>prolog</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Day #7: Number of possible decodable messages</title>
      <dc:creator>Manish Reddy Nandineni</dc:creator>
      <pubDate>Wed, 17 Jun 2020 15:31:23 +0000</pubDate>
      <link>https://forem.com/nmreddy1911/day-7-number-of-possible-decodable-messages-14nd</link>
      <guid>https://forem.com/nmreddy1911/day-7-number-of-possible-decodable-messages-14nd</guid>
      <description>&lt;p&gt;Hello everyone.&lt;br&gt;
Today is the Day 7 of the #100DaysOfCodeChallenge.&lt;br&gt;
And it's a week!!!!&lt;br&gt;
Received a problem previously asked by Facebook with a hard tag to it.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Question On Day #6:
&lt;/h2&gt;

&lt;p&gt;Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of ways it can be decoded.&lt;/p&gt;

&lt;p&gt;For example, the message '111' would give 3, since it could be decoded as 'aaa', 'ka', and 'ak'.&lt;/p&gt;

&lt;p&gt;You can assume that the messages are decodable. For example, '001' is not allowed.&lt;/p&gt;
&lt;h2&gt;
  
  
  Algorithm
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The algorithm is similar to the concept behind fibonacci series. For every increase in the value of n is fibonacci we added it to the n-1 sum.&lt;/li&gt;
&lt;li&gt;Similarly, for every increase in the length of the message, we add a particular value to the number of possible ways, based on certain conditions.&lt;/li&gt;
&lt;li&gt;Points to check:

&lt;ul&gt;
&lt;li&gt;a 1-digit number is considered a message only if it is greater than 0, because a is represented by 1 according to the question.&lt;/li&gt;
&lt;li&gt;a 2-digit number is considered a message only if it has 2 in the ten's place and digit&amp;lt;7 in the one's place or 1 in ten's place and any digit in one's place.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Python Code&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
    Number Of Ways: 3&lt;/p&gt;

&lt;h1&gt;
  
  
  Visualiser
&lt;/h1&gt;

&lt;p&gt;You can look into the visualiser to understand the BTS of the logic &lt;a href="http://pythontutor.com/visualize.html#code=message%20%3D%20%221212121%22%0Al%20%3D%20len%28message%29%0Aa%20%3D%201%0Ab%20%3D%201%0Ac%20%3D%200%0Afor%20i%20in%20range%282,%20l%2B1%29%3A%0A%20%20%20%20m1%3Dmessage%5Bi-1%5D%0A%20%20%20%20m2%3Dmessage%5Bi-2%5D%0A%20%20%20%20if%20i%20!%3D%202%3A%0A%20%20%20%20%20%20%20%20a%20%3D%20b%0A%20%20%20%20%20%20%20%20b%20%3D%20c%0A%20%20%20%20%20%20%20%20c%20%3D%200%0A%20%20%20%20if%20m1%20!%3D%20'0'%3A%0A%20%20%20%20%20%20%20%20c%20%3D%20b%0A%20%20%20%20if%20%28m2%20%3D%3D%20'1'%20or%20m2%20%3D%3D%20'2'%29%20and%20m1%20%3C%20'7'%3A%0A%20%20%20%20%20%20%20%20c%20%2B%3D%20a%0Aprint%28%22Number%20Of%20Ways%3A%22,%20c%29&amp;amp;cumulative=false&amp;amp;curInstr=46&amp;amp;heapPrimitives=nevernest&amp;amp;mode=display&amp;amp;origin=opt-frontend.js&amp;amp;py=3&amp;amp;rawInputLstJSON=%5B%5D&amp;amp;textReferences=false"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Feel free to reach out for any query clearance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks and cheers:)&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>prolog</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Day #6: XOR Doubly Linked List</title>
      <dc:creator>Manish Reddy Nandineni</dc:creator>
      <pubDate>Tue, 16 Jun 2020 12:49:39 +0000</pubDate>
      <link>https://forem.com/nmreddy1911/day-6-xor-doubly-linked-list-33ae</link>
      <guid>https://forem.com/nmreddy1911/day-6-xor-doubly-linked-list-33ae</guid>
      <description>&lt;p&gt;Hello.&lt;br&gt;
Today is the Day 6 of the #100DaysOfCodeChallenge. Received a problem previously asked by Google with a hard tag to it.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Question On Day #6:
&lt;/h2&gt;

&lt;p&gt;An XOR linked list is a more memory efficient doubly linked list. Instead of each node holding &lt;code&gt;next&lt;/code&gt; and &lt;code&gt;prev&lt;/code&gt; fields, it holds a field named &lt;code&gt;both&lt;/code&gt;, which is an XOR of the next node and the previous node. Implement an XOR linked list; it has an &lt;code&gt;add(element)&lt;/code&gt; which adds the element to the end, and a &lt;code&gt;get(index)&lt;/code&gt; which returns the node at index.&lt;/p&gt;

&lt;p&gt;Doing it on C++, since pointer implementation on Python is messy and not efficient.&lt;/p&gt;
&lt;h2&gt;
  
  
  Algorithm
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The main logic behind the question is to understand the properties of XOR.&lt;/li&gt;
&lt;li&gt;Refer &lt;a href="https://en.wikipedia.org/wiki/XOR_linked_list"&gt;wiki&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;C++ Code&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Nodes: &lt;br&gt;
40 30 20 10 &lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  &lt;strong&gt;Explanation&lt;/strong&gt;&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Not getting much into the details of the logic, the wikipedia reference speaks it all.&lt;/p&gt;

&lt;p&gt;You can look into the visualiser to understand the BTS of the logic &lt;a href="http://www.pythontutor.com/cpp.html#code=%23include%20%3Cbits/stdc%2B%2B.h%3E%0A%23include%20%3Cinttypes.h%3E%0Ausing%20namespace%20std%3B%0Aclass%20Node%0A%7B%0Apublic%3A%0A%20%20%20%20int%20val%3B%0A%20%20%20%20Node%20*ptr%3B%0A%7D%3B%0A%0ANode%20*XOR%28Node%20*a,%20Node%20*b%29%0A%7B%0A%20%20%20%20return%20%28Node%20*%29%28%28uintptr_t%29%28a%29%20%5E%20%28uintptr_t%29%28b%29%29%3B%0A%7D%0A%0Avoid%20insert%28Node%20**head_ref,%20int%20val%29%0A%7B%0A%0A%20%20%20%20Node%20*new_node%20%3D%20new%20Node%28%29%3B%0A%20%20%20%20new_node-%3Eval%20%3D%20val%3B%0A%20%20%20%20new_node-%3Eptr%20%3D%20*head_ref%3B%0A%20%20%20%20if%20%28*head_ref%20!%3D%20NULL%29%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%28*head_ref%29-%3Eptr%20%3D%20XOR%28new_node,%20%28*head_ref%29-%3Eptr%29%3B%0A%20%20%20%20%7D%0A%20%20%20%20*head_ref%20%3D%20new_node%3B%0A%7D%0Avoid%20printList%28Node%20*head%29%0A%7B%0A%20%20%20%20Node%20*curr%20%3D%20head%3B%0A%20%20%20%20Node%20*prev%20%3D%20NULL%3B%0A%20%20%20%20Node%20*next%3B%0A%20%20%20%20cout%3C%3C%22Nodes%3A/n%22%3B%0A%20%20%20%20while%20%28curr%20!%3D%20NULL%29%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20curr-%3Eval%20%3C%3C%20%22%20%22%3B%0A%20%20%20%20%20%20%20%20next%20%3D%20XOR%28prev,%20curr-%3Eptr%29%3B%0A%20%20%20%20%20%20%20%20prev%20%3D%20curr%3B%0A%20%20%20%20%20%20%20%20curr%20%3D%20next%3B%0A%20%20%20%20%7D%0A%20%20%20%20cout%20%3C%3C%20%22%5Cn%22%3B%0A%7D%0Aint%20main%28%29%0A%7B%0A%20%20%20%20Node%20*head%20%3D%20NULL%3B%0A%20%20%20%20insert%28%26head,%2010%29%3B%0A%20%20%20%20insert%28%26head,%2020%29%3B%0A%20%20%20%20insert%28%26head,%2030%29%3B%0A%20%20%20%20insert%28%26head,%2040%29%3B%0A%20%20%20%20printList%28head%29%3B%0A%20%20%20%20return%20%280%29%3B%0A%7D&amp;amp;curInstr=42&amp;amp;mode=display&amp;amp;origin=opt-frontend.js&amp;amp;py=cpp&amp;amp;rawInputLstJSON=%5B%5D"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Feel free to reach out for any query clearance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks and cheers:)&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>prolog</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Day #5: Functional Programming with Python.</title>
      <dc:creator>Manish Reddy Nandineni</dc:creator>
      <pubDate>Mon, 15 Jun 2020 14:16:53 +0000</pubDate>
      <link>https://forem.com/nmreddy1911/day-5-fuctional-programming-with-python-3p97</link>
      <guid>https://forem.com/nmreddy1911/day-5-fuctional-programming-with-python-3p97</guid>
      <description>&lt;p&gt;Hey. Hope you have been enjoying the series so far.&lt;br&gt;
Today is the Day 5 of the #100DaysOfCodeChallenge. Received a problem previously asked by &lt;a href="https://www.janestreet.com/"&gt;Jane Street&lt;/a&gt; (refer the hyperlink for the company details) with a medium tag to it. This question is a true puzzle and can be solved in a minute if your functional programming understanding is strong.&lt;/p&gt;

&lt;p&gt;You may get confused with the question, just like the way I did. I will try my best in putting the solution in the simplest way possible. You can always reach out to me if you need help.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Question On Day #5:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;cons(a, b)&lt;/code&gt; constructs a pair, and &lt;code&gt;car(pair)&lt;/code&gt; and &lt;code&gt;cdr(pair)&lt;/code&gt; returns the first and last element of that pair. For example, &lt;code&gt;car(cons(3, 4))&lt;/code&gt; returns &lt;code&gt;3&lt;/code&gt;, and &lt;code&gt;cdr(cons(3, 4))&lt;/code&gt; returns &lt;code&gt;4&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Given this implementation of cons:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def cons(a, b):
    def pair(f):
        return f(a, b)
    return pair
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Implement &lt;code&gt;car&lt;/code&gt; and &lt;code&gt;cdr&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;The question is a bit confusing but let us try to understand it clearly.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;cons&lt;/code&gt; is a function which takes two inputs (consider integers). It returns a value which is an function defined inside itself, i.e. &lt;code&gt;pair&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pair&lt;/code&gt; is a function which takes a function &lt;code&gt;f&lt;/code&gt; as input and returns the return value of its input function, i.e. the return value of the function &lt;code&gt;f&lt;/code&gt; is returned by &lt;code&gt;pair&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Now &lt;code&gt;f&lt;/code&gt; is a function which will return a value.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;f&lt;/code&gt; is the key to all our solution.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Algorithm
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The end value obtained on calling the function cons is a function pointer pointing to the &lt;code&gt;pair&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;The required implementation goes as follows:&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Python Code&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
    3&lt;br&gt;
    4&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Explanation&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When &lt;strong&gt;&lt;code&gt;car(cons(3,4))&lt;/code&gt;&lt;/strong&gt; is called, &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;cons(3,4) is invoked in the first place.&lt;/li&gt;
&lt;li&gt;cons returns the function pair function.&lt;/li&gt;
&lt;li&gt;The parameter passed to the &lt;code&gt;car&lt;/code&gt; function is the &lt;code&gt;pair&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;car&lt;/code&gt; function returns the value returned by calling the &lt;code&gt;pair&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pair&lt;/code&gt; function takes a function as input, so we pass the &lt;code&gt;left&lt;/code&gt; function to it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pair&lt;/code&gt; function returns the value obtained by passing a, b to a a function &lt;code&gt;f&lt;/code&gt;. So, our &lt;code&gt;left&lt;/code&gt; is the function &lt;code&gt;f&lt;/code&gt;, which takes a, b as input and returns a as output to the &lt;code&gt;pair&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt; Now the same a is returned by &lt;code&gt;pair&lt;/code&gt; function to the &lt;code&gt;car&lt;/code&gt; function, which will again return the same value of a.&lt;/li&gt;
&lt;li&gt;Now a is returned at the end and printed.&lt;/li&gt;
&lt;li&gt;Same goes with the function &lt;code&gt;cdr&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  The Python visualiser is very much needed to understand this logic more clearly, please refer it &lt;a href="https://cscircles.cemc.uwaterloo.ca/visualize#code=%0Adef+cons%28a,+b%29:%0A++++def+pair%28f%29:%0A++++++++return+f%28a,+b%29%0A++++return+pair%0Adef+car%28pair%29:%0A++++def+left%28a,b%29:%0A++++++++return+a%0A++++return+pair%28left%29%0Adef+cdr%28pair%29:%0A++++def+right%28a,b%29:%0A++++++++return+b%0A++++return+pair%28right%29%0Aprint%28car%28cons%283,4%29%29%29%0Aprint%28cdr%28cons%283,4%29%29%29&amp;amp;mode=display&amp;amp;raw_input=&amp;amp;curInstr=33"&gt;here&lt;/a&gt;.
&lt;/h1&gt;

&lt;p&gt;I hope you were able to understand this problem.&lt;br&gt;
Feel free to reach out for any query clearance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks and cheers:)&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>prolog</category>
      <category>beginners</category>
      <category>functional</category>
    </item>
    <item>
      <title>Day #4: Finding the smallest positive missing integer from an unordered list using Python.</title>
      <dc:creator>Manish Reddy Nandineni</dc:creator>
      <pubDate>Sun, 14 Jun 2020 13:50:07 +0000</pubDate>
      <link>https://forem.com/nmreddy1911/day-4-finding-the-smallest-positive-missing-integer-from-an-unordered-list-using-python-11a9</link>
      <guid>https://forem.com/nmreddy1911/day-4-finding-the-smallest-positive-missing-integer-from-an-unordered-list-using-python-11a9</guid>
      <description>&lt;p&gt;Hey, today is the Day 4 of the #100DaysOfCodeChallenge. Received a problem previously asked by Stripe with a hard tag to it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Question On Day #4:
&lt;/h2&gt;

&lt;p&gt;Given an array of integers, find the first missing positive integer in &lt;strong&gt;linear time and constant space&lt;/strong&gt;. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.&lt;/p&gt;

&lt;p&gt;For example, the input &lt;code&gt;[3, 4, -1, 1]&lt;/code&gt; should give &lt;code&gt;2&lt;/code&gt;. The input &lt;code&gt;[1, 2, 0]&lt;/code&gt; should give &lt;code&gt;3&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Algorithm
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Input the given array into a list&lt;/li&gt;
&lt;li&gt;Filter out all the non-positive numbers from the list.&lt;/li&gt;
&lt;li&gt;Note that after filtering the non positive elements, the maximum number of positive numbers in the array at the best case can be n, i.e. all the numbers from 1 to n, where n is the length of the positive numbers list.&lt;/li&gt;
&lt;li&gt;Now we can mark all the numbers greater than n as not required, because the smallest positive number will definitely be in the range of [1,n] or it will be n+1 if all the numbers from 1 to n are in the array.&lt;/li&gt;
&lt;li&gt;Coming to the core logic, consider an element &lt;code&gt;v&lt;/code&gt; in the array which is not flag, i.e. it is in the range of [1,n]. Now mark the value at index &lt;code&gt;v&lt;/code&gt; negative. If the value v is negative, consider its absolute value.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Why does this work?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When a element is made negative, it means that the index &lt;code&gt;i&lt;/code&gt; where it is located, which is a positive integer, exists in the list.&lt;/li&gt;
&lt;li&gt;If the positive integer doesn't exist in the list, that particular index remains positive, so when the array is traversed, we can exit at the first positive number encountered and output that index since that is the missing number. &lt;/li&gt;
&lt;li&gt;If the entire array is negative, it means that all the numbers from 1 to n exist in the array, hence the smallest positive number missing is (n+1).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Point to note:&lt;/strong&gt; Since index start from 0, it should be kept in mind while negating the numbers and returning the result.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Python Code&lt;/strong&gt;  &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;7
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Feel free to reach out for any query clearance.&lt;br&gt;
&lt;strong&gt;Thanks and cheers:)&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>prolog</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Day #3: Serializing and Deserializing a Binary Tree with Python.</title>
      <dc:creator>Manish Reddy Nandineni</dc:creator>
      <pubDate>Sat, 13 Jun 2020 09:23:32 +0000</pubDate>
      <link>https://forem.com/nmreddy1911/day-3-serializing-and-deserializing-a-binary-tree-with-python-2kdd</link>
      <guid>https://forem.com/nmreddy1911/day-3-serializing-and-deserializing-a-binary-tree-with-python-2kdd</guid>
      <description>&lt;p&gt;Hello, today is the Day 3 of the #100DaysOfCodeChallenge. Received a problem previously asked by Google with a medium tag to it. This is also listed in the hard set of problems on LeetCode.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Question On Day #3:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Given the root to a binary tree, implement &lt;code&gt;serialize(root)&lt;/code&gt;, which serializes the tree into a string, and &lt;code&gt;deserialize(s)&lt;/code&gt;, which deserializes the string back into the tree.&lt;br&gt;
For example, given the following &lt;code&gt;Node&lt;/code&gt; class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node:
    def __init__(self, val, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;The following test should pass:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node = Node('root', Node('left', Node('left.left')), Node('right'))
assert deserialize(serialize(node)).left.left.val == 'left.left'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Algorithm
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create the binary tree using the Node class. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Serializing:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;To serialize the tree into a string, I considered the null nodes as #.&lt;/li&gt;
&lt;li&gt;Starting from root, Depth First Search is used to traverse through the tree. &lt;/li&gt;
&lt;li&gt;If a node exists, its value is appended to the string and the left sub tree is traversed.&lt;/li&gt;
&lt;li&gt;This process continues until there are no more children left. Then a '#' is appended to the array and the recursive function is exited.&lt;/li&gt;
&lt;li&gt;Then the right sub tree is traversed and the same process is followed.&lt;/li&gt;
&lt;li&gt;The final resulting is string is returned.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deserializing:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;To deserialize a string to form a tree, the first word in the space seperated string is considered as the root.&lt;/li&gt;
&lt;li&gt;When a '#' is encountered, it is considered as a empty node and None is returned.&lt;/li&gt;
&lt;li&gt;In the remaining cases, a new node with the value as the word is formed and the remaining string is traversed.&lt;/li&gt;
&lt;li&gt;This function is called recursively until the end of the string is reached.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Python Code&lt;/strong&gt;  &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String after serializing node: root left left.left # # # right # # &lt;br&gt;
Assert: True&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Python Visualiser To Understand The Recursion Better&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;View the visualisation of the above code in the &lt;a href="https://cscircles.cemc.uwaterloo.ca/visualize#code=class+Node%3A%0A++++def+__init__(self,+val,+left%3DNone,+right%3DNone)%3A%0A++++++++self.val+%3D+val%0A++++++++self.left+%3D+left%0A++++++++self.right+%3D+right%0A%0A%0Anode+%3D+Node('root',+Node('left',+Node('left.left')),+Node('right'))%0As+%3D+%22%22%0A%0A%23+serializing+function%0A%0A%0Adef+serialize(node,+s%3D%22%22)%3A%0A++++if(not+node)%3A%0A++++++++s+%2B%3D+%22%23+%22%0A++++++++return+s%0A++++s+%2B%3D+(str(node.val)%2B%22+%22)%0A++++s+%3D+serialize(node.left,+s%3Ds)%0A++++s+%3D+serialize(node.right,+s%3Ds)%0A++++return+s%0A%0A%0A%23+deserializing+function%0Ai+%3D+0%0A%0A%0Adef+deserialize(s)%3A%0A++++global+i%0A++++if+s%5Bi%5D+%3D%3D+%22%23%22%3A%0A++++++++if(i+%3C+len(s)-2)%3A%0A++++++++++++i+%2B%3D+2%0A++++++++return+None%0A++++else%3A%0A++++++++space+%3D+s%5Bi%3A%5D.find(%22+%22)%0A++++++++sp+%3D+space%2Bi%0A++++++++node+%3D+Node(s%5Bi%3Asp%5D)%0A++++++++i+%3D+sp%2B1%0A++++++++node.left+%3D+deserialize(s)%0A++++++++node.right+%3D+deserialize(s)%0A++++++++return+node%0A%0Aprint(%22String+after+serializing+node%3A+%22%2Bserialize(node))%0Aprint(%22Assert%3A%22,deserialize(serialize(node)).left.left.val+%3D%3D+'left.left')&amp;amp;mode=display&amp;amp;raw_input=&amp;amp;curInstr=223"&gt;Python Visualiser&lt;/a&gt; to understand the recursion better, step by step.&lt;/p&gt;

&lt;p&gt;Please drop your queries in the comments section.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks and cheers:)&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>prolog</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Day #2: Product of all the elements of the array, except self.</title>
      <dc:creator>Manish Reddy Nandineni</dc:creator>
      <pubDate>Fri, 12 Jun 2020 03:34:21 +0000</pubDate>
      <link>https://forem.com/nmreddy1911/day-2-product-of-all-the-elements-of-the-array-except-self-1ale</link>
      <guid>https://forem.com/nmreddy1911/day-2-product-of-all-the-elements-of-the-array-except-self-1ale</guid>
      <description>&lt;p&gt;Hello, today is the Day 2 of the #100DaysOfCodeChallenge. Received a problem previously asked by Uber with a hard tag to it. Tried my best in solving it, looking for better solutions if possible. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;But before that, an addition to yesterday's solution. The reason why in operator takes a time of O(1) on average for sets is: in Python, the sets are stored as hash tables. So when we need to check for the membership of a key, python calculates it's hash value which is the index and thus gives the answer if it exists or not. In most cases, the hashes are unique, and the worst case occurs only when the hash value is common for all the elements in the set and that's when all the elements are to be checked leading to a time complexity of O(n).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The Question On Day #2:  &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Given an array of integers, return a new array such that each element at  
index `i` of the new array is the product of all the numbers in the  
original array except the one at `i`.

For example, if our input was `[1, 2, 3, 4, 5]`, the expected output would  
be `[120, 60, 40, 30, 24]`. If our input was `[3, 2, 1]`, the expected  
output would be `[2, 3, 6]`. 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Condition: Division is not allowed.&lt;/strong&gt;  &lt;/p&gt;

&lt;h2&gt;
  
  
  Naive Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Input the given &lt;em&gt;n&lt;/em&gt; numbers into a list.
&lt;/li&gt;
&lt;li&gt;Traverse through the list n times and calculate the product each time, by skipping the number at that index.
&lt;/li&gt;
&lt;li&gt;Store the product in a new answer list.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Python Code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;     &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  
     &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
     &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;  
        &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;  
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
                &lt;span class="k"&gt;continue&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;*=&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  
            &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;  
     &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Time Complexity Of The Above Solution&lt;/strong&gt;  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The traversal through the list for the outer loop executes at a time of O(n). The inner loop again takes a time of O(n) to traverse through the entire list.&lt;/strong&gt;  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The total time complexity for the solution will result in a time complexity of O(n^2).&lt;/em&gt;&lt;/strong&gt;  &lt;/p&gt;

&lt;h2&gt;
  
  
  Better Approach
&lt;/h2&gt;

&lt;p&gt;I wrote a code satisfying the problem statement but found a better solution than mine in terms of time complexity at LeetCode. &lt;a href="https://leetcode.com/articles/product-of-array-except-self/#"&gt;ref&lt;/a&gt;  &lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Input the given &lt;em&gt;n&lt;/em&gt; numbers into a list.
&lt;/li&gt;
&lt;li&gt;Consider that each element has a left product and a right produt, which mean product of all the elements to the left of the element and product of all the elements to the right of the element respectively.
&lt;/li&gt;
&lt;li&gt;Store the left and right products of each element in 2 different lists l and r where l has the left product of an element at the index i in list a and r similarly has the right product.
&lt;/li&gt;
&lt;li&gt;Now store the product of multiplying l[i[4] and r[i] in r[i] itself, which is the final output list.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Python Code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  
    &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
    &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;  
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;  

    &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;             &lt;span class="c1"&gt;#since there is no element to the left of the first element we keep it's left product as 1  
&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;  
        &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  

    &lt;span class="s"&gt;'''Ex: take i=1 the left product at i=1 is product of all elements  
    to the left of it. Since we have the left product of the   
    previous element already stored in the list l we multiply  
    it with the element at i-1.'''&lt;/span&gt;  

    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;       &lt;span class="c1"&gt;#since there is no element to the right of the last element.  
&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt; 
        &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="c1"&gt;#same logic as above but in reverrse order
&lt;/span&gt;
    &lt;span class="c1"&gt;#calculating the individual product
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;*=&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Time Complexity Of The Above Solution&lt;/strong&gt;  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The 3 individual loops need 1 traversal each through the entire list and each loop has only one operation in it.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Thus the total time complexity of the above program is O(n)&lt;/strong&gt;    &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Thanks and cheers:)&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>prolog</category>
    </item>
    <item>
      <title>Day #1: My Start With 100 Days Of Code</title>
      <dc:creator>Manish Reddy Nandineni</dc:creator>
      <pubDate>Wed, 10 Jun 2020 17:32:13 +0000</pubDate>
      <link>https://forem.com/nmreddy1911/my-start-with-100-days-of-code-59e6</link>
      <guid>https://forem.com/nmreddy1911/my-start-with-100-days-of-code-59e6</guid>
      <description>&lt;p&gt;Hi! I'm Manish. I was wondering what to do in this COVID-19 pandemic, to stay a bit productive and then the phrase &lt;strong&gt;100 Days Of Code&lt;/strong&gt; striked me while surfing the internet and I decided to take up the challenge. &lt;/p&gt;

&lt;p&gt;To begin with that, I have enolled for the email subscription by &lt;a href="https://www.dailycodingproblem.com/"&gt;&lt;strong&gt;Daily Coding Problems&lt;/strong&gt;&lt;/a&gt;. The team at DCP sends us an email with one coding problem a day and I believed that it would be the right thing for the challenge. To add to that, I have decided to share my experience on solving the question too and start writing on &lt;strong&gt;dev&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The Question On Day #1:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Given a list of numbers and a number `k`, return whether any two numbers from the list add up to `k`.

For example, given `[10, 15, 3, 7]` and `k` of `17`, return true since `10 + 7` is `17`.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  My First Approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Input the given numbers into a list and and the sum to check in a variable.&lt;/li&gt;
&lt;li&gt;Traverse through the list and for each element at index &lt;em&gt;i&lt;/em&gt;, check if there exists a complementary number starting from the index &lt;em&gt;i&lt;/em&gt; in the list.&lt;/li&gt;
&lt;li&gt;If there exists atleast one such number print "pair exists" and exit the loop by marking a flag.&lt;/li&gt;
&lt;li&gt;After the cursor exits the loop adn if the flag is still unmarked, print "pair doesn't exist".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Python Code&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;l=[10,15,3,7]
k=17
flag=0
for i in range(len(l)-1):
    if (k-l[i]) in l[i+1:]:
        #slicing to prevent additional checks
        print("pair exists")
        flag=1
        break
if flag==0:
    print("pair doesn't exist")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

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

&lt;ul&gt;
&lt;li&gt;The check for the complementary number is only done for the sliced list from i+1 to len(l), since the previous number pairs are already checked in the iterations before.&lt;/li&gt;
&lt;li&gt;The last element of the list is thus skipped from being checked for a complementary number.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity Of The Above Solution&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The traversal through the list takes a time of O(n) where n is the length of list. For each element, there is an another check with &lt;em&gt;in&lt;/em&gt; operator which again takes a time complexity of O(n-i) for a list in python &lt;a href="https://wiki.python.org/moin/TimeComplexity"&gt;ref&lt;/a&gt;.&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The total time complexity for the solution will result in a time complexity of O(n^2).&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Better Approach
&lt;/h2&gt;

&lt;p&gt;An approach better to the above solution is to convert the list into a set first, does making the &lt;em&gt;in operator&lt;/em&gt; efficient to use.&lt;/p&gt;

&lt;p&gt;The time complexity of using the in operator on a set is O(1) on average and O(n) in the worst case.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python Code&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;l=[10,15,3,7]
k=17
s=set(l) #converting list l into a set and storing it in s
flag=0
for i in s:
    if (k-i) in s:
        print("pair exists")
        flag=1
        break
if flag==0:
    print("pair doesn't exist")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity Of The Above Solution&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The conversion from list to a set takes a time of O(n) where n is the length of list. Hence, the total time complexity for the above solution is O(n)+O(nx1)&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;[O(nx1) on average and O(nxn) in the worst case for the loop and condition check using in operator on the set s].&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Thus the total time complexity on average results to O(n) and O(n^2) in the worst case scenario.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This question helped me in understanding the time complexities of individual data structure operations in python. I would love to hear from my fellow developers and take suggestions on improving the above logic. I am a pretty newbie to solving such questions, please consider it. I also request you to drop suggestions to improve my article readability or understanding.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Thanks and cheers:)&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>prolog</category>
    </item>
  </channel>
</rss>
