<?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: late_riser</title>
    <description>The latest articles on Forem by late_riser (@late_riser).</description>
    <link>https://forem.com/late_riser</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%2F127694%2Fff1b821d-07fd-49f0-a3f5-a189dc1a0fea.png</url>
      <title>Forem: late_riser</title>
      <link>https://forem.com/late_riser</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/late_riser"/>
    <language>en</language>
    <item>
      <title>Leetcode 713. Subarray Product Less Than K</title>
      <dc:creator>late_riser</dc:creator>
      <pubDate>Tue, 09 Aug 2022 21:26:57 +0000</pubDate>
      <link>https://forem.com/late_riser/leetcode-713-subarray-product-less-than-k-3k2n</link>
      <guid>https://forem.com/late_riser/leetcode-713-subarray-product-less-than-k-3k2n</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O4C9j7eh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/trpw7m0833wmd1in89g1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O4C9j7eh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/trpw7m0833wmd1in89g1.png" alt="Leetcode 713. Subarray Product Less Than K" width="880" height="708"&gt;&lt;/a&gt;Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [10,5,2,6], k = 100
Output: 8
Explanation: The 8 subarrays that have product less than 100 are:
[10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]
Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [1,2,3], k = 0
Output: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Constraints&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 &amp;lt;= nums.length &amp;lt;= 3 * 104
1 &amp;lt;= nums[i] &amp;lt;= 1000
0 &amp;lt;= k &amp;lt;= 106
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Editorial&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The simplest way to think of this problem is taking cumulative product from an index up to the end of the array. Then check if any of those &lt;code&gt;products &amp;lt; k&lt;/code&gt; or not. We will have to do this for each index. Then, for any subarray &lt;code&gt;product &amp;lt; k&lt;/code&gt;, we can count the number of subarrays and find the result by adding up those counts.&lt;/p&gt;

&lt;p&gt;But this approach will cost us O(N2) [O N square] runtime which is not efficient and we don't want that! So, let's not waste time on it. The better approach to solve this problem is to &lt;a href="https://foolishhungry.com/subarray-product-less-than-k/"&gt;.... read more&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Editorial source: &lt;a href="https://foolishhungry.com"&gt;foolishhungry blog&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Search in Rotated Sorted Array (Leetcode 33)</title>
      <dc:creator>late_riser</dc:creator>
      <pubDate>Sat, 06 Aug 2022 03:04:00 +0000</pubDate>
      <link>https://forem.com/late_riser/search-in-rotated-sorted-array-leetcode-33-3jdh</link>
      <guid>https://forem.com/late_riser/search-in-rotated-sorted-array-leetcode-33-3jdh</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lh8TTIaz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ypexqrk0tdf163wmy80x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lh8TTIaz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ypexqrk0tdf163wmy80x.png" alt="Search in Rotated Sorted Array (Leetcode 33)" width="810" height="368"&gt;&lt;/a&gt;&lt;strong&gt;Problem&lt;/strong&gt;&lt;br&gt;
There is an integer array nums sorted in ascending order (with distinct values).&lt;/p&gt;

&lt;p&gt;Prior to being passed to your function, nums is possibly rotated at an unknown pivot index &lt;code&gt;k (1 &amp;lt;= k &amp;lt; nums.length)&lt;/code&gt; such that the resulting array is &lt;code&gt;nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]&lt;/code&gt; &lt;code&gt;(0-indexed)&lt;/code&gt;. For example, &lt;code&gt;[0,1,2,4,5,6,7]&lt;/code&gt; might be rotated at pivot index 3 and become &lt;code&gt;[4,5,6,7,0,1,2]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.&lt;/p&gt;

&lt;p&gt;You must write an algorithm with O(logn) runtime complexity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example 3:

Input: nums = [1], target = 0
Output: -1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Source: Leetcode&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Constraints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 &amp;lt;= nums.length &amp;lt;= 5000
-104 &amp;lt;= nums[i] &amp;lt;= 104
All values of nums are unique.
nums is an ascending array that is possibly rotated.
-104 &amp;lt;= target &amp;lt;= 104
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How to solve this interesting problem?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If a sorted array is not rotated, it is easy to find a target value using Binary Search algorithm. Because, we know the target value will be somewhere between the first and the last element of the array. But if we rotate the array, the array no longer remains sorted, right? &lt;/p&gt;

&lt;p&gt;&lt;a href="https://foolishhungry.com/search-in-rotated-sorted-array/"&gt;Read more in our blog Foolish Hungry dot com ... :)&lt;/a&gt;&lt;/p&gt;

