<?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: Mridu Bhatnagar</title>
    <description>The latest articles on DEV Community by Mridu Bhatnagar (@mridubhatnagar).</description>
    <link>https://dev.to/mridubhatnagar</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%2F47806%2F6462044b-bfc9-4c6e-b7e7-bc177fe6f918.jpeg</url>
      <title>DEV Community: Mridu Bhatnagar</title>
      <link>https://dev.to/mridubhatnagar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mridubhatnagar"/>
    <language>en</language>
    <item>
      <title>Course Launch 🚀 - Sets and Frozen Sets in Python</title>
      <dc:creator>Mridu Bhatnagar</dc:creator>
      <pubDate>Wed, 26 May 2021 18:58:57 +0000</pubDate>
      <link>https://dev.to/mridubhatnagar/course-launch-sets-and-frozen-sets-in-python-46g5</link>
      <guid>https://dev.to/mridubhatnagar/course-launch-sets-and-frozen-sets-in-python-46g5</guid>
      <description>&lt;p&gt;Are you a newbie and is your next Python learning goal is Sets and Frozen Sets?&lt;/p&gt;

&lt;p&gt;Preparing for an upcoming interview, want to brush up your knowledge on Sets and Frozen Sets while solving problem alongside?&lt;/p&gt;

&lt;p&gt;I recently built and published a self-paced Python course on Linkedin Learning on Sets and Frozen Sets.&lt;/p&gt;

&lt;p&gt;Course Features 👇:&lt;/p&gt;

&lt;p&gt;📌 The full course is FREE for a month from the date of your sign-up.&lt;br&gt;
📌 3 bite-sized lessons are available for preview (no sign-up needed).&lt;br&gt;
📌 You can download the lesson on Linkedin Learning iOS and android applications and can learn offline.&lt;br&gt;
📌 Hands-on examples, MCQs, challenges&lt;br&gt;
📌 The course is a collection of bite-sized videos.&lt;br&gt;
📌 Course duration - 1 hour 37 min, lesson duration varies between 40 sec to 6 minutes maximum.&lt;/p&gt;

&lt;p&gt;NOTE - If your employer or your university has given you access to the Linkedin Learning library. You would be able to access the full course.&lt;/p&gt;

&lt;p&gt;Take the course and spread the word among your peers.&lt;br&gt;
If you take the course do let me know your feedback.&lt;/p&gt;

&lt;p&gt;Course Link:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/learning/python-data-structures-sets-and-frozen-sets"&gt;https://www.linkedin.com/learning/python-data-structures-sets-and-frozen-sets&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>python</category>
      <category>codenewbie</category>
      <category>showdev</category>
    </item>
    <item>
      <title>Build a Vocabulary Bot for WhatsApp using Twilio and Python</title>
      <dc:creator>Mridu Bhatnagar</dc:creator>
      <pubDate>Thu, 02 Jul 2020 13:17:10 +0000</pubDate>
      <link>https://dev.to/mridubhatnagar/build-a-vocabulary-bot-for-whatsapp-using-twilio-and-python-1dji</link>
      <guid>https://dev.to/mridubhatnagar/build-a-vocabulary-bot-for-whatsapp-using-twilio-and-python-1dji</guid>
      <description>&lt;p&gt;My mother teaches English to visually impaired students. Due to the current situation with the Coronavirus, in-person classes had to be suspended, and now she is teaching through phone conference calls, with all learning material being exchanged on WhatsApp. I saw an opportunity to solve a problem here, so I decided to build a Vocabulary chatbot to assist her in her teaching.&lt;/p&gt;

&lt;p&gt;This tutorial will show you how to create a WhatsApp chatbot that can help you in improving your vocabulary using the Twilio API for WhatsApp, Python and Flask. Below, you can see the conversation I had with the bot.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.twilio.com/blog/build-vocabulary-bot-whatsapp-python-twilio"&gt;https://www.twilio.com/blog/build-vocabulary-bot-whatsapp-python-twilio&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>A long pending to-do task - The Portfolio Website!!!</title>
      <dc:creator>Mridu Bhatnagar</dc:creator>
      <pubDate>Mon, 15 Jun 2020 15:19:20 +0000</pubDate>
      <link>https://dev.to/mridubhatnagar/a-long-pending-to-do-task-the-portfolio-website-157l</link>
      <guid>https://dev.to/mridubhatnagar/a-long-pending-to-do-task-the-portfolio-website-157l</guid>
      <description>&lt;p&gt;Since a long time I was contemplating on building a portfolio website. But, got stuck with frameworks, languages, tools, static site generators, content management system the list goes on and on... and the downward spiral began. &lt;/p&gt;

