<?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: Samantha Vincent</title>
    <description>The latest articles on DEV Community by Samantha Vincent (@samantha_vincent_11).</description>
    <link>https://dev.to/samantha_vincent_11</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%2F3838515%2Ff33df687-eaca-47df-b008-cf35a82bf26d.jpg</url>
      <title>DEV Community: Samantha Vincent</title>
      <link>https://dev.to/samantha_vincent_11</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/samantha_vincent_11"/>
    <language>en</language>
    <item>
      <title>IP adresses and Subnets</title>
      <dc:creator>Samantha Vincent</dc:creator>
      <pubDate>Mon, 30 Mar 2026 18:41:16 +0000</pubDate>
      <link>https://dev.to/samantha_vincent_11/ip-adresses-and-subnets-2heg</link>
      <guid>https://dev.to/samantha_vincent_11/ip-adresses-and-subnets-2heg</guid>
      <description>&lt;h2&gt;
  
  
  IP ADDRESS
&lt;/h2&gt;

&lt;p&gt;In real life, let's say you want to find a place or you want to reach a place you need to know the location or precisely the &lt;em&gt;address&lt;/em&gt; of that place. Similarly in computers IP address is used to know or denote a particular computer that is connected under the same network. Basically, every node(device) connected under the same network has a unique address known as IP address. &lt;/p&gt;

&lt;p&gt;Example of an IP address: 10.1.32.12&lt;br&gt;
 Each part has 8 bits and can store 0-255 values.&lt;/p&gt;

&lt;h2&gt;
  
  
  SUBNETS
&lt;/h2&gt;

&lt;p&gt;In a large network, communication between different devices take too much time or is unnecessary. Here, Subnets helps separate or breakdown the network into different parts so that communication is easier and not unnecessary. Imagine in a college it has one Public IP(large network) here we use subnets for each block or department so that communication in the department is fast and effective.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Subnets and IP address work
&lt;/h2&gt;

&lt;p&gt;Every IP belongs to a subnet. Subnets tell you the number of ip's or the the range of IP addresses that are available or fall under that subnet.&lt;br&gt;
For example lets take an IP address: 192.168.1.10&lt;br&gt;
There are always two parts in an IP address&lt;br&gt;
-Network&lt;br&gt;
-Host&lt;br&gt;
Here 192.168.1 is network and 10 denotes host.&lt;/p&gt;

&lt;p&gt;To know how many IP's are stored for networks and host we use a method called &lt;strong&gt;CIDR(Classless Inter Domain Routing).&lt;/strong&gt;&lt;br&gt;
CIDR helps us to know how many devices one can connect under a subnet in an IP. The notation for CIDR is a.b.c.d/n&lt;/p&gt;

&lt;p&gt;Example: 10.1.32.12/24&lt;br&gt;
Here, &lt;strong&gt;/24&lt;/strong&gt; means 256 devices can be connected and is stored for networks.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>computerscience</category>
      <category>learning</category>
      <category>networking</category>
    </item>
    <item>
      <title>Sorting Algorithms</title>
      <dc:creator>Samantha Vincent</dc:creator>
      <pubDate>Sun, 22 Mar 2026 17:39:22 +0000</pubDate>
      <link>https://dev.to/samantha_vincent_11/sorting-algorithms-48j8</link>
      <guid>https://dev.to/samantha_vincent_11/sorting-algorithms-48j8</guid>
      <description>&lt;h1&gt;
  
  
  Sorting Algorithms
&lt;/h1&gt;

&lt;p&gt;A sorting algorithm is a way to arrange elements of an array in a specific order, usually ascending or descending. It helps in organizing data so that other operations like searching and processing become easier and faster.&lt;/p&gt;

&lt;p&gt;Sorting algorithms may look similar at first, but each one follows a different idea. Understanding how they work with small examples makes them easier to grasp.&lt;/p&gt;




&lt;h2&gt;
  
  
  Merge Sort