</description>
      <category>binarysearch</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>foolishhungry</category>
    </item>
    <item>
      <title>Leetcode 523. Continuous Subarray Sum</title>
      <dc:creator>late_riser</dc:creator>
      <pubDate>Tue, 26 Jul 2022 17:56:37 +0000</pubDate>
      <link>https://forem.com/late_riser/leetcode-523-continuous-subarray-sum-2h9g</link>
      <guid>https://forem.com/late_riser/leetcode-523-continuous-subarray-sum-2h9g</guid>
      <description>&lt;p&gt;Given an integer array nums and an integer k, return true if nums has a continuous subarray of size at least two whose elements sum up to a multiple of k, or false otherwise.&lt;/p&gt;

&lt;p&gt;An integer x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k.&lt;/p&gt;

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

&lt;p&gt;Input: nums = [23,2,4,6,7], k = 6&lt;br&gt;
Output: true&lt;br&gt;
Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.&lt;br&gt;
Example 2:&lt;/p&gt;

&lt;p&gt;Input: nums = [23,2,6,4,7], k = 6&lt;br&gt;
Output: true&lt;br&gt;
Explanation: [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.&lt;br&gt;
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.&lt;br&gt;
Example 3:&lt;/p&gt;

&lt;p&gt;Input: nums = [23,2,6,4,7], k = 13&lt;br&gt;
Output: false&lt;/p&gt;

&lt;p&gt;Constraints:&lt;/p&gt;

&lt;p&gt;1 &amp;lt;= nums.length &amp;lt;= 105&lt;br&gt;
0 &amp;lt;= nums[i] &amp;lt;= 109&lt;br&gt;
0 &amp;lt;= sum(nums[i]) &amp;lt;= 231 - 1&lt;br&gt;
1 &amp;lt;= k &amp;lt;= 231 - 1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Here is the link of our blog to the solution of this problem:&lt;br&gt;
&lt;a href="https://foolishhungry.com/continuous-subarray-sum/"&gt;continuous subarray sum - FoolishHungry Blog&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Leetcode-42: Trapping Rain Water</title>
      <dc:creator>late_riser</dc:creator>
      <pubDate>Thu, 14 Oct 2021 18:13:08 +0000</pubDate>
      <link>https://forem.com/late_riser/rain-on-me-leetcode-42-trapping-rain-water-5bfh</link>
      <guid>https://forem.com/late_riser/rain-on-me-leetcode-42-trapping-rain-water-5bfh</guid>
      <description>&lt;p&gt;This article is taken from &lt;a href="https://foolishhungry.com" rel="noopener noreferrer"&gt;FOOLISH HUNGRY&lt;/a&gt; blog.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problem&lt;/strong&gt; &lt;br&gt;
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.leetcode.com%2Fuploads%2F2018%2F10%2F22%2Frainwatertrap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.leetcode.com%2Fuploads%2F2018%2F10%2F22%2Frainwatertrap.png"&gt;&lt;/a&gt;&lt;br&gt;
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]&lt;br&gt;
Output: 6&lt;br&gt;
Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.&lt;/p&gt;

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

&lt;p&gt;Input: height = [4,2,0,3,2,5]&lt;br&gt;
Output: 9&lt;br&gt;
Constraints:&lt;/p&gt;

&lt;p&gt;n == height.length&lt;br&gt;
1 &amp;lt;= n &amp;lt;= 2 * 104&lt;br&gt;
0 &amp;lt;= height[i] &amp;lt;= 105&lt;br&gt;
Source: &lt;a href="https://leetcode.com/problems/trapping-rain-water/" rel="noopener noreferrer"&gt;Leetcode&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ALERT, ALERT, ALERT !!!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you have come to see the solution before trying to solve it yourself, this really won’t help you, promise! If that is the case, we will recommend, please go back and first try it yourself. If you can’t solve it after (at least)an hour of thinking and trying, please come back! 🙂 You can solve it &lt;a href="https://leetcode.com/problems/trapping-rain-water/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Consider we have only three elevations named A, B and C with heights [10, 5, 20]. If we continue to pour water on elevation 5, it will remain on top of 5 until it reaches 10. Anything above 10 will be drained away as show below.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi0.wp.com%2Ffoolishhungry.com%2Fwp-content%2Fuploads%2F2021%2F10%2Ftrapping_intro.png%3Fw%3D1189%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi0.wp.com%2Ffoolishhungry.com%2Fwp-content%2Fuploads%2F2021%2F10%2Ftrapping_intro.png%3Fw%3D1189%26ssl%3D1"&gt;&lt;/a&gt;&lt;br&gt;
So, for B, the effective height of water is the minimum height of all the maximum heights that belong to both sides of B.&lt;/p&gt;