&lt;p&gt;Procastination, indecisiveness, indecisiveness and the time passed by. What started as a question in 2018, remained a task in my head ever since. Finally, 2020. Check. Task done. &lt;/p&gt;

&lt;p&gt;2 years to decide. 2 days to build. Sounds funny, right. This is a classic case of decision paralysis.&lt;/p&gt;

&lt;p&gt;In all these 2 years. Time and again the thought of building the website used to strike my mind. I used to check out couple of options available, reach out to more people. &lt;br&gt;
End result - confused.&lt;/p&gt;
&lt;h4&gt;
  
  
  Non-technical things I learned in the process:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The more people you ask for an advice. The more confusing it gets. Each of us have our own reasons of preferring something over the other.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;My main purpose of building this was to showcase information that my resume does not effectively convey and is important. Spending days and nights on deciding the framework was pointless.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Blogging on the same platform was never the idea. Half baked information and using the shiny new framework after thorough research. Would deviate me from the purpose of building the website.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This is just a portfolio website. Thinking about often trending questions, and most talked about topic amongst developers "Scaling". There seems to be no such scenario where the traffic will spike.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Uneccessary load on mind is not worth it. Time is limited.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Person X used Gatsby, Person Y used Jekyll, Person Z used Hugo. Don't be tempted by others choices. Everyone has a different purpose to built. Mine was to put forth information, without missing out on anything.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Animation - Might give a Wow feeling, but when wrongly done results in worst of its kind user experinece.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Don't get lost amongst all the choices that are available. With small tasks like this one not directly affecting anyone. Just get the job done. Should be the first priority.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  Technical things I learned while building it from scratch:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Good way to get a hang of fundamentals. Good old HTML/CSS/JS.&lt;/li&gt;
&lt;li&gt;Decision making process. From design, frontend, backend, deployment. All throughout you'll have to make decisions at every step.&lt;/li&gt;
&lt;li&gt;Deployment.&lt;/li&gt;
&lt;li&gt;Integrating GraphQL API.&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  Website - &lt;a href="https://www.mridubhatnagar.in/"&gt;https://www.mridubhatnagar.in/&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Ps: The site is not mobile responsive at the moment. &lt;br&gt;
To get the results open it on laptop, chrome browser.&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;
      &lt;div class="ltag__twitter-tweet__media ltag__twitter-tweet__media__video-wrapper"&gt;
        &lt;div class="ltag__twitter-tweet__media--video-preview"&gt;
          &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--55Ou7G26--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/tweet_video_thumb/EZmZucvUEAAz4UX.jpg" alt="unknown tweet media content"&gt;
          &lt;img src="/assets/play-butt.svg" class="ltag__twitter-tweet__play-butt" alt="Play butt"&gt;
        &lt;/div&gt;
        &lt;div class="ltag__twitter-tweet__video"&gt;
          
            
          
        &lt;/div&gt;
      &lt;/div&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--XD78-0RN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/1082372789997314049/dqttTpIu_normal.jpg" alt="Mridu Bhatnagar profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Mridu Bhatnagar
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        @mridu__
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P4t6ys1m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      A long-pending task completed. 🥳 &lt;br&gt;I will be deploying it soon. 🙂 
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      16:12 PM - 03 Jun 2020
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1268213912790433793" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-reply-action.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1268213912790433793" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-retweet-action.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      1
      &lt;a href="https://twitter.com/intent/like?tweet_id=1268213912790433793" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-like-action.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
      41
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


</description>
      <category>html</category>
      <category>css</category>
      <category>javascript</category>
      <category>portfolio</category>
    </item>
    <item>
      <title>Day-20 Guess Number Higher or Lower 👩‍💻</title>
      <dc:creator>Mridu Bhatnagar</dc:creator>
      <pubDate>Sun, 03 May 2020 18:59:53 +0000</pubDate>
      <link>https://dev.to/mridubhatnagar/day-21-guess-number-higher-or-lower-2ofk</link>
      <guid>https://dev.to/mridubhatnagar/day-21-guess-number-higher-or-lower-2ofk</guid>
      <description>&lt;p&gt;For the last 19 days, I am making sure to solve at least a problem a day. I got stuck at optimizing a problem named Rotate Array yesterday. And, as a result, couldn't solve a problem, could not write. I am still stuck at the same problem. Meanwhile, I solved a new one.&lt;/p&gt;

&lt;h5&gt;
  
  
  Background
&lt;/h5&gt;

&lt;p&gt;This problem statement is a part of LeetCode's learn card titled Binary Search. Under the sub-heading Binary Search Template-I.&lt;/p&gt;

&lt;h5&gt;
  
  
  Problem Statement