&lt;/h2&gt;

&lt;p&gt;Merge Sort divides the array into smaller parts and then merges them back in sorted order.&lt;/p&gt;

&lt;p&gt;It keeps splitting the array until each part has one element. Then it starts merging them step by step in a sorted way.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;code&gt;[4, 2, 1, 3]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Split  &lt;code&gt;[4, 2]&lt;/code&gt; and &lt;code&gt;[1, 3]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Split again &lt;code&gt;[4] [2] [1] [3]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Merge  &lt;code&gt;[2, 4]&lt;/code&gt; and &lt;code&gt;[1, 3]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Final merge  &lt;code&gt;[1, 2, 3, 4]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Key Points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses divide and conquer&lt;/li&gt;
&lt;li&gt;Always O(n log n)&lt;/li&gt;
&lt;li&gt;Uses extra space for merging&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Quick Sort
&lt;/h2&gt;

&lt;p&gt;Quick Sort picks a pivot and places it in the correct position.&lt;/p&gt;

&lt;p&gt;Then it rearranges the array so that smaller elements are on the left and larger ones are on the right. The same process is repeated recursively.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;code&gt;[4, 2, 5, 1, 3]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pick pivot = &lt;code&gt;3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Left  &lt;code&gt;[2, 1]&lt;/code&gt;, Right  &lt;code&gt;[4, 5]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Sort both sides  &lt;code&gt;[1, 2]&lt;/code&gt; and &lt;code&gt;[4, 5]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Final  &lt;code&gt;[1, 2, 3, 4, 5]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Key Points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Very fast in practice&lt;/li&gt;
&lt;li&gt;Average time O(n log n)&lt;/li&gt;
&lt;li&gt;Worst case O(n²) if pivot is bad&lt;/li&gt;
&lt;li&gt;In-place (no extra space needed)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Heap Sort
&lt;/h2&gt;

&lt;p&gt;Heap Sort first builds a max heap and then extracts elements one by one.&lt;/p&gt;

&lt;p&gt;A max heap ensures the largest element is always at the top. We swap it with the last element and reduce the heap size.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;code&gt;[4, 1, 3, 2]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build max heap  &lt;code&gt;[4, 2, 3, 1]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Remove 4  &lt;code&gt;[1, 2, 3]&lt;/code&gt; → heapify → &lt;code&gt;[3, 2, 1]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Remove 3 &lt;code&gt;[1, 2]&lt;/code&gt; → heapify → &lt;code&gt;[2, 1]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Final &lt;code&gt;[1, 2, 3, 4]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Key Points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Time complexity O(n log n)&lt;/li&gt;
&lt;li&gt;No extra space required&lt;/li&gt;
&lt;li&gt;Not stable&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Counting Sort
&lt;/h2&gt;

&lt;p&gt;Counting Sort counts occurrences of each number instead of comparing elements.&lt;/p&gt;

&lt;p&gt;It works best when the range of numbers is small.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;code&gt;[2, 1, 2, 0, 1]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Count  &lt;code&gt;0:1, 1:2, 2:2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Rebuild  &lt;code&gt;[0, 1, 1, 2, 2]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Key Points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Time complexity O(n + k)&lt;/li&gt;
&lt;li&gt;Very fast for small ranges&lt;/li&gt;
&lt;li&gt;Not comparison-based&lt;/li&gt;
&lt;li&gt;Uses extra space&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Each algorithm uses a different idea:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Merge Sort - divide and merge&lt;/li&gt;
&lt;li&gt;Quick Sort - partition around pivot&lt;/li&gt;
&lt;li&gt;Heap Sort - use heap structure&lt;/li&gt;
&lt;li&gt;Counting Sort - count frequencies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Knowing these patterns helps in choosing the right approach.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>computerscience</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Reverse the array</title>
      <dc:creator>Samantha Vincent</dc:creator>
      <pubDate>Sun, 22 Mar 2026 17:27:35 +0000</pubDate>
      <link>https://dev.to/samantha_vincent_11/reverse-the-array-19d9</link>
      <guid>https://dev.to/samantha_vincent_11/reverse-the-array-19d9</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;You’re given an array of integers and need to reverse the array in-place.&lt;/p&gt;




