<?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: Yash</title>
    <description>The latest articles on DEV Community by Yash (@yashdevs).</description>
    <link>https://dev.to/yashdevs</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%2F1935471%2Fd8a85658-d4b3-4b84-9615-35233e599e86.png</url>
      <title>DEV Community: Yash</title>
      <link>https://dev.to/yashdevs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yashdevs"/>
    <language>en</language>
    <item>
      <title>Skip Lists - Advanced DSA</title>
      <dc:creator>Yash</dc:creator>
      <pubDate>Fri, 16 Aug 2024 21:21:04 +0000</pubDate>
      <link>https://dev.to/yashdevs/skip-lists-advanced-dsa-1nm6</link>
      <guid>https://dev.to/yashdevs/skip-lists-advanced-dsa-1nm6</guid>
      <description>&lt;p&gt;Redis which is a popular fast in-memory database uses many optimization techniques to speed up the retrievals, one of them is the concept of using skip lists, in simple terms skip lists are augmented linked lists for faster search, as you can skip through portions of the list which don't contain the data you're looking for.&lt;/p&gt;

&lt;p&gt;The data is stored in the form of linked lists with an additional information pertaining to an abstract concept of &lt;code&gt;lanes&lt;/code&gt;, each node contains 3 data members: &lt;code&gt;value&lt;/code&gt;, &lt;code&gt;next&lt;/code&gt; and &lt;code&gt;down&lt;/code&gt; and the data has a layer of abstraction called lanes, there are 2 types of lanes: normal lane, which is the original linked list and express lanes which are the abstractions, here is a representation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxdg5wofgsjy6avygfzc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxdg5wofgsjy6avygfzc.png" alt="Image description" width="800" height="114"&gt;&lt;/a&gt;&lt;br&gt;
// Image courtesy: &lt;a href="https://www.geeksforgeeks.org/skip-list/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/skip-list/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code for searching in a linked list is really neat:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SkipNode* search(int key) {
    curr = head;
    while(curr != nullptr) {
        while(curr -&amp;gt; right != nullptr &amp;amp;&amp;amp; curr -&amp;gt; right -&amp;gt; value &amp;lt;= key) {
            curr = curr -&amp;gt; right;
        }
        if(curr -&amp;gt; val == key) break;
        curr = curr -&amp;gt; down;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>algorithms</category>
    </item>
    <item>
      <title>Python notes/tricks/lessons/nuances</title>
      <dc:creator>Yash</dc:creator>
      <pubDate>Fri, 16 Aug 2024 21:14:01 +0000</pubDate>
      <link>https://dev.to/yashdevs/python-tricks-5b0c</link>
      <guid>https://dev.to/yashdevs/python-tricks-5b0c</guid>
      <description>&lt;p&gt;I made a C++ tricks post and I wanted to write about a lot more of tricks that I know, since most of them are in Python and JS even though I have coded in C++ a lot more but anyways here they are, I'll start with trivial things about basic Python syntax but there are some things that are not known to beginners, which I've marked with a star ⭐️:&lt;/p&gt;

&lt;h1&gt;
  
  
  1. Lists
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;An ordered collection of items data structure&lt;/li&gt;
&lt;li&gt;Can have multiple types&lt;/li&gt;
&lt;li&gt;&lt;code&gt;aa = ["apple", 'd', 3, 9.3]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Initialization:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;a = [1, 2]&lt;/li&gt;
&lt;li&gt;a = list()&lt;/li&gt;
&lt;li&gt;a = list([2, 3])&lt;/li&gt;
&lt;li&gt;&lt;code&gt;[0]*5&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;concat_list = list1 + list2&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Accessing elements ⭐️
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;0 ... n-1 and -n ... -1&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Functions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;len(list1)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;a.append(value)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;a.insert(index, value)&lt;/code&gt;: inserts value at index and pushes everything to the right&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;a.pop()&lt;/code&gt;: returns the end value and removes it&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;a.remove(value)&lt;/code&gt;: if the value is not present you will get a &lt;code&gt;ValueError&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;a.clear()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;a.reverse()&lt;/code&gt;: reverses in place&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;a.sort()&lt;/code&gt;: modifies orginal&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;b = sorted(a)&lt;/code&gt;: doesn't modify original&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Slicing ⭐️
&lt;/h2&gt;

&lt;p&gt;An easy way to access subarrays of a list&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;slicedList = a[1:5]&lt;/code&gt;: start at index 1 and stop at index 5, index 5 is excluded&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;slicedList = a[:3]&lt;/code&gt;: from 0 ... 2&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;slicedList = a[1:3]&lt;/code&gt;: from 1 to 2&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;slicedList = a[:]&lt;/code&gt;: duplicate the entire list&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;slicedList = myList[::2]&lt;/code&gt;: step index, every second item, start with first, basically &lt;code&gt;i=0;i&amp;lt;n;i+=2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;slicedList = myList[::-1]&lt;/code&gt;: nice way to reverse a list&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Copying a list
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Shallow copy
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;a = [1, 2, 3]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;b = a&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Modifying one also modifies the other since both are same memory allocations&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Deep Copy ⭐️
&lt;/h3&gt;

&lt;p&gt;3 ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;b = a.copy()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;b = list(a)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt; &lt;code&gt;b = a[:]&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  List Comprehension
&lt;/h2&gt;

&lt;p&gt;I will start off with some basic list comprehensions, but they'll quickly get less trivial.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;values = [x+1 for x in range(10)] -&amp;gt; [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  - &lt;code&gt;values = [x for x in range(51) if x % 2 == 0]&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;word&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;word&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="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;y&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="c1"&gt;# OR, a nicer way is ⭐️
&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="n"&gt;string&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;string&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;a&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;string&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="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;y&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Nested List Comprehension ⭐️
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Flattening a list of lists, or a 2D matrix&lt;/code&gt;&lt;br&gt;
Nested list comprehension: Flattening a matrix (a list of lists) ⭐️&lt;/p&gt;

&lt;p&gt;Example: &lt;code&gt;matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;matrix&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="c1"&gt;# first for loop then the next for loop is inside the first and so on
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Other list comprehensions:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Even&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; 
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Odd&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; 
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;This syntax -&amp;gt;&lt;br&gt;
&lt;code&gt;"Even" if x % 2 == 0 else "Odd"&lt;/code&gt; is valid in a lot of places in python, you can also put it in variables, this is basically a ternary operator but more verbose&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;⭐️ For understanding any list comprehension always look on the left and the right side of the expression, any nested loops go from left to right, when the if else is on the left of the for loop we check what value do we wanna insert depending on the if else condition, whereas when the for loop is on the right we are trying to filter from the list&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For example to create a 5x5x5 list: ⭐️&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[[&lt;/span&gt;&lt;span class="n"&gt;num&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;num&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⭐️ Look for the exterior most for loop, whatever is to its left will get added to the values[] array, and the meaning of this syntax: for _ in range(5) is that I want to do whatever is on the left of this for loop 5 times (because I don't care about the value of the iterator variable at each iteration)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Funtions in comprehensions:\
&lt;code&gt;values = [x**2 for x in values if valid(x)]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Dictionary comprehension
&lt;/h1&gt;

&lt;p&gt;If we have &lt;code&gt;pairs = [("a", 1), ("b", 2)]&lt;/code&gt;: a list of pairs or tuples&lt;/p&gt;

&lt;p&gt;-&amp;gt; &lt;code&gt;my_dict = {k:v for k, v in pairs}&lt;/code&gt;: value unpacking, for this to work, each "pair" in pairs must have exactly 2 entities&lt;/p&gt;

&lt;h1&gt;
  
  
  Set comprehension
&lt;/h1&gt;

&lt;p&gt;Let &lt;code&gt;nums = [1, 2, 3, 1, 3, 2, 4, 2, 1, 3]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;unique_nums = {x for x in nums}&lt;/code&gt;: python will know that this should be a set because you don't have any keys&lt;/p&gt;

&lt;p&gt;I will write this once I get some time, but you can still check them out here: &lt;a href="https://github.com/Yash-op7/Advanced-Python-Concepts" rel="noopener noreferrer"&gt;AdvancedPythonConcepts&lt;/a&gt; this is my git repo where I documented python concepts when I first learnt them. I will write this post based off of this repo once I get the time...&lt;/p&gt;

</description>
      <category>python</category>
      <category>dsa</category>
    </item>
    <item>
      <title>C++ nuances/tricks</title>
      <dc:creator>Yash</dc:creator>
      <pubDate>Fri, 16 Aug 2024 20:31:22 +0000</pubDate>
      <link>https://dev.to/yashdevs/c-tricks-3e6l</link>
      <guid>https://dev.to/yashdevs/c-tricks-3e6l</guid>
      <description>&lt;p&gt;This is a post I will regularly update with the nuances and tricks that I discover while solving complex DSA questions in C++.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sets in C++:
&lt;/h3&gt;

&lt;p&gt;You can refer to this code if you want to experiment with the functions listed below: &lt;a href="https://github.com/Yash-op7/Coding-Blog-Code-Snippets/blob/main/cppTricks.cpp" rel="noopener noreferrer"&gt;code&lt;/a&gt;&lt;br&gt;
Let's say you have a &lt;code&gt;set&amp;lt;int&amp;gt; nums;&lt;/code&gt;, then:&lt;/p&gt;
&lt;h4&gt;
  
  
  1. To get the edge elements you can do:
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int first = *nums.begin();
int last = *(--(nums.end()));
// OR    
first = *(--(nums.rend()));
last = *nums.rbegin();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;⭐️ so basically try to not use end() and rend() as they are a bit tricky and there are simpler alternatives:&lt;/p&gt;

&lt;p&gt;❗️ Note that rbegin() and rend() are reverse iterators so to get the second last element using rbegin() what do you think we will do?&lt;/p&gt;

&lt;p&gt;-&amp;gt; The answer is: &lt;code&gt;int secondLast = *(++(nums.rbegin()));&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;❗️ Also remember that rend() and end() don't give iterators pointing to elements, rather they return the pointer of one past the first or the last, i.e, nums.end() will give iterator pointing to the address after the last element which is not dereference-able similarly nums.rend() will give the iterator of the location one before the first element so be careful of how you use them otherwise it will result in memory segmentation errors.&lt;/p&gt;

&lt;p&gt;Also one final note about edge operators:&lt;br&gt;
You can also use constant operators if your functionality requires, to write more predictable and debuggable code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto FIRST_ITR = nums.cbegin();
auto LAST_ITR = nums.cend();
auto FIRST_REV_ITR = nums.crbegin();
auto LAST_REV_ITR = nums.crend();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Different ways to erase elements:
&lt;/h4&gt;

&lt;p&gt;There are 3 methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;nums.erase(iterator)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;⭐️ If you want to remove the 4th element in the set you &lt;strong&gt;cannot&lt;/strong&gt; do:&lt;br&gt;
&lt;code&gt;nums.erase(((nums.begin())+4));&lt;/code&gt;&lt;br&gt;
because while sets are ordered containers they don't allow random access like vectors so you must iterate with the iterator one at a time like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto it = nums.begin();
int x = 4;
while(x--) it++;
nums.erase(it);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;nums.erase(value)&lt;/code&gt; - straightforward, just removes the value from the set if present.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nums.erase(iterator1, iterator2)&lt;/code&gt; - useful, removes elements in the range [iterator1, iterator2) ❗️ note the closed-open interval&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. Trivial but useful functions:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;find()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lower_bound(x)&lt;/code&gt; - Returns an iterator pointing to the first element not less than the given value.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;upper_bound(x)&lt;/code&gt; - Returns an iterator pointing to the first element greater than the given value.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;equal_range(x)&lt;/code&gt; - Basically returns a pair of iterators equivalent to &lt;code&gt;make_pair(nums.lower_bound(x), nums.upper_bound(x))&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;empty()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;size()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  4. Hidden but awesome functions:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;emplace()&lt;/code&gt;: an interesting function to say the least:

&lt;ul&gt;
&lt;li&gt;consider this code:
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;pair&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;insert&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="c1"&gt;// or&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;make_pair&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="c1"&gt;// but with emplace you can do:&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;emplace&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="c1"&gt;// because emplace creates the object in the set directly so it will just pass 1, 2 to the container's element's constructor which is pair&amp;lt;int, int&amp;gt; here.&lt;/span&gt;
&lt;span class="c1"&gt;// or&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;emplace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;make_pair&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="c1"&gt;// but not using list initializer like s.emplace({1, 2)) ❌ because emplace() doesn't automatically deduce that {1, 2} should be converted to a pair&amp;lt;int, int&amp;gt;, so instead you'll have to do:&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;emplace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pair&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;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="c1"&gt;// anyways its better to just stick to make_pair() or direct args passing in .emplace() rather than relying on implicit template deduction&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;benefit of using &lt;code&gt;.emplace()&lt;/code&gt; over &lt;code&gt;.insert()&lt;/code&gt; as they both provide the same utility is the performance difference of the two methods, &lt;code&gt;insert(x)&lt;/code&gt; will create x then transfer it to the set in memory, &lt;code&gt;emplace(x)&lt;/code&gt; will directly create x in the set's internal memory saving transfer/copy cost. this is a negligible advantage for simple objects but for more complicated objects it can be a key optimization.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;swap()&lt;/code&gt; - Exchanges the contents of two sets. This is a fast operation because it swaps pointers and size metadata rather than copying the elements.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;merge(anotherSet)&lt;/code&gt; - Transfers all elements from anotherSet into the set which are not present in destination set, removing duplicates.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    std::set&amp;lt;int&amp;gt; set1 = {1, 3, 5, 7, 9};
    std::set&amp;lt;int&amp;gt; set2 = {2, 4, 6, 8, 10, 5};

    // Merge set2 into set1
    set1.merge(set2);

    // Now set2 has one element 5 and set1 contains the union of both.

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;comparison operators - surprisingly you can compare sets in C++ accordign to their lexicographical order, for example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    set&amp;lt;int&amp;gt; set1 = {1, 3, 5};
    set&amp;lt;int&amp;gt; set2 = {1, 3, 5};
    set&amp;lt;int&amp;gt; set3 = {1, 2, 6};
    set&amp;lt;int&amp;gt; set4 = {1, 3, 4, 7};

    // Compare set1 and set2
    if (set1 == set2) {
        cout &amp;lt;&amp;lt; "set1 is equal to set2" &amp;lt;&amp;lt; endl;
    }

    // Compare set1 and set3
    if (set1 &amp;lt; set3) {
        cout &amp;lt;&amp;lt; "set1 is less than set3" &amp;lt;&amp;lt; endl;
    }

    // Compare set1 and set4
    if (set1 &amp;lt; set4) {
        cout &amp;lt;&amp;lt; "set1 is less than set4" &amp;lt;&amp;lt; endl;
    }

    // output: set1 is equal to set2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Custom set - When constructing a set, you can provide a custom comparator to change the default ordering of the elements.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct ReverseComparator {
    bool operator()(const int&amp;amp; lhs, const int&amp;amp; rhs) const {
        return lhs &amp;gt; rhs; // Reverse order
    }
};

std::set&amp;lt;int, ReverseComparator&amp;gt; mySet;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Sneaky optimization while iterating through containers
&lt;/h3&gt;

&lt;p&gt;Let's say you have a &lt;code&gt;map&amp;lt;string, set&amp;lt;int&amp;gt;&amp;gt; mapper&lt;/code&gt; structure, there are two optimizations you should know if you want iterate over them in the most efficient way possible:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(auto &amp;amp;z : mapper) {             // use &amp;amp;z instead of z
    set&amp;lt;int&amp;gt; &amp;amp;values = z.second;    // same here, reference the value instead of copying it anew
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This might seem trivial but many people make this mistake and get TLE as I have seen in many editorials, so I thought should address it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Object Destructuring in pair
&lt;/h3&gt;

&lt;p&gt;Similar to Python and JS object and array destructuring you can do the following in c++:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    queue&amp;lt;pair&amp;lt;int, vector&amp;lt;int&amp;gt;&amp;gt;&amp;gt; q;
    q.push({INT_MAX, {1, 2}});

    auto [x, y] = q.front();        // works
//  auto [x, &amp;amp;y] = q.front();       // doesn't work

    auto &amp;amp;v = q.front().second;     // do this instead for initialization via reference
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because, when you use structured bindings like auto [x, y], you are effectively unpacking the members of a tuple-like object (like std::pair, std::tuple, etc.) into separate variables. This syntax is intended for value bindings, meaning it creates new variables (x and y) that are copies of the values from the tuple (or pair) being unpacked. So it works well with primitive types but when copying entire objects it could be the cause of TLE.&lt;/p&gt;

&lt;h3&gt;
  
  
  String caution
&lt;/h3&gt;

&lt;p&gt;In c++, you should avoid adding characters to string literals directly as it leads to pointer arithmetic instead of string concatenation. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    string str = "";
    str += "abc" + 'd';    // Runtime Error
    cout &amp;lt;&amp;lt; str &amp;lt;&amp;lt; '\n';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;because, when you do "abc" + 'd', the expression "abc" is treated as a pointer, and 'd' (which is an integer in this context, representing the ASCII value of 'd') is added to the pointer. This results in pointer arithmetic, not string concatenation, which throws an out of bounds exception.&lt;/p&gt;

&lt;p&gt;Observe the below example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    string str = "";
    str += "abcdefg" + 2;
    cout &amp;lt;&amp;lt; str &amp;lt;&amp;lt; '\n';
    // Output: cdefg

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Lambda functions
&lt;/h3&gt;

&lt;p&gt;If you are familiar with Lambda functions in Python (or JS) you know how awesome they can be, I use them excessively in Python, so I thought I should explore them in C++, so here goes:&lt;br&gt;
Basic syntax is:&lt;br&gt;
&lt;code&gt;[capture list](parameters) -&amp;gt; return type { body }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;// This is not complete yet, I will write once I find the time...&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>dsa</category>
      <category>cppstl</category>
    </item>
    <item>
      <title>Design a stack that supports getMin() in O(1) time and O(1) extra space</title>
      <dc:creator>Yash</dc:creator>
      <pubDate>Fri, 16 Aug 2024 15:28:47 +0000</pubDate>
      <link>https://dev.to/yashdevs/design-a-stack-that-supports-getmin-in-o1-time-and-o1-extra-space-9li</link>
      <guid>https://dev.to/yashdevs/design-a-stack-that-supports-getmin-in-o1-time-and-o1-extra-space-9li</guid>
      <description>&lt;p&gt;I came across this interview question the other day, and when I saw the solution, I learned something new. I decided to share it for anyone interested. The task is to design a variation of the stack data structure but with the additional functionality of fetching the current minimum element in the stack without using more than constant extra space. Try to work it out on your own; I will lay out one of the solutions that I found. If your solution is something else, I would love to know it. Anyway, here it is:&lt;/p&gt;

&lt;h2&gt;
  
  
  The Idea
&lt;/h2&gt;

&lt;p&gt;Whenever there is a new minimum pushed to the stack, use the old minimum in a way to relate to the new minimum while simultaneously being able to regenerate the old minimum. Here is a clearer representation:&lt;/p&gt;

&lt;p&gt;Maintain one extra variable associated with the stack, let it be &lt;code&gt;minEle&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Push(int x):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Case 1&lt;/strong&gt;: If the stack is empty, insert &lt;code&gt;x&lt;/code&gt; and set &lt;code&gt;minEle = x&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Case 2&lt;/strong&gt;: If &lt;code&gt;x &amp;gt;= minEle&lt;/code&gt;, simply push &lt;code&gt;x&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Case 3&lt;/strong&gt;: If &lt;code&gt;x &amp;lt; minEle&lt;/code&gt; (when &lt;code&gt;x&lt;/code&gt; is the new minimum), push &lt;code&gt;2*x - minEle&lt;/code&gt;. This is because &lt;code&gt;2*x - previous_minEle &amp;lt; x&lt;/code&gt; is always true. Then, set &lt;code&gt;minEle = x&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pop():&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Case 1&lt;/strong&gt;: If &lt;code&gt;stk.top() &amp;gt;= minEle&lt;/code&gt;, just pop the topmost element.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Case 2&lt;/strong&gt;: If &lt;code&gt;stk.top() &amp;lt; minEle&lt;/code&gt;, then we know that this is actually &lt;code&gt;2*minEle - previous_minEle&lt;/code&gt;. Since we know &lt;code&gt;minEle&lt;/code&gt;, we can find the previous &lt;code&gt;minEle&lt;/code&gt;, update &lt;code&gt;minEle&lt;/code&gt;, and then pop the top element.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I have written an implementation in C++ using an object-oriented approach here: &lt;a href="https://github.com/Yash-op7/Coding-Blog-Code-Snippets/blob/main/MinStack.cpp" rel="noopener noreferrer"&gt;MinStack.cpp&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here is a sample test run:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7s6yo71p97dcuso16en2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7s6yo71p97dcuso16en2.png" alt="Image description" width="800" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dsa</category>
      <category>cpp</category>
      <category>techinterview</category>
      <category>lowleveldesign</category>
    </item>
  </channel>
</rss>