&lt;/h5&gt;

&lt;p&gt;We are playing the Guess Game. The game is as follows:&lt;br&gt;
I pick a number from 1 to n. You have to guess which number I picked.&lt;br&gt;
Every time you guess wrong, I'll tell you whether the number is higher or lower.&lt;br&gt;
You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-1 : My number is lower
 1 : My number is higher
 0 : Congrats! You got it!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Example
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: n = 10, pick = 6
Output: 6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Solution Approach
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;start = 1, end = n+1. (See the problem statement. Numbers from 1 to n. Hence, make sure both are inclusive).&lt;/li&gt;
&lt;li&gt;calculate mid. mid = int((start+end)/2)&lt;/li&gt;
&lt;li&gt;Now pass mid as parameter to the guess API. &lt;/li&gt;
&lt;li&gt;Divide the list based on the value returned by API.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# The guess API is already defined for you.
# @param num, your guess
# @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
# def guess(num: int) -&amp;gt; int:

class Solution:
    def guessNumber(self, n: int) -&amp;gt; int:
        start = 1
        end = n+1
        while start &amp;lt; end:
            mid = int((start+end)/2)
            if guess(mid) == -1:
                end = mid
            elif guess(mid) == 1:
                start = mid + 1
            elif guess(mid) == 0:
                return mid
        return -1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Learnings
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;The time complexity of the above solution is - O(logn).&lt;/li&gt;
&lt;li&gt;When in the problem statement it is already mentioned sorted array think binary search.&lt;/li&gt;
&lt;li&gt;Time complexity of Binary Search - O(logn). Time complexity of Linear Search - O(n). Binary search is more efficient. &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Day-19 Third Maximum Number</title>
      <dc:creator>Mridu Bhatnagar</dc:creator>
      <pubDate>Fri, 01 May 2020 06:44:22 +0000</pubDate>
      <link>https://dev.to/mridubhatnagar/day-19-third-maximum-number-521k</link>
      <guid>https://dev.to/mridubhatnagar/day-19-third-maximum-number-521k</guid>
      <description>&lt;h5&gt;
  
  
  Background
&lt;/h5&gt;

&lt;p&gt;This problem statement is a part of LeetCode's learn card Introduction to Data Structures Fun with Arrays - 101. Under the sub-heading conclusion.&lt;/p&gt;

&lt;h5&gt;
  
  
  Problem Statement
&lt;/h5&gt;

&lt;p&gt;Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).&lt;/p&gt;

&lt;h5&gt;
  
  
  Example 1
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: [3, 2, 1]

Output: 1

Explanation: The third maximum is 1.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Example 2
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: [1, 2]

Output: 2

Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Example 3
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: [2, 2, 3, 1]

Output: 1

Explanation: Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Solution Approach 1
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Intuitively initial thought that came was to keep calculating max from the array. But, this would also mean every time the maximum element is found. Then we would have to remove the same element from the array. Then again calculate max from the remaining elements in the array. &lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Problems with solution approach 1
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Built-in remove cannot be used for deleting an element because there can be duplicate elements present in the list. Remove will only delete the first occurrence of the element.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Solution Approach 2
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Create a variable maximum and store some arbitrary value in it.&lt;/li&gt;
&lt;li&gt;If the length of the list is greater than 3. Then iterate over the list. If the value of the element is greater than the value of the maximum variable. Store the value of the element in maximum.&lt;/li&gt;
&lt;li&gt;Store some arbitrary value in variable second_max. Iterate over the list. If element value is not equal to the value of the maximum. Then again compare all element values with the value of second_max. If the value of element is greater than the stored value in second_max. Replace.&lt;/li&gt;
&lt;li&gt;The result would be the third max element. &lt;/li&gt;
&lt;li&gt;Else if the length of the list is not greater than 3. Then return a max of the array.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import sys
class Solution:
    def thirdMax(self, nums: List[int]) -&amp;gt; int:
        maximum = -sys.maxsize
        if len(nums) &amp;gt;= 3:
            for element in nums:
                if element &amp;gt; maximum:
                    maximum = element
            second_max = -sys.maxsize
            for element in nums:
                if (element != maximum) and (second_max &amp;lt; element):
                    second_max = element
            third_max = -sys.maxsize
            for element in nums:
                if (element != maximum) and (element != second_max) and (third_max &amp;lt; element):
                    third_max = element
            if (maximum != -sys.maxsize) and (second_max != -sys.maxsize) and (third_max != -sys.maxsize):
                result = third_max
            else:
                result = max(nums)
        else:
            result = max(nums)

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