&lt;h2&gt;
  
  
  Strategy
&lt;/h2&gt;

&lt;p&gt;At first, it feels like you can just use a built-in reverse function.&lt;/p&gt;

&lt;p&gt;But since we need to do it in-place, I thought about it differently:&lt;/p&gt;

&lt;p&gt;What if I swap elements from both ends?&lt;/p&gt;

&lt;p&gt;So I use two pointers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One at the start&lt;/li&gt;
&lt;li&gt;One at the end&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then keep swapping them and move inward.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;reverseArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&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;arr&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="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Key Lines Explained
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;arr[left], arr[right] = arr[right], arr[left]&lt;/code&gt;&lt;br&gt;
Swaps the elements at the start and end.&lt;/p&gt;




&lt;p&gt;&lt;code&gt;left += 1&lt;/code&gt; and &lt;code&gt;right -= 1&lt;/code&gt;&lt;br&gt;
Moves both pointers towards the center.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Works
&lt;/h2&gt;

&lt;p&gt;We’re just swapping elements from opposite ends:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First with last&lt;/li&gt;
&lt;li&gt;Second with second last&lt;/li&gt;
&lt;li&gt;And so on&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By the time pointers meet, the array is reversed.&lt;/p&gt;




&lt;h2&gt;
  
  
  Complexity
&lt;/h2&gt;

&lt;p&gt;Time: O(n)&lt;br&gt;
Space: O(1)&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Note
&lt;/h2&gt;

&lt;p&gt;This is a simple two-pointer problem.&lt;/p&gt;

&lt;p&gt;Instead of creating a new array, just swap elements in-place to reverse it efficiently.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Find the Maximum and Minimum Element in the Array</title>
      <dc:creator>Samantha Vincent</dc:creator>
      <pubDate>Sun, 22 Mar 2026 17:23:39 +0000</pubDate>
      <link>https://dev.to/samantha_vincent_11/find-the-maximum-and-minimum-element-in-the-array-2dg9</link>
      <guid>https://dev.to/samantha_vincent_11/find-the-maximum-and-minimum-element-in-the-array-2dg9</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;You’re given an array of integers and need to find the minimum and maximum elements.&lt;/p&gt;




&lt;h2&gt;
  
  
  Strategy
&lt;/h2&gt;

&lt;p&gt;At first, it feels like sorting the array will help.&lt;/p&gt;

&lt;p&gt;But we don’t really need to sort just to find min and max.&lt;/p&gt;

&lt;p&gt;So I kept it simple:&lt;/p&gt;

&lt;p&gt;For each number, I check—&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is it smaller than the current minimum?&lt;/li&gt;
&lt;li&gt;Is it bigger than the current maximum?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And update accordingly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;getMinMax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;minimum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&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="n"&gt;maximum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&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="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="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;minimum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;minimum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;maximum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;maximum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;minimum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;maximum&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Key Lines Explained
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;if num &amp;lt; minimum:&lt;/code&gt;&lt;br&gt;
Update the minimum when we find a smaller number.&lt;/p&gt;




&lt;p&gt;&lt;code&gt;if num &amp;gt; maximum:&lt;/code&gt;&lt;br&gt;
Update the maximum when we find a bigger number.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Works
&lt;/h2&gt;

&lt;p&gt;We just keep track of two things while looping:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Smallest number so far&lt;/li&gt;
&lt;li&gt;Largest number so far&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No need to rearrange anything.&lt;/p&gt;




&lt;h2&gt;
  
  
  Complexity
&lt;/h2&gt;

&lt;p&gt;Time: O(n)&lt;br&gt;
Space: O(1)&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Note
&lt;/h2&gt;