&lt;p&gt;In other words, for any elevation,&lt;/p&gt;

&lt;p&gt;effective height for B = min ( max(all heights to the left of B), max(all heights to the right of B))&lt;/p&gt;

&lt;p&gt;Before jumping into the code, let’s do a little simulation of the idea of effective height and trapped rain water calculation.&lt;/p&gt;

&lt;p&gt;Consider we have 5 elevation points with heights [10, 0, 5, 0, 20] as below.&lt;/p&gt;

&lt;p&gt;(Sorry for the drawing mistake, height of E doesn’t look proportionate to A or C 😛 That won’t hurt understanding the solution though )&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi1.wp.com%2Ffoolishhungry.com%2Fwp-content%2Fuploads%2F2021%2F10%2Ftrapping_rain_water1.png%3Fw%3D892%26ssl%3D1%3Cbr%3E%250A" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi1.wp.com%2Ffoolishhungry.com%2Fwp-content%2Fuploads%2F2021%2F10%2Ftrapping_rain_water1.png%3Fw%3D892%26ssl%3D1%3Cbr%3E%250A"&gt;&lt;/a&gt;&lt;br&gt;
Let’s start with elevation point A. There is no point left to it, so, the max height to A is the height of itself, 10. There are 4 points right to A with heights [0, 5, 0, 20]. The max is 20. So, the minimum between left max and right max is 10. So, the effective height is 10 – 10 = 0. That means, elevation point A cannot trap any water.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi1.wp.com%2Ffoolishhungry.com%2Fwp-content%2Fuploads%2F2021%2F10%2Ftrapping_rain_water2.png%3Fresize%3D1568%252C1608%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi1.wp.com%2Ffoolishhungry.com%2Fwp-content%2Fuploads%2F2021%2F10%2Ftrapping_rain_water2.png%3Fresize%3D1568%252C1608%26ssl%3D1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the same way, max height to the left of B is 10 and to the right of B is 20. Effective height is min(10, 20) = 10 and B can trap 10 – 0 = 10 units of water.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi2.wp.com%2Ffoolishhungry.com%2Fwp-content%2Fuploads%2F2021%2F10%2Ftrapping_rain_water3.png%3Fresize%3D1568%252C1608%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi2.wp.com%2Ffoolishhungry.com%2Fwp-content%2Fuploads%2F2021%2F10%2Ftrapping_rain_water3.png%3Fresize%3D1568%252C1608%26ssl%3D1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next three images show with calculation how much water C, D and E can trap.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi2.wp.com%2Ffoolishhungry.com%2Fwp-content%2Fuploads%2F2021%2F10%2Ftrapping_rain_water4.png%3Fresize%3D1568%252C1608%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi2.wp.com%2Ffoolishhungry.com%2Fwp-content%2Fuploads%2F2021%2F10%2Ftrapping_rain_water4.png%3Fresize%3D1568%252C1608%26ssl%3D1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi1.wp.com%2Ffoolishhungry.com%2Fwp-content%2Fuploads%2F2021%2F10%2Ftrapping_rain_water5.png%3Fresize%3D1568%252C1608%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi1.wp.com%2Ffoolishhungry.com%2Fwp-content%2Fuploads%2F2021%2F10%2Ftrapping_rain_water5.png%3Fresize%3D1568%252C1608%26ssl%3D1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi1.wp.com%2Ffoolishhungry.com%2Fwp-content%2Fuploads%2F2021%2F10%2Ftrapping_rain_water6.png%3Fresize%3D1568%252C1608%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi1.wp.com%2Ffoolishhungry.com%2Fwp-content%2Fuploads%2F2021%2F10%2Ftrapping_rain_water6.png%3Fresize%3D1568%252C1608%26ssl%3D1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, the total unit of water trapped will be 0 + 10 + 5 + 10 + 0 = 25.&lt;/p&gt;

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

&lt;p&gt;To find the maximum height to the left and right of an elevation point, we can loop from that point and go to left once and go to right again. So, this will cause a runtime complexity of O(N2). However, we will get O(1) space complexity since we are not using any extra spaces.&lt;/p&gt;

&lt;p&gt;Runtime complexity can easily be improved to O(N) if we apply simple Dynamic Programming and store the max value so far from the beginning of the array in another array called left. Also, we can store the same starting from the end of the array in another array called right. Then each time we want to know the max values of left and right of an elevation point, we will simply check the left and right array.&lt;/p&gt;