&lt;h5&gt;
  
  
  Learnings
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Time complexity of the above solution is O(n).&lt;/li&gt;
&lt;li&gt;The initial value of the maximum, second_max, third_max has to be a number that doesn't exist in the original list. Otherwise, it would lead to unexpected output.
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Day-18 Merge Sorted Array</title>
      <dc:creator>Mridu Bhatnagar</dc:creator>
      <pubDate>Thu, 30 Apr 2020 18:18:36 +0000</pubDate>
      <link>https://dev.to/mridubhatnagar/day-18-merge-sorted-array-khm</link>
      <guid>https://dev.to/mridubhatnagar/day-18-merge-sorted-array-khm</guid>
      <description>&lt;h5&gt;
  
  
  Background
&lt;/h5&gt;

&lt;p&gt;This problem statement is a part of the Introduction to Data Structures Fun with arrays-101 on leetcode. Under the sub-heading Inserting the Items into an Array.&lt;/p&gt;

&lt;h5&gt;
  
  
  Problem Statement
&lt;/h5&gt;

&lt;p&gt;Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;The number of elements initialized in nums1 and nums2 are m and n 
  respectively.&lt;/li&gt;
&lt;li&gt;You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Example
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6],       n = 3

Output: [1,2,2,3,5,6]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Solution approach 1
&lt;/h5&gt;

&lt;h6&gt;
  
  
  Case 1: nums1[i] &amp;lt; nums2[j]
&lt;/h6&gt;

&lt;ul&gt;
&lt;li&gt;Compare one element each from both the arrays. If element in 1st array is less then the element in 2nd array. Insert the element to array 3. Increment the counter i of array 1 by 1.&lt;/li&gt;
&lt;/ul&gt;

&lt;h6&gt;
  
  
  Case 2: nums2[j] &amp;lt; nums1[i]
&lt;/h6&gt;

&lt;ul&gt;
&lt;li&gt;If element in array 2 is less than element in array 1. Then insert the element in array 3. Increment the counter j of array 2 to the next element.&lt;/li&gt;
&lt;/ul&gt;

&lt;h6&gt;
  
  
  Case 3: nums1[i] == nums2[j]
&lt;/h6&gt;

&lt;ul&gt;
&lt;li&gt;Insert nums2[j] in array 3. Increment both counter i and j by 1.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a result, array 3 would be the final merge sorted array. &lt;/p&gt;

&lt;h6&gt;
  
  
  Things to observe
&lt;/h6&gt;

&lt;ol&gt;
&lt;li&gt;We are using an extra array. Array 3, that means extra space. As asked in the question. The solution needs to be an in-place solution.&lt;/li&gt;
&lt;li&gt;Notice that the skeleton code below mentions no return type. Hence, even though nums3 is the merged sorted array. It cannot be returned.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -&amp;gt; None:
        """
        Do not return anything, modify nums1 in-place instead.
        """
        i = 0
        j = 0 
        nums3 = []
        while i &amp;lt; m and j &amp;lt; n:
            if nums1[i] &amp;lt; nums2[j]:
                nums3.append(nums1[i])
                i += 1
            elif nums1[i] &amp;gt; nums2[j]:
                nums3.append(nums2[j])
                j += 1
            elif nums1[i] == nums2[j]:
                nums3.append(nums1[i])
                i += 1
                nums3.append(nums2[j])
                j += 1

        while i &amp;lt; m:
            nums3.append(nums1[i])
            i += 1

        while j &amp;lt; n:
            nums3.append(nums2[j])
            j += 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Solution approach 2
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Keep the merging logic same.&lt;/li&gt;
&lt;li&gt;Instead of returning nums3. We can do item assignment. And, copy all the elements of nums3 to nums1.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -&amp;gt; None:
        """
        Do not return anything, modify nums1 in-place instead.
        """
        i = 0
        j = 0 
        nums3 = []
        while i &amp;lt; m and j &amp;lt; n:
            if nums1[i] &amp;lt; nums2[j]:
                nums3.append(nums1[i])
                i += 1
            elif nums1[i] &amp;gt; nums2[j]:
                nums3.append(nums2[j])
                j += 1
            elif nums1[i] == nums2[j]:
                nums3.append(nums1[i])
                i += 1
                nums3.append(nums2[j])
                j += 1

        while i &amp;lt; m:
            nums3.append(nums1[i])
            i += 1

        while j &amp;lt; n:
            nums3.append(nums2[j])
            j += 1


        for i in range(0, len(nums1)):
            nums1[i] = nums3[i]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Learnings
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;We have been able to merge the two sorted array.&lt;/li&gt;
&lt;li&gt;Time complexity - O(n)&lt;/li&gt;
&lt;li&gt;Time complexity of append - O(1)&lt;/li&gt;
&lt;li&gt;As all values of nums1 itself are changed. So, there is no need to return anything. Hence, the solution is accepted.&lt;/li&gt;
&lt;li&gt;List.insert might not be the way out. The reason being it is a built-in function. Secondly, insertion in at defined index adds on to the time complexity.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Things to figure out
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;The solution needs to be an in-place solution. That is no extra memory needs to be used. How to solve it without using any extra space?&lt;/li&gt;
&lt;li&gt;Will optimizing on space complexity lead to increased time complexity?&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Day-17 Binary Search</title>
      <dc:creator>Mridu Bhatnagar</dc:creator>
      <pubDate>Wed, 29 Apr 2020 19:15:26 +0000</pubDate>
      <link>https://dev.to/mridubhatnagar/day-17-binary-search-45op</link>
      <guid>https://dev.to/mridubhatnagar/day-17-binary-search-45op</guid>
      <description>&lt;h5&gt;
  
  
  Background