&lt;p&gt;This problem is very straightforward.&lt;/p&gt;

&lt;p&gt;Instead of doing extra work like sorting, just compare and update as you go.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Kth Smallest</title>
      <dc:creator>Samantha Vincent</dc:creator>
      <pubDate>Sun, 22 Mar 2026 17:20:48 +0000</pubDate>
      <link>https://dev.to/samantha_vincent_11/kth-smallest-4p60</link>
      <guid>https://dev.to/samantha_vincent_11/kth-smallest-4p60</guid>
      <description>&lt;h1&gt;
  
  
  Kth Smallest Element
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;You’re given an array of integers and an integer k, and need to find the kth smallest element in the array.&lt;/p&gt;




&lt;h2&gt;
  
  
  Strategy
&lt;/h2&gt;

&lt;p&gt;At first, it feels like you need something complex to track the kth element.&lt;/p&gt;

&lt;p&gt;But since the kth smallest element is based on sorted order, I thought about it differently:&lt;/p&gt;

&lt;p&gt;What if I just sort the array first?&lt;/p&gt;

&lt;p&gt;So the idea is simple:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sort the array&lt;/li&gt;
&lt;li&gt;Return the element at index &lt;code&gt;k - 1&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because indexing starts from 0.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;kthSmallest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Key Lines Explained
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;arr.sort()&lt;/code&gt;&lt;br&gt;
Sorts the array in ascending order.&lt;/p&gt;




&lt;p&gt;&lt;code&gt;return arr[k - 1]&lt;/code&gt;&lt;br&gt;
Since arrays are 0-indexed, the kth smallest element will be at index &lt;code&gt;k - 1&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Works
&lt;/h2&gt;

&lt;p&gt;Once the array is sorted:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Smallest element → index 0&lt;/li&gt;
&lt;li&gt;2nd smallest → index 1&lt;/li&gt;
&lt;li&gt;kth smallest → index k-1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the answer becomes direct.&lt;/p&gt;




&lt;h2&gt;
  
  
  Complexity
&lt;/h2&gt;

&lt;p&gt;Time: O(n log n)&lt;br&gt;
Space: O(1)&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Note
&lt;/h2&gt;

&lt;p&gt;This is the most straightforward approach.&lt;/p&gt;

&lt;p&gt;Even though there are more optimized solutions (like heaps or quickselect), sorting keeps it simple and easy to understand.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>dsa</category>
      <category>python</category>
    </item>
    <item>
      <title>Sort 0s, 1s, and 2s</title>
      <dc:creator>Samantha Vincent</dc:creator>
      <pubDate>Sun, 22 Mar 2026 17:16:52 +0000</pubDate>
      <link>https://dev.to/samantha_vincent_11/sort-0s-1s-and-2s-g7f</link>
      <guid>https://dev.to/samantha_vincent_11/sort-0s-1s-and-2s-g7f</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;You’re given an array of integers and need to sort an array containing only 0s, 1s, and 2s.&lt;/p&gt;




&lt;h2&gt;
  
  
  Strategy
&lt;/h2&gt;

&lt;p&gt;At first, it feels like you can just sort the array or count frequencies.&lt;/p&gt;

&lt;p&gt;But the follow-up asks for a one-pass solution with constant space.&lt;/p&gt;

&lt;p&gt;Instead, I thought about it differently:&lt;br&gt;
At each position, I asked—&lt;/p&gt;

&lt;p&gt;Where should this element go?&lt;/p&gt;