&lt;p&gt;So, formally, the algorithm is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start from i = 0 to end and store max value in left[ ] array. Do Something like left [i] = max(left[i – 1], height [i] )&lt;/li&gt;
&lt;li&gt;Start from i = end to 0 and store max value in left[ ] array. Do Something like right [i] = max(right[i + 1], height [i] )&lt;/li&gt;
&lt;li&gt;Now, loop through the height array and take effective height as, effective_height = min (left[i], right[I]). Add trapped water unit.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int trap(vector&amp;lt;int&amp;gt;&amp;amp; height) {
        int N = height.size();

        vector&amp;lt;int&amp;gt; left(N, 0);
        vector&amp;lt;int&amp;gt; right(N, 0);

        left[0] = height[0];
        right[N - 1] = height[N - 1];

        for(int i = 1; i &amp;lt; N; i++){
            left[i] = max(left[i - 1], height[i]);
            right[N - i - 1] = max(height[N - i - 1], right[N - i]);
        }

        int ans = 0;

        for(int i = 0; i &amp;lt; N; i++){
            ans += min(left[i], right[i]) - height[i];
        }

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

&lt;/div&gt;



&lt;p&gt;Happy Coding!! :D&lt;/p&gt;

&lt;p&gt;This article is taken from &lt;a href="https://foolishhungry.com" rel="noopener noreferrer"&gt;FOOLISH HUNGRY&lt;/a&gt; blog.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>algorithms</category>
      <category>dp</category>
    </item>
    <item>
      <title>30-Day LeetCoding Challenge (Day-1)</title>
      <dc:creator>late_riser</dc:creator>
      <pubDate>Thu, 02 Apr 2020 23:22:41 +0000</pubDate>
      <link>https://forem.com/late_riser/30-day-leetcoding-challenge-day-1-59d1</link>
      <guid>https://forem.com/late_riser/30-day-leetcoding-challenge-day-1-59d1</guid>
      <description>&lt;p&gt;&lt;em&gt;&lt;strong&gt;Background:&lt;/strong&gt; We all are home for the current pandemic situation. Leetcode has taken this opportunity to throw a challenge for the users (which is positive, I think).&lt;br&gt;
The challenge is titled &lt;a href="https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/"&gt;30-Day LeetCoding Challenge&lt;/a&gt;. I have started it since yesterday and wish to complete it and write blog post for each problem I solve in a day. This is just to keep myself motivated to stay at home and give something back to the community. As a disclaimer, I have taken problem statements and examples from leetcode. Without any more introduction, I will jump into the problem, how I solved it and link to my code. If you find this helpful, give me a poke!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Title:&lt;/strong&gt; &lt;a href="https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/528/week-1/3283/"&gt;Single Number&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Description:&lt;/strong&gt; Given a non-empty array of integers, every element appears twice except for one. Find that single one. Your algorithm should have a linear run time complexity i.e. O(n). Could you implement it without using extra memory?&lt;/p&gt;

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

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: [2,2,1]
Output: 1 (because 1 the only number that is appearing once)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



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

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: [4,1,2,1,2]
Output: 4 (because 1 the only number that is appearing once)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; I solved this problem using bitwise XOR. The idea is, when you XOR a number with itself, you will get 0. For example, 2441139 ⊗ XOR 2441139 = 0.&lt;br&gt;
Now, say, we have 5 numbers like 3, 4, 9, 3, 9. If we XOR these numbers from begin to end, we will end up XORing 3 twice and 9 twice. Both of them will make the result 0 and 4 will remain intact since any number XOR with 0 ends up to that number. (If you don’t get the idea still, I would suggest you to do the calculations on paper, you should get it for sure!)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt; Here is my C++ code for it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    int singleNumber(vector&amp;lt;int&amp;gt;&amp;amp; nums) {
        int sum = 0;
        for(auto x: nums)
            sum ^= x;
        return sum;

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



&lt;p&gt;If you want to learn more about bitwise operation, &lt;a href="https://www.hackerearth.com/practice/basic-programming/bit-manipulation/basics-of-bit-manipulation/tutorial/"&gt;this link&lt;/a&gt; is a good resource in my opinion.&lt;/p&gt;

&lt;p&gt;If you find this helpful or want me to improve my explanation(I think that’s more probable) or want to shout out of anger because of my crappy writing, you can post your comments :).&lt;/p&gt;

&lt;p&gt;Stay safe at home! Happy Coding! :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/sadi_late_riser"&gt;Follow me&lt;/a&gt; on twitter.&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>30daychallenge</category>
      <category>bitwisexor</category>
      <category>cpp</category>
    </item>
  </channel>
</rss>