&lt;/h5&gt;

&lt;p&gt;This problem statement is a part of LeetCode's learn card binary search. Under the sub-heading background.&lt;/p&gt;

&lt;h5&gt;
  
  
  Problem Statement
&lt;/h5&gt;

&lt;p&gt;Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise, return -1.&lt;/p&gt;

&lt;h5&gt;
  
  
  Example 1
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Example 2
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



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

&lt;ol&gt;
&lt;li&gt;You may assume that all elements in nums are unique n will be in the range [1, 10000].&lt;/li&gt;
&lt;li&gt;The value of each element in nums will be in the range [-9999, 9999].&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Solution Approach
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Keep dividing the array into halves.&lt;/li&gt;
&lt;li&gt;left = 0, right = len(array)-1. Mid would be left+right/2.&lt;/li&gt;
&lt;li&gt;Then determine where the element to be searched lies.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def search(self, nums: List[int], target: int) -&amp;gt; int:
        left = 0
        right = len(nums)-1
        while left &amp;lt;= right:
            mid = abs(int((left+right)/2))
            if target &amp;lt; nums[mid]:
                right = mid - 1
            elif target &amp;gt; nums[mid]:
                left = mid + 1
            elif nums[mid] == target:
                 return mid
        return -1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Learnings
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Time complexity is O(logn).&lt;/li&gt;
&lt;li&gt;Take care of the edge conditions. &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Day-16 Reverse words in a string III</title>
      <dc:creator>Mridu Bhatnagar</dc:creator>
      <pubDate>Tue, 28 Apr 2020 19:43:44 +0000</pubDate>
      <link>https://dev.to/mridubhatnagar/day-16-reverse-words-in-a-string-iii-jn3</link>
      <guid>https://dev.to/mridubhatnagar/day-16-reverse-words-in-a-string-iii-jn3</guid>
      <description>&lt;h5&gt;
  
  
  Background
&lt;/h5&gt;

&lt;p&gt;This problem statement is a part of LeetCode's learn card titled Arrays and Strings. Under the sub-heading conclusion.&lt;/p&gt;

&lt;h5&gt;
  
  
  Problem Statement
&lt;/h5&gt;

&lt;p&gt;Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.&lt;/p&gt;

&lt;h5&gt;
  
  
  Example 1
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note: In the string, each word is separated by single space and there will not be any extra space in the string.&lt;/p&gt;

&lt;h5&gt;
  
  
  Solution Approach 1
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Convert the entire string into a list. Split the string based on whitespaces.&lt;/li&gt;
&lt;li&gt;Iterate over the list. Reverse each word. For reverse using slicing.&lt;/li&gt;
&lt;li&gt;Reconstruct the entire string. This time every word would be reversed.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def reverseWords(self, s: str) -&amp;gt; str:
        s = s.split()
        s1 = ''
        for x in range(0, len(s)):
            if x == len(s) - 1:
                s1 += s[x][::-1]
            else:
                s1 += s[x][::-1] + ' '
        return s1    
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Things to notice
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;We are already using a for loop to iterate over list. Time complexity - O(n).&lt;/li&gt;
&lt;li&gt;We are reversing the string using slicing.&lt;/li&gt;
&lt;li&gt;Extra space is used here as well. As a new string is being constructed instead of making changes in-place.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Solution approach 2
&lt;/h5&gt;