&lt;p&gt;So I used three pointers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;low&lt;/code&gt; for placing 0s&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mid&lt;/code&gt; for traversal&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;high&lt;/code&gt; for placing 2s&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So for every element:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If it’s 0 → move it to the front&lt;/li&gt;
&lt;li&gt;If it’s 1 → leave it where it is&lt;/li&gt;
&lt;li&gt;If it’s 2 → move it to the end&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This way, everything gets sorted in a single pass.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sort012&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;low&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="n"&gt;high&lt;/span&gt; &lt;span class="o"&gt;=&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;arr&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="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;high&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;low&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;low&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="n"&gt;low&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
                &lt;span class="n"&gt;mid&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;elif&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&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="n"&gt;mid&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;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;high&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;high&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="n"&gt;high&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Key Lines Explained
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;if arr[mid] == 0:&lt;/code&gt;&lt;br&gt;
Move 0 to the front by swapping with the &lt;code&gt;low&lt;/code&gt; pointer.&lt;/p&gt;




&lt;p&gt;&lt;code&gt;elif arr[mid] == 1:&lt;/code&gt;&lt;br&gt;
Already in the correct place, just move forward.&lt;/p&gt;




&lt;p&gt;&lt;code&gt;else:&lt;/code&gt;&lt;br&gt;
Move 2 to the end by swapping with the &lt;code&gt;high&lt;/code&gt; pointer.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Works
&lt;/h2&gt;

&lt;p&gt;We split the array into three parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Left side → all 0s&lt;/li&gt;
&lt;li&gt;Middle → all 1s&lt;/li&gt;
&lt;li&gt;Right side → all 2s&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As we traverse, we keep adjusting these regions until the array is sorted.&lt;/p&gt;




&lt;h2&gt;
  
  
  Complexity
&lt;/h2&gt;

&lt;p&gt;Time: O(n)&lt;br&gt;
Space: O(1)&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Note
&lt;/h2&gt;

&lt;p&gt;This problem looks like a sorting problem, but it’s really about placing elements in the right region.&lt;/p&gt;

&lt;p&gt;Once you think in terms of positions instead of sorting, the one-pass solution becomes clear.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>interview</category>
      <category>python</category>
    </item>
    <item>
      <title>Move All Negative Elements to End</title>
      <dc:creator>Samantha Vincent</dc:creator>
      <pubDate>Sun, 22 Mar 2026 17:10:36 +0000</pubDate>
      <link>https://dev.to/samantha_vincent_11/move-all-negative-elements-to-end-3f6k</link>
      <guid>https://dev.to/samantha_vincent_11/move-all-negative-elements-to-end-3f6k</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;You’re given an array of integers and need to move all negative elements to the end while maintaining the order of elements.&lt;/p&gt;




&lt;h2&gt;
  
  
  Strategy
&lt;/h2&gt;

&lt;p&gt;At first, it feels like you can just swap elements in-place.&lt;/p&gt;

&lt;p&gt;But that quickly gets messy, especially if you want to preserve the order.&lt;/p&gt;

&lt;p&gt;Instead, I thought about it differently:&lt;br&gt;
At each element, I asked—&lt;/p&gt;

&lt;p&gt;Is it positive or negative?&lt;/p&gt;

&lt;p&gt;So for every element:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If it’s positive, keep it in one list&lt;/li&gt;
&lt;li&gt;If it’s negative, keep it in another list&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This way, I can process the array in one pass and rebuild it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;segregateElements&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;positives&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
        &lt;span class="n"&gt;negatives&lt;/span&gt; &lt;span class="o"&gt;=&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;num&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;positives&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;negatives&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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="n"&gt;positives&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;
            &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="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="n"&gt;negatives&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;
            &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Key Lines Explained
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;current_sum = max(arr[i], current_sum + arr[i])&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This line doesn’t apply here, but the key idea is similar—making a decision at each step.&lt;/p&gt;

&lt;p&gt;Here, the decision is simply whether the number is positive or negative.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Works
&lt;/h2&gt;

&lt;p&gt;If we try to rearrange elements directly, we might break the order.&lt;/p&gt;

&lt;p&gt;By separating them first:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We preserve order&lt;/li&gt;
&lt;li&gt;We keep the logic simple&lt;/li&gt;
&lt;li&gt;Then we combine them back&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Complexity
&lt;/h2&gt;

&lt;p&gt;Time: O(n)&lt;br&gt;
Space: O(n)&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Note
&lt;/h2&gt;

&lt;p&gt;This problem looks like it needs tricky in-place operations, but it doesn’t.&lt;/p&gt;

&lt;p&gt;Once you focus on separating elements instead of rearranging them during traversal, the solution becomes much simpler.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Kadanes Algorithm</title>
      <dc:creator>Samantha Vincent</dc:creator>
      <pubDate>Sun, 22 Mar 2026 17:03:58 +0000</pubDate>
      <link>https://dev.to/samantha_vincent_11/kadanes-algorithm-32af</link>
      <guid>https://dev.to/samantha_vincent_11/kadanes-algorithm-32af</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;You’re given an array of integers and need to find the &lt;strong&gt;maximum sum of any contiguous subarray&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Strategy
&lt;/h2&gt;

&lt;p&gt;At first, it feels like you need to check every possible subarray.&lt;/p&gt;

&lt;p&gt;But that quickly becomes too slow.&lt;/p&gt;

&lt;p&gt;Instead, I thought about it differently:&lt;br&gt;
At each position, I asked—&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Is it better to continue the current subarray, or start fresh from here?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So for every element:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Either extend the current sum&lt;/li&gt;
&lt;li&gt;Or start a new subarray from that element&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This way, I only pass through the array once.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;maxSubArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;current_sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&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="n"&gt;max_sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
            &lt;span class="n"&gt;current_sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;current_sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
            &lt;span class="n"&gt;max_sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current_sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;max_sum&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Key Lines Explained
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;current_sum = max(arr[i], current_sum + arr[i])&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This decides whether to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Continue the current subarray&lt;/li&gt;
&lt;li&gt;Or start a new one&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;max_sum = max(max_sum, current_sum)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Keeps track of the maximum sum seen so far.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Works
&lt;/h2&gt;

&lt;p&gt;If the current sum becomes negative, it won’t help in building a larger sum later.&lt;/p&gt;

&lt;p&gt;So instead of carrying it forward, we reset and start from the current element.&lt;/p&gt;




&lt;h2&gt;
  
  
  Complexity
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time:&lt;/strong&gt; O(n)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space:&lt;/strong&gt; O(1)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Note
&lt;/h2&gt;

&lt;p&gt;This problem looks like it needs brute force, but it doesn’t.&lt;/p&gt;

&lt;p&gt;Once you focus on making a decision at each step instead of looking at all possibilities, the solution becomes much simpler.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>programming</category>
      <category>python</category>
    </item>
    <item>
      <title>Kadanes Algorithm - P2</title>
      <dc:creator>Samantha Vincent</dc:creator>
      <pubDate>Sun, 22 Mar 2026 16:52:22 +0000</pubDate>
      <link>https://dev.to/samantha_vincent_11/kadanes-algorithm-p2-582a</link>
      <guid>https://dev.to/samantha_vincent_11/kadanes-algorithm-p2-582a</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;Problem&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;You’re given an array of integers and need to find the maximum sum of any contiguous subarray.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Strategy&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;At first, it feels like you need to check every possible subarray.&lt;/p&gt;

&lt;p&gt;But that quickly becomes too slow.&lt;/p&gt;

&lt;p&gt;Instead, I thought about it differently:&lt;br&gt;
At each position, I asked—&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Is it better to continue the current subarray, or start fresh from here?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So for every element:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Either extend the current sum&lt;/li&gt;
&lt;li&gt;Or start a new subarray from that element&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This way, I only pass through the array once.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Code&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;maxSubArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;current_sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&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="n"&gt;max_sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
            &lt;span class="n"&gt;current_sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;current_sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
            &lt;span class="n"&gt;max_sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current_sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;max_sum&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;Key Lines Explained&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;current_sum = max(arr[i], current_sum + arr[i])&lt;/code&gt;&lt;br&gt;