&lt;p&gt;This is probably the longer route to reach the solution.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Split the string into words. Split using whitespaces.&lt;/li&gt;
&lt;li&gt;Iterate over the list. Convert each word into letters. &lt;/li&gt;
&lt;li&gt;Then do a reverse based on two-pointer approach. Once, the reverse is done to join those letters to form string again.&lt;/li&gt;
&lt;li&gt;Then again convert the list of reversed words to form the output string.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def reverseWords(self, s: str) -&amp;gt; str:
        L = []
        string = s.split()
        for word in string:
            y = list(word)
            start = 0
            end = len(y) - 1
            while start &amp;lt; end:
                temp = y[start]
                y[start] = y[end]
                y[end] = temp
                start += 1
                end -= 1
            new_string = ''.join(y)
            L.append(new_string)
        return ' '.join(L)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Things to notice
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;We are using extra space. As we are creating a new list in the process.&lt;/li&gt;
&lt;li&gt;There is a while loop inside of a for loop. This adds on to the time complexity. &lt;/li&gt;
&lt;li&gt;That whole reverse logic was written to avoid using list.reverse().&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Some follow up questions
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;What is the time complexity of reversing a string using slicing s[::-1]? (see solution 1)&lt;/li&gt;
&lt;li&gt;As we are using slicing inside a for loop. Will the overall time-complexity of code be O(nk)? (refer to solution 1)&lt;/li&gt;
&lt;li&gt;In solution approach 2. We are using a while loop inside of a for loop. This would lead to time complexity O(nk) or O(n^2)?
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Day-15 Reverse words in a string</title>
      <dc:creator>Mridu Bhatnagar</dc:creator>
      <pubDate>Mon, 27 Apr 2020 10:24:02 +0000</pubDate>
      <link>https://dev.to/mridubhatnagar/day-15-reverse-words-in-a-string-3foj</link>
      <guid>https://dev.to/mridubhatnagar/day-15-reverse-words-in-a-string-3foj</guid>
      <description>&lt;h5&gt;
  
  
  Background
&lt;/h5&gt;

&lt;p&gt;This problem statement is a part of LeetCode's card Arrays and Strings. Under the sub-heading Conclusion.&lt;/p&gt;

&lt;h5&gt;
  
  
  Problem Statement
&lt;/h5&gt;

&lt;p&gt;Given an input string, reverse the string word by word.&lt;/p&gt;

&lt;h5&gt;
  
  
  Example 1
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: "the sky is blue"
Output: "blue is sky the"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Example 2
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: "  hello world!  "
Output: "world! hello"
Explanation: Your reversed string should not contain leading or trailing spaces.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Example 3
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: "a good   example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



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

&lt;ol&gt;
&lt;li&gt;A word is defined as a sequence of non-space characters.&lt;/li&gt;
&lt;li&gt;Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.&lt;/li&gt;
&lt;li&gt;You need to reduce multiple spaces between two words to a single 
  space in the reversed string.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Solution Approach 1
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Replace double space with single space. &lt;/li&gt;
&lt;li&gt;Remove leading and trailing spaces.&lt;/li&gt;
&lt;li&gt;Reverse.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def reverseWords(self, s: str) -&amp;gt; str:
        s.replace("  ", " ").rstrip().lstrip()
        string = s.split()
        string.reverse()
        return ' '.join(string)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Learnings and takeaways from the above snippet
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;We have hardcoded 2 spaces. Hardcoding in programming should never be done.&lt;/li&gt;
&lt;li&gt;If count of consecutive spaces is increased. The code would fail.&lt;/li&gt;
&lt;li&gt;Using built-in method reverse() could appear as a shortcut. &lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Updated solution overcoming the shortcomings listed above.
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;s.lstrip().rstrip().split(). Takes care of multiple spaces, leading spaces, trailing space.&lt;/li&gt;
&lt;li&gt;replace reverse() with implementation of reverse.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def reverseWords(self, s: str) -&amp;gt; str:
        string = s.lstrip().rstrip().split()
        start = 0
        end = len(string) - 1
        while start &amp;lt; end:
            temp = string[start]
            string[start] = string[end]
            string[end] = temp
            start += 1
            end -= 1

        return ' '.join(string)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Ps:&lt;br&gt;
Last section of the post. Which starts from learnings from the shortcomings of the above snippet is updated after seeing the first comment.&lt;/p&gt;

</description>
      <category>python</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Day-15 Array Partition I</title>
      <dc:creator>Mridu Bhatnagar</dc:creator>
      <pubDate>Mon, 27 Apr 2020 05:37:58 +0000</pubDate>
      <link>https://dev.to/mridubhatnagar/day-15-array-partition-i-h8i</link>
      <guid>https://dev.to/mridubhatnagar/day-15-array-partition-i-h8i</guid>
      <description>&lt;h5&gt;
  
  
  Background
&lt;/h5&gt;

&lt;p&gt;This problem statement was a part of LeetCode's learn card: Array and Strings. Under the sub-heading Two pointer technique.&lt;/p&gt;