This decides whether to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Continue the current subarray&lt;/li&gt;
&lt;li&gt;Or start a new one&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;max_sum = max(max_sum, current_sum)&lt;/code&gt;&lt;br&gt;&lt;br&gt;
Keeps track of the maximum sum seen so far.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Why This Works&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If the current sum becomes negative, it won’t help in building a larger sum later.&lt;/p&gt;

&lt;p&gt;So instead of carrying it forward, we reset and start from the current element.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Complexity&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Time: O(n)&lt;/li&gt;
&lt;li&gt;Space: O(1)&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Final Note&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This problem looks like it needs brute force, but it doesn’t.&lt;/p&gt;

&lt;p&gt;Once you focus on making a decision at each step instead of looking at all possibilities, the solution becomes much simpler.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Next Permutation</title>
      <dc:creator>Samantha Vincent</dc:creator>
      <pubDate>Sun, 22 Mar 2026 16:44:54 +0000</pubDate>
      <link>https://dev.to/samantha_vincent_11/next-permutation-cf4</link>
      <guid>https://dev.to/samantha_vincent_11/next-permutation-cf4</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Next Permutation&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Problem&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Given an array, rearrange it into the next lexicographically greater permutation.&lt;br&gt;
If no such permutation exists, rearrange it into the smallest possible order (sorted in ascending order).&lt;/p&gt;

&lt;p&gt;The change has to be done &lt;strong&gt;in-place&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Strategy&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;At first, it feels like you need to generate all permutations and then pick the next one.&lt;/p&gt;

&lt;p&gt;But that’s not practical.&lt;/p&gt;

&lt;p&gt;Instead, I tried looking at the array from the right side.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Moving from right to left, find the first place where the order breaks&lt;/li&gt;
&lt;li&gt;That’s the point where we can make a slightly bigger permutation&lt;/li&gt;
&lt;li&gt;Swap it with the next larger element&lt;/li&gt;
&lt;li&gt;Then reverse everything after it to make it as small as possible&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So it becomes:&lt;br&gt;
&lt;strong&gt;find where it breaks → swap → reverse&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Code&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;nextPermutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&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;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&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;while&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
                &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&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;while&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;Key Lines Explained&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;while i &amp;gt;= 0 and nums[i] &amp;gt;= nums[i+1]:&lt;/code&gt;&lt;br&gt;
This finds the point where the sequence stops increasing from the right.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;nums[i], nums[j] = nums[j], nums[i]&lt;/code&gt;&lt;br&gt;
Swap with the next larger element to make the permutation just a bit bigger.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reversing from &lt;code&gt;i+1&lt;/code&gt; to end&lt;br&gt;
This ensures the remaining part is the smallest possible.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Why This Works&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;We’re not trying to create all permutations.&lt;/p&gt;

&lt;p&gt;We’re just making the smallest possible change that results in a larger arrangement.&lt;/p&gt;

&lt;p&gt;If that’s not possible (like in a completely decreasing array), we reset to the smallest order.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Complexity&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Time: O(n)&lt;/li&gt;
&lt;li&gt;Space: O(1)&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Final Note&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This problem doesn’t feel obvious at first.&lt;/p&gt;

&lt;p&gt;But once you start looking at the array from the right and focus on where the order changes, the steps become clear and repeatable.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Move Zeros</title>
      <dc:creator>Samantha Vincent</dc:creator>
      <pubDate>Sun, 22 Mar 2026 16:41:51 +0000</pubDate>
      <link>https://dev.to/samantha_vincent_11/move-zeros-293c</link>
      <guid>https://dev.to/samantha_vincent_11/move-zeros-293c</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Move Zeroes&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Problem&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;You’re given an array and need to move all &lt;code&gt;0&lt;/code&gt;s to the end, while keeping the order of the non-zero elements the same.&lt;/p&gt;

&lt;p&gt;The catch is that it has to be done &lt;strong&gt;in-place&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Strategy&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;At first, it feels like you need to shift elements every time you see a zero.&lt;/p&gt;

&lt;p&gt;But that quickly gets messy.&lt;/p&gt;

&lt;p&gt;Instead, I focused on the non-zero elements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keep a position for where the next non-zero should go&lt;/li&gt;
&lt;li&gt;Traverse the array once&lt;/li&gt;
&lt;li&gt;Every time I see a non-zero, move it to that position&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So instead of pushing zeros back, I’m just pulling non-zero elements forward.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Code&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;moveZeroes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;j&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;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&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;nums&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;Key Lines Explained&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;j = 0&lt;/code&gt;&lt;br&gt;
This keeps track of where the next non-zero element should be placed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;if nums[i] != 0:&lt;/code&gt;&lt;br&gt;
We only act when we find a non-zero value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;nums[j], nums[i] = nums[i], nums[j]&lt;/code&gt;&lt;br&gt;
This moves the non-zero element forward without losing order.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;j += 1&lt;/code&gt;&lt;br&gt;
Move to the next position after placing a non-zero.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Why This Works&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;All non-zero elements are moved forward in the same order they appear.&lt;/p&gt;

&lt;p&gt;Zeros are never explicitly moved — they just get pushed to the end as a result of the swaps.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Complexity&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Time: O(n)&lt;/li&gt;
&lt;li&gt;Space: O(1)&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Final Note&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This problem looks like it’s about zeros, but it’s actually about placing non-zero elements correctly.&lt;/p&gt;

&lt;p&gt;Once that perspective shifts, the solution becomes straightforward.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>interview</category>
      <category>python</category>
    </item>
    <item>
      <title>Valid Anagram</title>
      <dc:creator>Samantha Vincent</dc:creator>
      <pubDate>Sun, 22 Mar 2026 16:38:54 +0000</pubDate>
      <link>https://dev.to/samantha_vincent_11/valid-anagram-54ma</link>
      <guid>https://dev.to/samantha_vincent_11/valid-anagram-54ma</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Valid Anagram&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Problem&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Given two strings &lt;code&gt;s&lt;/code&gt; and &lt;code&gt;t&lt;/code&gt;, check if &lt;code&gt;t&lt;/code&gt; is an anagram of &lt;code&gt;s&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An anagram means both strings contain the &lt;strong&gt;same characters with the same frequency&lt;/strong&gt;, just arranged differently.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Strategy&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The idea is simple:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Count how many times each character appears in &lt;code&gt;s&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Then try to “cancel out” those counts using &lt;code&gt;t&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If everything balances to zero, the strings are anagrams.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Code&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;isAnagram&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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;t&lt;/span&gt;&lt;span class="p"&gt;):&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;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&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;t&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;

        &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&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;ch&lt;/span&gt; &lt;span class="ow"&gt;in&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;count&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
            &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ch&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="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;Key Lines Explained&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;if len(s) != len(t):&lt;/code&gt;&lt;br&gt;
If lengths differ, they can’t be anagrams.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;count[ch] = count.get(ch, 0) + 1&lt;/code&gt;&lt;br&gt;
Count frequency of each character in &lt;code&gt;s&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;if ch not in count or count[ch] == 0:&lt;/code&gt;&lt;br&gt;
If a character in &lt;code&gt;t&lt;/code&gt; doesn’t match, return &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;count[ch] -= 1&lt;/code&gt;&lt;br&gt;
Reduce count as we match characters.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Why This Works&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Anagrams have the same characters in the same quantities.&lt;/p&gt;

&lt;p&gt;By counting and cancelling, we’re checking if both strings are perfectly balanced.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Complexity&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Time: O(n)&lt;/li&gt;
&lt;li&gt;Space: O(1) (since only lowercase letters)&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Final Note&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This problem is less about strings and more about counting.&lt;/p&gt;

&lt;p&gt;Once you think in terms of frequency instead of order, it becomes straightforward.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>interview</category>
      <category>python</category>
    </item>
  </channel>
</rss>