&lt;h5&gt;
  
  
  Problem Statement
&lt;/h5&gt;

&lt;p&gt;Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.&lt;/p&gt;

&lt;h5&gt;
  
  
  Example 1
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: [1,4,3,2]

Output: 4
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



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

&lt;ol&gt;
&lt;li&gt;n is a positive integer, which is in the range of [1, 10000].&lt;/li&gt;
&lt;li&gt;All the integers in the array will be in the range of [-10000, 
  10000].&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Solution Approach 1
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Pick some random examples. And, try calculating the sum of pairs manually for different combination of integers to see if there is some pattern.&lt;/li&gt;
&lt;li&gt;Based on step 1. One conclusion that can be drawn is pairing consecutive small numbers together, followed by pairing large numbers. Leads to maximum sum.&lt;/li&gt;
&lt;li&gt;Now, 2 achieve step 2. We can sort the given array.&lt;/li&gt;
&lt;li&gt;Once the array is sorted. We can have 2 variables. One variable points to the current element. Another element points to the next element.&lt;/li&gt;
&lt;li&gt;The current element and the next element form a pair.&lt;/li&gt;
&lt;li&gt;Size of array is 2n. Number of pairs are n is mentioned in the problem statement. Based on this we can break the loop.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def arrayPairSum(self, nums: List[int]) -&amp;gt; int:
        current=0
        next_element = current+1
        total = 0
        counter = 0     
        nums.sort()
        while current &amp;lt; next_element:
            total += min(nums[current], nums[next_element])
            current  = next_element + 1
            next_element = current+ 1
            counter += 1
            if counter == int(len(nums)/2):
                break
        return total
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Learnings and obervation from solution approach 1
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Time complexity of list.sort() is O(nlogn). Then we are iterating over the list. And calling built-in method min. Min has its own complexity. This in turn would increase the overall time complexity.&lt;/li&gt;
&lt;li&gt;We are already sorting the array. And then forming pairs. So, always the minimum element would be the current element. Hence, no need to again call built-in min method to find the minimum of the two numbers.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Updated code based on takeaways from solution approach 1
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def arrayPairSum(self, nums: List[int]) -&amp;gt; int:
        current=0
        next_element = current+1
        total = 0
        counter = 0
        nums.sort()
        while current &amp;lt; next_element:
            total += nums[current]
            current  = next_element + 1
            next_element = current+ 1
            counter += 1
            if counter == int(len(nums)/2):
                break
        return total
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the above solution, we are breaking the loop when count of pairs is equal to half the length of the array. Another approach could be to scrap this whole part off. Instead, put this condition. The index of current element should always be less the length of array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def arrayPairSum(self, nums: List[int]) -&amp;gt; int:
        current=0
        next_element = current+1
        total = 0
        nums.sort()
        while current &amp;lt; next_element and current &amp;lt; len(nums)-1:
            total += nums[current]
            current  = next_element + 1
            next_element = current+ 1
        return total
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Learnings
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Time complexity - O(nlogn).&lt;/li&gt;
&lt;li&gt;Got rid of unnecessary min() built-in function.&lt;/li&gt;
&lt;li&gt;Two pointer technique.&lt;/li&gt;
&lt;li&gt;List.sort() method is an in-place sort. Using sorted() for sorting would have returned a new list. That, however, was not needed.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Day-14 Reverse String</title>
      <dc:creator>Mridu Bhatnagar</dc:creator>
      <pubDate>Sun, 26 Apr 2020 12:00:35 +0000</pubDate>
      <link>https://dev.to/mridubhatnagar/day-14-reverse-string-4hld</link>
      <guid>https://dev.to/mridubhatnagar/day-14-reverse-string-4hld</guid>
      <description>&lt;h5&gt;
  
  
  Background
&lt;/h5&gt;

&lt;p&gt;This problem statement is a part of Leetcode's learning card Array and Strings. Under the sub-heading two pointer technique.&lt;/p&gt;

&lt;h5&gt;
  
  
  Problem Statement
&lt;/h5&gt;

&lt;p&gt;Write a function that reverses a string. The input string is given as an array of characters char[].&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;p&gt;You may assume all the characters consist of printable ASCII characters.&lt;/p&gt;

&lt;h5&gt;
  
  
  Example 1
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Example 2
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Solution Approach 1
&lt;/h5&gt;

&lt;p&gt;Use python built-in methods. There are two ways of doing the same. Suppose s in the initial list. Then s.reverse() could be done or s[::-1] can be done.&lt;/p&gt;

&lt;p&gt;Had reaching the solution by using the built-in methods been the aim. Probably the whole exercise would not have been worth the effort.&lt;/p&gt;

&lt;h5&gt;
  
  
  Solution Approach 2
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Iterate over the list.&lt;/li&gt;
&lt;li&gt;Swap the first element with the last element.&lt;/li&gt;
&lt;li&gt;The list would be completely reversed when we are at the mid of the list. &lt;/li&gt;
&lt;li&gt;When the index of the current element is equal to mid of the list. Break.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def reverseString(self, s: List[str]) -&amp;gt; None:
        """
        Do not return anything, modify s in-place instead.
        """
        for x in range(0, len(s)):
            if x == int(len(s)/2):
                break
            else:
                temp = s[x]
                s[x] = s[len(s)-x-1]
                s[len(s)-x-1]=temp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Learnings
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Time complexity - O(n), Space complexity - O(1).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A more elegant approach&lt;/p&gt;

&lt;h5&gt;
  
  
  Solution approach 3
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Keep two variables. One at the starting of the list at index 0. Another at the end of the list. Index len(L)-1.&lt;/li&gt;
&lt;li&gt;Keep swapping the elements until both the variables reach the mid of the list.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def reverseString(self, s: List[str]) -&amp;gt; None:
        """
        Do not return anything, modify s in-place instead.
        """
        start = 0
        end = len(s) - 1
        while start &amp;lt; end:
            temp = s[start]
            s[start] = s[end]
            s[end] = temp
            start += 1
            end -= 1    
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  Learnings
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;The solution is still time complexity O(n) and space complexity O(1).&lt;/li&gt;
&lt;li&gt;Two pointer technique. Though in python there is no concept of pointer as such.&lt;/li&gt;
&lt;li&gt;Built-in reverse method is also implemented the same way.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Day-13 Find Pivot Index</title>
      <dc:creator>Mridu Bhatnagar</dc:creator>
      <pubDate>Fri, 24 Apr 2020 19:42:23 +0000</pubDate>
      <link>https://dev.to/mridubhatnagar/day-13-find-pivot-index-27lh</link>
      <guid>https://dev.to/mridubhatnagar/day-13-find-pivot-index-27lh</guid>
      <description>&lt;h5&gt;
  
  
  Background
&lt;/h5&gt;

&lt;p&gt;This problem statement is a part of Leetcode's learning card Arrays and Strings. Under the sub-heading Introduction to Array.&lt;/p&gt;

&lt;h5&gt;
  
  
  Problem Statement
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: 
nums = [1, 7, 3, 6, 5, 6]
Output: 3
Explanation: 
The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
Also, 3 is the first index where this occurs.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: 
nums = [1, 2, 3]
Output: -1
Explanation: 
There is no index that satisfies the conditions in the problem statement.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



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

&lt;ol&gt;
&lt;li&gt;The length of nums will be in the range [0, 10000].&lt;/li&gt;
&lt;li&gt;Each element nums[i] will be an integer in the range [-1000, 1000].&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Solution Approach 1
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Reading the problem statement intuitive method that struck was to slice the list. And keep calculating the sum of those sliced sub-lists. When sum of the elements to the left-hand side of the element and right-hand side of the element matches. Return the index.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Problems with Approach 1
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;This would result in an unoptimized solution. Slicing has its own time complexity depending on the length of the sublist. Overall complexity would have become quadratic.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Solution Approach 2
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Take a pen and paper and mathematically try observing the pattern. &lt;/li&gt;
&lt;li&gt;Calculate the total of all elements present in the list.&lt;/li&gt;
&lt;li&gt;Then compute the sum of all elements to the left-hand side of the current index. Store this sum for each element in a new list.&lt;/li&gt;
&lt;li&gt;Sum of right-hand side would be. 
right_hand_side = Total - value of element at current index - sum of left hand side elements at same index.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def pivotIndex(self, nums: List[int]) -&amp;gt; int:
        total = 0
        result = -1
        for x in range(0, len(nums)):
            total += nums[x]

        element_sum = 0
        left_side_sum = [0] # Sum of elements to the left of value
        for i in range(1, len(nums)):
            element_sum += nums[i-1]
            left_side_sum.append(element_sum)

        for index in range(0, len(nums)):
            right_side_sum = total - nums[index] - left_side_sum[index]
            if right_side_sum == left_side_sum[index]:
                result = index
                break

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



&lt;h5&gt;
  
  
  Learnings and Takeaways
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Time complexity - O(n), Space complexity - O(n)&lt;/li&gt;
&lt;li&gt;Time complexity of append - O(1)&lt;/li&gt;
&lt;li&gt;The first element of left_side_sum is initialized to 0 because there is no value to the left-hand side of leftmost element present in the list. &lt;/li&gt;
&lt;/ol&gt;

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