<?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: Jayaprasanna Roddam</title>
    <description>The latest articles on DEV Community by Jayaprasanna Roddam (@jayaprasanna_roddam).</description>
    <link>https://dev.to/jayaprasanna_roddam</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%2F1783643%2F716b50e5-c799-4bc3-85b4-f73442287706.jpg</url>
      <title>DEV Community: Jayaprasanna Roddam</title>
      <link>https://dev.to/jayaprasanna_roddam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jayaprasanna_roddam"/>
    <language>en</language>
    <item>
      <title>Math for Quantum Computing</title>
      <dc:creator>Jayaprasanna Roddam</dc:creator>
      <pubDate>Sun, 22 Mar 2026 11:09:20 +0000</pubDate>
      <link>https://dev.to/jayaprasanna_roddam/math-for-quantum-computing-k1f</link>
      <guid>https://dev.to/jayaprasanna_roddam/math-for-quantum-computing-k1f</guid>
      <description>&lt;h1&gt;
  
  
  Linear Algebra Foundations for Quantum Computing (Explained Clearly)
&lt;/h1&gt;




&lt;h2&gt;
  
  
  1. Vector Spaces (Real vs Complex)
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;vector space&lt;/strong&gt; is a collection of objects (called vectors) where you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add two vectors&lt;/li&gt;
&lt;li&gt;Multiply a vector by a scalar&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Real Vector Space
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Scalars are &lt;strong&gt;real numbers (ℝ)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Example: ℝ² = (x, y)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complex Vector Space
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Scalars are &lt;strong&gt;complex numbers (ℂ)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Example: ℂ² = (a + ib, c + id)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why Complex Matters in Quantum Computing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Quantum states use &lt;strong&gt;complex amplitudes&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Phase (angle in complex plane) is crucial for interference&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. Basis
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;basis&lt;/strong&gt; is a set of vectors that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can generate every vector in the space (span)&lt;/li&gt;
&lt;li&gt;Are linearly independent&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example (ℝ²)
&lt;/h3&gt;

&lt;p&gt;Basis:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;(1, 0)&lt;/li&gt;
&lt;li&gt;(0, 1)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Any vector:&lt;br&gt;
(x, y) = x(1,0) + y(0,1)&lt;/p&gt;

&lt;h3&gt;
  
  
  In Quantum Computing
&lt;/h3&gt;

&lt;p&gt;Standard basis:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;|0⟩ = (1, 0)&lt;/li&gt;
&lt;li&gt;|1⟩ = (0, 1)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. Dimension
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;dimension&lt;/strong&gt; of a vector space = number of vectors in a basis.&lt;/p&gt;

&lt;h3&gt;
  
  
  Examples
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;ℝ² → dimension = 2&lt;/li&gt;
&lt;li&gt;ℝ³ → dimension = 3&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  In Quantum Computing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;1 qubit → dimension = 2
&lt;/li&gt;
&lt;li&gt;n qubits → dimension = 2ⁿ
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This exponential growth is where quantum power comes from.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Inner Product
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;inner product&lt;/strong&gt; measures similarity between two vectors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real Case
&lt;/h3&gt;

&lt;p&gt;For vectors u = (u₁, u₂), v = (v₁, v₂):&lt;/p&gt;

&lt;p&gt;⟨u, v⟩ = u₁v₁ + u₂v₂&lt;/p&gt;

&lt;h3&gt;
  
  
  Complex Case
&lt;/h3&gt;

&lt;p&gt;⟨u, v⟩ = Σ (conjugate(uᵢ) * vᵢ)&lt;/p&gt;

&lt;h3&gt;
  
  
  Why It Matters
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Gives length and angle&lt;/li&gt;
&lt;li&gt;Used to compute probabilities in quantum mechanics&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. Norm
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;norm&lt;/strong&gt; is the length of a vector.&lt;/p&gt;

&lt;p&gt;||v|| = √⟨v, v⟩&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;v = (3, 4)&lt;/p&gt;

&lt;p&gt;||v|| = √(3² + 4²) = 5&lt;/p&gt;

&lt;h3&gt;
  
  
  In Quantum Computing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Quantum states must be normalized:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;||ψ|| = 1&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensures probabilities sum to 1&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  6. Orthogonality
&lt;/h2&gt;

&lt;p&gt;Two vectors are &lt;strong&gt;orthogonal&lt;/strong&gt; if their inner product is zero:&lt;/p&gt;

&lt;p&gt;⟨u, v⟩ = 0&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;(1, 0) · (0, 1) = 0 → orthogonal&lt;/p&gt;

&lt;h3&gt;
  
  
  In Quantum Computing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;|0⟩ and |1⟩ are orthogonal&lt;/li&gt;
&lt;li&gt;Means they are perfectly distinguishable states&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  7. Eigenvalues &amp;amp; Eigenvectors
&lt;/h2&gt;

&lt;p&gt;A vector v is an &lt;strong&gt;eigenvector&lt;/strong&gt; of matrix A if:&lt;/p&gt;

&lt;p&gt;A v = λ v&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;v = eigenvector&lt;/li&gt;
&lt;li&gt;λ = eigenvalue&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Intuition
&lt;/h3&gt;

&lt;p&gt;Applying A:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Changes only the &lt;strong&gt;scale&lt;/strong&gt;, not direction&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;A = [[2, 0],&lt;br&gt;&lt;br&gt;
     [0, 3]]&lt;/p&gt;

&lt;p&gt;Eigenvectors:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;(1, 0) → eigenvalue = 2
&lt;/li&gt;
&lt;li&gt;(0, 1) → eigenvalue = 3
&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Advanced Linear Algebra for Quantum Computing
&lt;/h1&gt;




&lt;h2&gt;
  
  
  1. Spectral Decomposition
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Spectral decomposition&lt;/strong&gt; expresses a matrix in terms of its eigenvalues and eigenvectors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;

&lt;p&gt;If a matrix A has eigenvalues λ₁, λ₂, ..., λₙ and corresponding orthonormal eigenvectors v₁, v₂, ..., vₙ:&lt;/p&gt;

&lt;p&gt;A = Σ (λᵢ * |vᵢ⟩⟨vᵢ|)&lt;/p&gt;




&lt;h3&gt;
  
  
  Intuition
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Any matrix can be “broken down” into simpler components&lt;/li&gt;
&lt;li&gt;Each component scales along a specific direction (eigenvector)&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Why It Matters in Quantum Computing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Measurement operators are decomposed this way&lt;/li&gt;
&lt;li&gt;Helps compute probabilities of outcomes&lt;/li&gt;
&lt;li&gt;Used heavily in quantum algorithms and physics&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. Unitary Matrices
&lt;/h2&gt;

&lt;p&gt;A matrix U is &lt;strong&gt;unitary&lt;/strong&gt; if:&lt;/p&gt;

&lt;p&gt;U†U = UU† = I&lt;/p&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;U† = conjugate transpose of U&lt;/li&gt;
&lt;li&gt;I = identity matrix&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Key Properties
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Preserves length (norm)&lt;/li&gt;
&lt;li&gt;Reversible transformation&lt;/li&gt;
&lt;li&gt;Columns are orthonormal&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Intuition
&lt;/h3&gt;

&lt;p&gt;Unitary transformations = &lt;strong&gt;rotation in complex space&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  In Quantum Computing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;All quantum gates are unitary&lt;/li&gt;
&lt;li&gt;Ensures:

&lt;ul&gt;
&lt;li&gt;No information loss&lt;/li&gt;
&lt;li&gt;State remains normalized&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Hadamard Gate:&lt;/p&gt;

&lt;p&gt;H = (1/√2) * [[1, 1],&lt;br&gt;&lt;br&gt;
              [1, -1]]&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Hermitian Operators
&lt;/h2&gt;

&lt;p&gt;A matrix A is &lt;strong&gt;Hermitian&lt;/strong&gt; if:&lt;/p&gt;

&lt;p&gt;A† = A&lt;/p&gt;




&lt;h3&gt;
  
  
  Key Properties
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Eigenvalues are &lt;strong&gt;real&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Eigenvectors are orthogonal&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Intuition
&lt;/h3&gt;

&lt;p&gt;Hermitian matrices represent &lt;strong&gt;measurable quantities&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  In Quantum Computing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Observables (like position, energy) are Hermitian&lt;/li&gt;
&lt;li&gt;Measurement outcomes = eigenvalues&lt;/li&gt;
&lt;li&gt;System collapses to corresponding eigenvector&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;A = [[2, i],&lt;br&gt;&lt;br&gt;
     [-i, 3]]&lt;/p&gt;

&lt;p&gt;This is Hermitian because A† = A&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Tensor Products (VERY IMPORTANT)
&lt;/h2&gt;

&lt;p&gt;Tensor product combines two vector spaces into a larger one.&lt;/p&gt;




&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;

&lt;p&gt;If:&lt;br&gt;
u = (a, b)&lt;br&gt;&lt;br&gt;
v = (c, d)&lt;/p&gt;

&lt;p&gt;Then:&lt;/p&gt;

&lt;p&gt;u ⊗ v = (ac, ad, bc, bd)&lt;/p&gt;




&lt;h3&gt;
  
  
  Example (Qubits)
&lt;/h3&gt;

&lt;p&gt;|0⟩ = (1, 0)&lt;br&gt;&lt;br&gt;
|1⟩ = (0, 1)&lt;/p&gt;

&lt;p&gt;Then:&lt;/p&gt;

&lt;p&gt;|0⟩ ⊗ |1⟩ = |01⟩ = (0, 1, 0, 0)&lt;/p&gt;




&lt;h3&gt;
  
  
  Key Idea
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;1 qubit → 2D space
&lt;/li&gt;
&lt;li&gt;2 qubits → 4D space
&lt;/li&gt;
&lt;li&gt;n qubits → 2ⁿ space
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Why Tensor Product is Critical
&lt;/h3&gt;

&lt;p&gt;It enables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multi-qubit systems&lt;/li&gt;
&lt;li&gt;Entanglement&lt;/li&gt;
&lt;li&gt;Exponential state space growth&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Important Insight
&lt;/h3&gt;

&lt;p&gt;Not all states can be written as tensor products:&lt;/p&gt;

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

&lt;p&gt;|ψ⟩ = (|00⟩ + |11⟩)/√2&lt;/p&gt;

&lt;p&gt;This is &lt;strong&gt;entangled&lt;/strong&gt; → cannot be separated&lt;/p&gt;




&lt;h3&gt;
  
  
  Why This Matters in Quantum Computing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Observables (like measurement) are operators&lt;/li&gt;
&lt;li&gt;Eigenvalues = possible measurement results&lt;/li&gt;
&lt;li&gt;Eigenvectors = corresponding states&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Big Picture Connection
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Vector spaces → where quantum states live
&lt;/li&gt;
&lt;li&gt;Basis → how we represent states
&lt;/li&gt;
&lt;li&gt;Inner product → gives probabilities
&lt;/li&gt;
&lt;li&gt;Norm → ensures valid quantum states
&lt;/li&gt;
&lt;li&gt;Orthogonality → distinguishes states
&lt;/li&gt;
&lt;li&gt;Eigen concepts → define measurement outcomes
&lt;/li&gt;
&lt;li&gt;Spectral decomposition → breaks operators into measurable parts
&lt;/li&gt;
&lt;li&gt;Unitary matrices → define valid quantum evolution
&lt;/li&gt;
&lt;li&gt;Hermitian operators → define measurements
&lt;/li&gt;
&lt;li&gt;Tensor products → scale systems exponentially&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧠 One Line Summary
&lt;/h2&gt;

&lt;p&gt;Quantum computing =&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Linear algebra over complex spaces + tensor products + unitary evolution&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Quantum Computing</title>
      <dc:creator>Jayaprasanna Roddam</dc:creator>
      <pubDate>Sun, 22 Mar 2026 11:05:50 +0000</pubDate>
      <link>https://dev.to/jayaprasanna_roddam/quantum-computing-2b1a</link>
      <guid>https://dev.to/jayaprasanna_roddam/quantum-computing-2b1a</guid>
      <description>&lt;h2&gt;
  
  
  1. Mathematical Foundations (Non-negotiable)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Linear Algebra
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Vector spaces (real vs complex)&lt;/li&gt;
&lt;li&gt;Basis, dimension&lt;/li&gt;
&lt;li&gt;Inner product&lt;/li&gt;
&lt;li&gt;Norms&lt;/li&gt;
&lt;li&gt;Orthogonality&lt;/li&gt;
&lt;li&gt;Eigenvalues &amp;amp; eigenvectors&lt;/li&gt;
&lt;li&gt;Spectral decomposition&lt;/li&gt;
&lt;li&gt;Unitary matrices&lt;/li&gt;
&lt;li&gt;Hermitian operators&lt;/li&gt;
&lt;li&gt;Tensor products (&lt;strong&gt;VERY IMPORTANT&lt;/strong&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Probability Theory
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Random variables&lt;/li&gt;
&lt;li&gt;Conditional probability&lt;/li&gt;
&lt;li&gt;Bayes theorem&lt;/li&gt;
&lt;li&gt;Expectation &amp;amp; variance&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complex Numbers
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Euler’s formula
&lt;/li&gt;
&lt;li&gt;Polar form
&lt;/li&gt;
&lt;li&gt;Complex conjugates
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. Quantum Mechanics Foundations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Postulates of Quantum Mechanics
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;State representation (wavefunction/state vector)&lt;/li&gt;
&lt;li&gt;Measurement postulate&lt;/li&gt;
&lt;li&gt;Evolution (Schrödinger equation intuition)&lt;/li&gt;
&lt;li&gt;Observables as operators&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Dirac Notation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Bra ⟨ψ| and Ket |ψ⟩&lt;/li&gt;
&lt;li&gt;Inner product ⟨ψ|φ⟩&lt;/li&gt;
&lt;li&gt;Outer product |ψ⟩⟨φ|&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Quantum States
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Pure states&lt;/li&gt;
&lt;li&gt;Mixed states&lt;/li&gt;
&lt;li&gt;Density matrices&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. Qubits and Multi-Qubit Systems
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Single Qubit
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Representation: α|0⟩ + β|1⟩&lt;/li&gt;
&lt;li&gt;Normalisation condition&lt;/li&gt;
&lt;li&gt;Measurement probabilities&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Bloch Sphere
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;State as a point on a sphere&lt;/li&gt;
&lt;li&gt;Rotations = quantum gates&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Multi-Qubit Systems
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Tensor product states&lt;/li&gt;
&lt;li&gt;Basis states (|00⟩, |01⟩, etc.)&lt;/li&gt;
&lt;li&gt;State explosion (2ⁿ-dimensional space)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Entanglement
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Bell states&lt;/li&gt;
&lt;li&gt;Non-separability&lt;/li&gt;
&lt;li&gt;Difference from classical correlation&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. Quantum Gates
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Single-Qubit Gates
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Pauli Gates (X, Y, Z)&lt;/li&gt;
&lt;li&gt;Hadamard (H)&lt;/li&gt;
&lt;li&gt;Phase gate (S, T)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Multi-Qubit Gates
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;CNOT (controlled NOT)&lt;/li&gt;
&lt;li&gt;Toffoli gate&lt;/li&gt;
&lt;li&gt;Controlled-U gates&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Properties
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Unitary transformations&lt;/li&gt;
&lt;li&gt;Reversibility&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. Quantum Circuits
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Circuit Model
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Wires = qubits&lt;/li&gt;
&lt;li&gt;Gates = operations&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Concepts
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Circuit depth&lt;/li&gt;
&lt;li&gt;Parallelism&lt;/li&gt;
&lt;li&gt;Measurement at end&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Universal Gate Sets
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Gate completeness&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  6. Quantum Algorithms
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Deutsch-Jozsa Algorithm
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First demonstration of quantum advantage&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Grover’s Algorithm
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Unstructured search
&lt;/li&gt;
&lt;li&gt;Quadratic speedup
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Shor’s Algorithm
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Integer factorization
&lt;/li&gt;
&lt;li&gt;Cryptography implications
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Quantum Fourier Transform (QFT)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Core subroutine for many algorithms
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Variational Quantum Algorithms
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;VQE (Variational Quantum Eigensolver)&lt;/li&gt;
&lt;li&gt;QAOA (Quantum Approximate Optimisation Algorithm)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  7. Quantum Machine Learning (QML)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Data Encoding
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Basis encoding&lt;/li&gt;
&lt;li&gt;Amplitude encoding&lt;/li&gt;
&lt;li&gt;Angle encoding&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Quantum Models
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Quantum Neural Networks (QNN)&lt;/li&gt;
&lt;li&gt;Variational circuits&lt;/li&gt;
&lt;li&gt;Quantum kernels&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Hybrid Models
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Classical + Quantum pipelines&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Open Problems
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Data loading bottleneck&lt;/li&gt;
&lt;li&gt;Noise sensitivity&lt;/li&gt;
&lt;li&gt;Lack of clear advantage&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  8. Quantum Hardware
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Physical Implementations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Superconducting qubits&lt;/li&gt;
&lt;li&gt;Trapped ions&lt;/li&gt;
&lt;li&gt;Photonic systems&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Challenges
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Decoherence&lt;/li&gt;
&lt;li&gt;Noise&lt;/li&gt;
&lt;li&gt;Error rates&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  9. Quantum Error Correction
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Bit flip error&lt;/li&gt;
&lt;li&gt;Phase flip error&lt;/li&gt;
&lt;li&gt;Shor code&lt;/li&gt;
&lt;li&gt;Surface codes&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  10. Industry Landscape
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;IBM (Qiskit)&lt;/li&gt;
&lt;li&gt;Google (Quantum AI, Sycamore)&lt;/li&gt;
&lt;li&gt;Microsoft (Azure Quantum)&lt;/li&gt;
&lt;li&gt;Rigetti Computing&lt;/li&gt;
&lt;li&gt;IonQ&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  11. Limitations &amp;amp; Misconceptions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Not all problems get speedup
&lt;/li&gt;
&lt;li&gt;Quantum ≠ universally faster
&lt;/li&gt;
&lt;li&gt;Hardware still immature
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  12. Future Directions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Fault-tolerant quantum computing
&lt;/li&gt;
&lt;li&gt;Quantum internet
&lt;/li&gt;
&lt;li&gt;Quantum advantage in ML
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  13. Tools &amp;amp; Frameworks
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Qiskit
&lt;/li&gt;
&lt;li&gt;Cirq
&lt;/li&gt;
&lt;li&gt;PennyLane
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  14. Research-Level Topics
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Quantum complexity classes (BQP, NP)&lt;/li&gt;
&lt;li&gt;Quantum supremacy vs advantage&lt;/li&gt;
&lt;li&gt;Hamiltonian simulation&lt;/li&gt;
&lt;li&gt;Adiabatic quantum computing&lt;/li&gt;
&lt;/ul&gt;




</description>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Sorting-based Pattern</title>
      <dc:creator>Jayaprasanna Roddam</dc:creator>
      <pubDate>Fri, 09 Jan 2026 17:50:36 +0000</pubDate>
      <link>https://dev.to/jayaprasanna_roddam/sorting-based-pattern-4o8p</link>
      <guid>https://dev.to/jayaprasanna_roddam/sorting-based-pattern-4o8p</guid>
      <description>&lt;p&gt;Sorting-based Pattern&lt;/p&gt;

&lt;h2&gt;
  
  
  OVERVIEW
&lt;/h2&gt;

&lt;p&gt;The Sorting-based Pattern solves problems by first sorting the data and then&lt;br&gt;
leveraging the sorted order to simplify logic, reduce complexity, or enable&lt;br&gt;
other efficient patterns like two pointers or greedy decisions.&lt;/p&gt;

&lt;p&gt;Sorting often transforms an otherwise complex problem into a structured one.&lt;/p&gt;

&lt;h2&gt;
  
  
  WHEN TO USE
&lt;/h2&gt;

&lt;p&gt;Use this pattern when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Order of elements matters&lt;/li&gt;
&lt;li&gt;Relative comparisons are important&lt;/li&gt;
&lt;li&gt;Grouping or pairing is required&lt;/li&gt;
&lt;li&gt;You want to apply two pointers or greedy logic&lt;/li&gt;
&lt;li&gt;A small increase in time (sorting) enables a big simplification&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  TIME &amp;amp; SPACE
&lt;/h2&gt;

&lt;p&gt;Sorting Time      : O(N log N)&lt;br&gt;
Post-processing   : Usually O(N)&lt;br&gt;
Space Complexity  : O(1) or O(N) depending on sort implementation&lt;/p&gt;

&lt;h2&gt;
  
  
  CORE IDEA
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Sort the array first&lt;/li&gt;
&lt;li&gt;Use the sorted property to:

&lt;ul&gt;
&lt;li&gt;Eliminate unnecessary comparisons&lt;/li&gt;
&lt;li&gt;Make monotonic decisions&lt;/li&gt;
&lt;li&gt;Detect patterns easily&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Apply linear scans or pointer-based techniques after sorting&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;EXAMPLE 1: Two Sum Using Sorting&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Check if there exists a pair with sum equal to target.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sort the array&lt;/li&gt;
&lt;li&gt;Use two pointers from opposite ends&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def two_sum_sort(nums, target):&lt;br&gt;
    nums.sort()&lt;br&gt;
    left = 0&lt;br&gt;
    right = len(nums) - 1&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while left &amp;lt; right:
    s = nums[left] + nums[right]

    if s == target:
        return True
    elif s &amp;lt; target:
        left += 1
    else:
        right -= 1

return False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 2: Merge Overlapping Intervals&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Given intervals, merge all overlapping intervals.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sort intervals by start time&lt;/li&gt;
&lt;li&gt;Compare current interval with last merged interval&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def merge_intervals(intervals):&lt;br&gt;
    if not intervals:&lt;br&gt;
        return []&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;intervals.sort(key=lambda x: x[0])
merged = [intervals[0]]

for start, end in intervals[1:]:
    last_end = merged[-1][1]

    if start &amp;lt;= last_end:
        merged[-1][1] = max(last_end, end)
    else:
        merged.append([start, end])

return merged
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 3: Find All Triplets That Sum to Zero (3Sum)&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Find all unique triplets such that a + b + c = 0.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sort array&lt;/li&gt;
&lt;li&gt;Fix one element&lt;/li&gt;
&lt;li&gt;Use two pointers for remaining two&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def three_sum(nums):&lt;br&gt;
    nums.sort()&lt;br&gt;
    result = []&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(len(nums)):
    if i &amp;gt; 0 and nums[i] == nums[i - 1]:
        continue

    left = i + 1
    right = len(nums) - 1

    while left &amp;lt; right:
        total = nums[i] + nums[left] + nums[right]

        if total == 0:
            result.append([nums[i], nums[left], nums[right]])

            left += 1
            right -= 1

            while left &amp;lt; right and nums[left] == nums[left - 1]:
                left += 1
            while left &amp;lt; right and nums[right] == nums[right + 1]:
                right -= 1

        elif total &amp;lt; 0:
            left += 1
        else:
            right -= 1

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

&lt;/div&gt;

&lt;p&gt;EXAMPLE 4: Check if Array Can Be Made Non-decreasing&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Check if array can become non-decreasing by modifying at most one element.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sorting gives the target order&lt;/li&gt;
&lt;li&gt;Compare with original array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def check_possibility(nums):&lt;br&gt;
    sorted_nums = sorted(nums)&lt;br&gt;
    mismatch = 0&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(len(nums)):&lt;br&gt;
    if nums[i] != sorted_nums[i]:&lt;br&gt;
        mismatch += 1&lt;br&gt;
        if mismatch &amp;gt; 2:&lt;br&gt;
            return False

&lt;p&gt;return True&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  IDENTIFICATION CHECKLIST&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Ask these questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does sorted order simplify decisions?&lt;/li&gt;
&lt;li&gt;Can sorting unlock a linear scan?&lt;/li&gt;
&lt;li&gt;Is relative ordering important?&lt;/li&gt;
&lt;li&gt;Is N log N acceptable?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If yes, Sorting-based Pattern fits.&lt;/p&gt;

&lt;h2&gt;
  
  
  COMMON PITFALLS
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Forgetting to handle duplicates after sorting&lt;/li&gt;
&lt;li&gt;Assuming stable sort when it is not required&lt;/li&gt;
&lt;li&gt;Sorting when order must be preserved&lt;/li&gt;
&lt;li&gt;Overlooking faster non-sorting solutions&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  SUMMARY
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Sort first, think later&lt;/li&gt;
&lt;li&gt;Enables two pointers and greedy strategies&lt;/li&gt;
&lt;li&gt;Often the key to reducing complexity&lt;/li&gt;
&lt;li&gt;A must-know foundational problem-solving pattern&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>datastructures</category>
    </item>
    <item>
      <title>In-place Modification</title>
      <dc:creator>Jayaprasanna Roddam</dc:creator>
      <pubDate>Fri, 09 Jan 2026 16:38:06 +0000</pubDate>
      <link>https://dev.to/jayaprasanna_roddam/in-place-modification-2bog</link>
      <guid>https://dev.to/jayaprasanna_roddam/in-place-modification-2bog</guid>
      <description>&lt;h2&gt;
  
  
  OVERVIEW
&lt;/h2&gt;

&lt;p&gt;In-place Modification refers to solving problems by modifying the input data&lt;br&gt;
structure directly, without using extra space proportional to input size.&lt;/p&gt;

&lt;p&gt;The goal is to achieve:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;O(1) auxiliary space&lt;/li&gt;
&lt;li&gt;Optimal time complexity&lt;/li&gt;
&lt;li&gt;Memory-efficient solutions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This pattern is very common in array problems and interview questions.&lt;/p&gt;

&lt;h2&gt;
  
  
  WHEN TO USE
&lt;/h2&gt;

&lt;p&gt;Use in-place modification when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The problem explicitly asks for in-place changes&lt;/li&gt;
&lt;li&gt;Extra space is restricted&lt;/li&gt;
&lt;li&gt;The final state of the array matters more than preserving the original&lt;/li&gt;
&lt;li&gt;Elements can be safely overwritten or rearranged&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  TIME &amp;amp; SPACE
&lt;/h2&gt;

&lt;p&gt;Time Complexity  : Usually O(N)&lt;br&gt;
Space Complexity : O(1)&lt;/p&gt;

&lt;h2&gt;
  
  
  CORE IDEA
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Reuse the input array for writing results&lt;/li&gt;
&lt;li&gt;Maintain one or more pointers to track write positions&lt;/li&gt;
&lt;li&gt;Carefully avoid overwriting unprocessed data&lt;/li&gt;
&lt;li&gt;Often combined with Two Pointers patterns&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;EXAMPLE 1: Remove Duplicates from Sorted Array&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Given a sorted array, remove duplicates in-place and return new length.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One pointer reads elements&lt;/li&gt;
&lt;li&gt;Another pointer writes unique values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def remove_duplicates(nums):&lt;br&gt;
    if not nums:&lt;br&gt;
        return 0&lt;/p&gt;

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

for read in range(1, len(nums)):
    if nums[read] != nums[read - 1]:
        nums[write] = nums[read]
        write += 1

return write
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 2: Move Zeroes to End&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Move all zeroes to the end while maintaining relative order of non-zero elements.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write pointer tracks position for next non-zero&lt;/li&gt;
&lt;li&gt;Read pointer scans array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def move_zeroes(nums):&lt;br&gt;
    write = 0&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for read in range(len(nums)):
    if nums[read] != 0:
        nums[write], nums[read] = nums[read], nums[write]
        write += 1

return nums
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 3: Remove Element (Leetcode-style)&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Remove all occurrences of a given value in-place.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Skip unwanted values&lt;/li&gt;
&lt;li&gt;Overwrite them with valid ones&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def remove_element(nums, val):&lt;br&gt;
    write = 0&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for read in range(len(nums)):
    if nums[read] != val:
        nums[write] = nums[read]
        write += 1

return write
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 4: Reverse Array In-Place&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Reverse an array using constant space.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Swap symmetric elements&lt;/li&gt;
&lt;li&gt;Move inward&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def reverse_array(nums):&lt;br&gt;
    left = 0&lt;br&gt;
    right = len(nums) - 1&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while left &amp;lt; right:
    nums[left], nums[right] = nums[right], nums[left]
    left += 1
    right -= 1

return nums
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 5: Replace Elements with Greatest Element on Right Side&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Replace each element with the greatest element to its right.&lt;br&gt;
Last element becomes -1.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Traverse from right&lt;/li&gt;
&lt;li&gt;Keep track of maximum so far&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def replace_with_greatest(nums):&lt;br&gt;
    max_so_far = -1&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(len(nums) - 1, -1, -1):&lt;br&gt;
    current = nums[i]&lt;br&gt;
    nums[i] = max_so_far&lt;br&gt;
    max_so_far = max(max_so_far, current)

&lt;p&gt;return nums&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  IDENTIFICATION CHECKLIST&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Ask these questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Am I allowed to modify the input?&lt;/li&gt;
&lt;li&gt;Can I reuse array slots safely?&lt;/li&gt;
&lt;li&gt;Is extra space discouraged?&lt;/li&gt;
&lt;li&gt;Can pointer-based overwriting solve this?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If yes, use In-place Modification.&lt;/p&gt;

&lt;h2&gt;
  
  
  COMMON PITFALLS
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Overwriting values before they are processed&lt;/li&gt;
&lt;li&gt;Forgetting write pointer increments&lt;/li&gt;
&lt;li&gt;Assuming input order does not matter when it does&lt;/li&gt;
&lt;li&gt;Using extra arrays unnecessarily&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  SUMMARY
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Modify input directly&lt;/li&gt;
&lt;li&gt;Constant extra space&lt;/li&gt;
&lt;li&gt;Often paired with two pointers&lt;/li&gt;
&lt;li&gt;Essential for space-optimized solutions&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>datastructures</category>
    </item>
    <item>
      <title>Hash Map Frequency Counting</title>
      <dc:creator>Jayaprasanna Roddam</dc:creator>
      <pubDate>Fri, 09 Jan 2026 11:17:00 +0000</pubDate>
      <link>https://dev.to/jayaprasanna_roddam/hash-map-frequency-counting-1dfo</link>
      <guid>https://dev.to/jayaprasanna_roddam/hash-map-frequency-counting-1dfo</guid>
      <description>&lt;h2&gt;
  
  
  OVERVIEW
&lt;/h2&gt;

&lt;p&gt;Hash Map Frequency Counting is a fundamental technique used to count occurrences&lt;br&gt;
of elements while iterating through data.&lt;/p&gt;

&lt;p&gt;Instead of repeatedly scanning the data, we store frequencies in a hash map&lt;br&gt;
(dictionary) and update them in O(1) time per element.&lt;/p&gt;

&lt;p&gt;This pattern is widely used in array, string, and prefix-sum based problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  WHEN TO USE
&lt;/h2&gt;

&lt;p&gt;Use this technique when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need counts of elements or characters&lt;/li&gt;
&lt;li&gt;You are checking duplicates or uniqueness&lt;/li&gt;
&lt;li&gt;You need fast lookups&lt;/li&gt;
&lt;li&gt;The order of elements is not the primary concern&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  TIME &amp;amp; SPACE
&lt;/h2&gt;

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

&lt;h2&gt;
  
  
  CORE IDEA
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Traverse the data once&lt;/li&gt;
&lt;li&gt;Store element → frequency mapping&lt;/li&gt;
&lt;li&gt;Update count as elements appear or disappear&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Typical operations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increment frequency&lt;/li&gt;
&lt;li&gt;Decrement frequency&lt;/li&gt;
&lt;li&gt;Check presence or count&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;EXAMPLE 1: Count Frequency of Elements in an Array&lt;/p&gt;

&lt;p&gt;def frequency_count(arr):&lt;br&gt;
    freq = {}&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for element in arr:
    if element in freq:
        freq[element] += 1
    else:
        freq[element] = 1

return freq
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 2: Character Frequency in a String&lt;/p&gt;

&lt;p&gt;def char_frequency(s):&lt;br&gt;
    freq = {}&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for ch in s:
    freq[ch] = freq.get(ch, 0) + 1

return freq
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 3: Find First Non-Repeating Character&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Return the first character in a string that appears exactly once.&lt;/p&gt;

&lt;p&gt;def first_non_repeating_char(s):&lt;br&gt;
    freq = {}&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for ch in s:
    freq[ch] = freq.get(ch, 0) + 1

for ch in s:
    if freq[ch] == 1:
        return ch

return None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 4: Subarray Sum Equals K (Using Prefix Sum + Hash Map)&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Count number of subarrays whose sum equals k.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use prefix sum&lt;/li&gt;
&lt;li&gt;Store frequency of each prefix sum&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def subarray_sum_equals_k(nums, k):&lt;br&gt;
    prefix_sum = 0&lt;br&gt;
    freq = {0: 1}&lt;br&gt;
    count = 0&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for num in nums:
    prefix_sum += num

    if prefix_sum - k in freq:
        count += freq[prefix_sum - k]

    freq[prefix_sum] = freq.get(prefix_sum, 0) + 1

return count
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 5: Longest Substring with At Most K Distinct Characters&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Return length of longest substring containing at most k distinct characters.&lt;/p&gt;

&lt;p&gt;def longest_substring_k_distinct(s, k):&lt;br&gt;
    left = 0&lt;br&gt;
    freq = {}&lt;br&gt;
    max_len = 0&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for right in range(len(s)):&lt;br&gt;
    freq[s[right]] = freq.get(s[right], 0) + 1
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while len(freq) &amp;amp;gt; k:
    freq[s[left]] -= 1
    if freq[s[left]] == 0:
        del freq[s[left]]
    left += 1

max_len = max(max_len, right - left + 1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;return max_len&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  IDENTIFICATION CHECKLIST&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Ask these questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do I need to count occurrences?&lt;/li&gt;
&lt;li&gt;Do I need fast existence checks?&lt;/li&gt;
&lt;li&gt;Is repeated scanning too slow?&lt;/li&gt;
&lt;li&gt;Can frequency changes track window validity?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If yes, Hash Map Frequency Counting is the right approach.&lt;/p&gt;

&lt;h2&gt;
  
  
  COMMON PITFALLS
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Forgetting to delete zero-frequency keys&lt;/li&gt;
&lt;li&gt;Using lists instead of hash maps for large ranges&lt;/li&gt;
&lt;li&gt;Not initializing base cases (like prefix sum 0)&lt;/li&gt;
&lt;li&gt;Overusing space when simple counters suffice&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  SUMMARY
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Store element → count mapping&lt;/li&gt;
&lt;li&gt;Enables O(1) updates and lookups&lt;/li&gt;
&lt;li&gt;Essential for sliding window and prefix sum patterns&lt;/li&gt;
&lt;li&gt;One of the most powerful everyday tools in problem solving&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>datastructures</category>
    </item>
    <item>
      <title>Difference Array</title>
      <dc:creator>Jayaprasanna Roddam</dc:creator>
      <pubDate>Fri, 09 Jan 2026 11:05:43 +0000</pubDate>
      <link>https://dev.to/jayaprasanna_roddam/difference-array-473l</link>
      <guid>https://dev.to/jayaprasanna_roddam/difference-array-473l</guid>
      <description>&lt;h2&gt;
  
  
  OVERVIEW
&lt;/h2&gt;

&lt;p&gt;Difference Array is an optimization technique used to efficiently perform&lt;br&gt;
multiple range update operations on an array.&lt;/p&gt;

&lt;p&gt;Instead of updating every element in a range [L, R], we update only the&lt;br&gt;
boundaries and reconstruct the final array later using prefix sums.&lt;/p&gt;

&lt;p&gt;This reduces range updates from O(N) to O(1).&lt;/p&gt;

&lt;h2&gt;
  
  
  WHEN TO USE
&lt;/h2&gt;

&lt;p&gt;Use Difference Array when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There are many range update operations&lt;/li&gt;
&lt;li&gt;Final array is needed after all updates&lt;/li&gt;
&lt;li&gt;Updates are additive (increment/decrement)&lt;/li&gt;
&lt;li&gt;Queries are fewer or done after all updates&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  TIME &amp;amp; SPACE
&lt;/h2&gt;

&lt;p&gt;Update Time per Query : O(1)&lt;br&gt;
Reconstruction Time   : O(N)&lt;br&gt;
Space Complexity      : O(N)&lt;/p&gt;

&lt;h2&gt;
  
  
  CORE IDEA
&lt;/h2&gt;

&lt;p&gt;Given an array arr of size N, create a difference array diff of size N.&lt;/p&gt;

&lt;p&gt;Definition:&lt;br&gt;
diff[0] = arr[0]&lt;br&gt;
diff[i] = arr[i] - arr[i - 1]&lt;/p&gt;

&lt;p&gt;To add value x to range [L, R]:&lt;br&gt;
diff[L] += x&lt;br&gt;
diff[R + 1] -= x   (if R + 1 &amp;lt; N)&lt;/p&gt;

&lt;p&gt;Final array is obtained by prefix sum of diff.&lt;/p&gt;

&lt;p&gt;EXAMPLE 1: Build Difference Array from Original Array&lt;/p&gt;

&lt;p&gt;def build_difference_array(arr):&lt;br&gt;
    diff = [0] * len(arr)&lt;br&gt;
    diff[0] = arr[0]&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(1, len(arr)):
    diff[i] = arr[i] - arr[i - 1]

return diff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 2: Apply Range Updates Using Difference Array&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Given an array of size N, apply multiple updates of the form:&lt;br&gt;
add value x to all elements from index L to R (inclusive).&lt;/p&gt;

&lt;p&gt;def apply_range_updates(n, updates):&lt;br&gt;
    diff = [0] * n&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for left, right, value in updates:
    diff[left] += value
    if right + 1 &amp;lt; n:
        diff[right + 1] -= value

return diff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 3: Reconstruct Final Array from Difference Array&lt;/p&gt;

&lt;p&gt;def reconstruct_array(diff):&lt;br&gt;
    arr = [0] * len(diff)&lt;br&gt;
    arr[0] = diff[0]&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(1, len(diff)):
    arr[i] = arr[i - 1] + diff[i]

return arr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 4: Full Flow Example&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Initial array of zeros, apply range updates, get final array.&lt;/p&gt;

&lt;p&gt;def range_update_final_array(n, updates):&lt;br&gt;
    diff = apply_range_updates(n, updates)&lt;br&gt;
    return reconstruct_array(diff)&lt;/p&gt;

&lt;h2&gt;
  
  
  IDENTIFICATION CHECKLIST
&lt;/h2&gt;

&lt;p&gt;Ask these questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Are there many range update operations?&lt;/li&gt;
&lt;li&gt;Is each update additive?&lt;/li&gt;
&lt;li&gt;Do I need the final array only after all updates?&lt;/li&gt;
&lt;li&gt;Can prefix sums reconstruct the result?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If yes, Difference Array is the ideal technique.&lt;/p&gt;

&lt;h2&gt;
  
  
  COMMON PITFALLS
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Forgetting to subtract at R + 1&lt;/li&gt;
&lt;li&gt;Out-of-bounds access for R + 1&lt;/li&gt;
&lt;li&gt;Trying to query intermediate states&lt;/li&gt;
&lt;li&gt;Confusing difference array with prefix sum array&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  SUMMARY
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Convert range updates into boundary updates&lt;/li&gt;
&lt;li&gt;Apply prefix sum to reconstruct array&lt;/li&gt;
&lt;li&gt;Massive optimization for range update problems&lt;/li&gt;
&lt;li&gt;Foundational for advanced techniques like Imos method&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>python</category>
      <category>datastructures</category>
    </item>
    <item>
      <title>Prefix Sum</title>
      <dc:creator>Jayaprasanna Roddam</dc:creator>
      <pubDate>Fri, 09 Jan 2026 10:56:08 +0000</pubDate>
      <link>https://dev.to/jayaprasanna_roddam/prefix-sum-124f</link>
      <guid>https://dev.to/jayaprasanna_roddam/prefix-sum-124f</guid>
      <description>&lt;p&gt;Prefix Sum&lt;/p&gt;

&lt;h2&gt;
  
  
  OVERVIEW
&lt;/h2&gt;

&lt;p&gt;Prefix Sum is a preprocessing technique used to efficiently answer range-based&lt;br&gt;
queries on arrays.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Precompute cumulative sums&lt;/li&gt;
&lt;li&gt;Use them to answer subarray sum queries in O(1)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of recomputing sums repeatedly, we reuse previously computed results.&lt;/p&gt;

&lt;h2&gt;
  
  
  WHEN TO USE
&lt;/h2&gt;

&lt;p&gt;Use Prefix Sum when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You are asked for sum of multiple subarrays&lt;/li&gt;
&lt;li&gt;Queries involve ranges [L, R]&lt;/li&gt;
&lt;li&gt;You want to reduce repeated work&lt;/li&gt;
&lt;li&gt;The array is static (no frequent updates)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  TIME &amp;amp; SPACE
&lt;/h2&gt;

&lt;p&gt;Preprocessing Time : O(N)&lt;br&gt;
Query Time        : O(1)&lt;br&gt;
Space Complexity  : O(N)&lt;/p&gt;

&lt;h2&gt;
  
  
  CORE IDEA
&lt;/h2&gt;

&lt;p&gt;Given an array:&lt;br&gt;
arr = [a0, a1, a2, a3, ...]&lt;/p&gt;

&lt;p&gt;Build prefix sum array:&lt;br&gt;
prefix[0] = 0&lt;br&gt;
prefix[i] = a0 + a1 + ... + a(i-1)&lt;/p&gt;

&lt;p&gt;Then:&lt;br&gt;
Sum of subarray [L, R] =&lt;br&gt;
prefix[R + 1] - prefix[L]&lt;/p&gt;

&lt;p&gt;EXAMPLE 1: Build Prefix Sum Array&lt;/p&gt;

&lt;p&gt;def build_prefix_sum(arr):&lt;br&gt;
    prefix = [0] * (len(arr) + 1)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(len(arr)):
    prefix[i + 1] = prefix[i] + arr[i]

return prefix
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 2: Range Sum Query&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Find sum of elements between index L and R (inclusive).&lt;/p&gt;

&lt;p&gt;Formula:&lt;br&gt;
sum(L, R) = prefix[R + 1] - prefix[L]&lt;/p&gt;

&lt;p&gt;def range_sum(prefix, left, right):&lt;br&gt;
    return prefix[right + 1] - prefix[left]&lt;/p&gt;

&lt;p&gt;EXAMPLE 3: Subarray Sum Equals K&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Count the number of subarrays whose sum equals k.&lt;/p&gt;

&lt;p&gt;Key Insight:&lt;br&gt;
If:&lt;br&gt;
prefix[j] - prefix[i] = k&lt;br&gt;
Then:&lt;br&gt;
prefix[i] = prefix[j] - k&lt;/p&gt;

&lt;p&gt;Use a hashmap to track prefix sum frequencies.&lt;/p&gt;

&lt;p&gt;def subarray_sum_equals_k(nums, k):&lt;br&gt;
    prefix_sum = 0&lt;br&gt;
    count = 0&lt;br&gt;
    freq = {0: 1}&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for num in nums:
    prefix_sum += num

    if prefix_sum - k in freq:
        count += freq[prefix_sum - k]

    freq[prefix_sum] = freq.get(prefix_sum, 0) + 1

return count
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 4: Maximum Sum Subarray of Size K&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Find maximum sum of any subarray of size k.&lt;/p&gt;

&lt;p&gt;This is a prefix-sum-based solution (sliding window optimized version exists).&lt;/p&gt;

&lt;p&gt;def max_sum_subarray_k(arr, k):&lt;br&gt;
    prefix = build_prefix_sum(arr)&lt;br&gt;
    max_sum = float('-inf')&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(len(arr) - k + 1):&lt;br&gt;
    current_sum = prefix[i + k] - prefix[i]&lt;br&gt;
    max_sum = max(max_sum, current_sum)

&lt;p&gt;return max_sum&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  IDENTIFICATION CHECKLIST&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Ask these questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Are multiple range queries involved?&lt;/li&gt;
&lt;li&gt;Is the array static?&lt;/li&gt;
&lt;li&gt;Do I need fast subarray sum computation?&lt;/li&gt;
&lt;li&gt;Can subtraction of cumulative values help?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If yes, Prefix Sum is the correct approach.&lt;/p&gt;

&lt;h2&gt;
  
  
  COMMON PITFALLS
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Off-by-one errors in indexing&lt;/li&gt;
&lt;li&gt;Forgetting to add initial 0 in prefix array&lt;/li&gt;
&lt;li&gt;Using prefix sum on frequently updated arrays&lt;/li&gt;
&lt;li&gt;Assuming only positive numbers (prefix works with negatives too)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  SUMMARY
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Precompute once, query many times&lt;/li&gt;
&lt;li&gt;Range sum in O(1)&lt;/li&gt;
&lt;li&gt;Powerful when combined with hash maps&lt;/li&gt;
&lt;li&gt;Foundational technique for many advanced problems&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>python</category>
    </item>
    <item>
      <title>Fast &amp; Slow Pointers (Cycle Detection)</title>
      <dc:creator>Jayaprasanna Roddam</dc:creator>
      <pubDate>Fri, 09 Jan 2026 10:33:30 +0000</pubDate>
      <link>https://dev.to/jayaprasanna_roddam/fast-slow-pointers-cycle-detection-gpg</link>
      <guid>https://dev.to/jayaprasanna_roddam/fast-slow-pointers-cycle-detection-gpg</guid>
      <description>&lt;h2&gt;
  
  
  OVERVIEW
&lt;/h2&gt;

&lt;p&gt;The Fast &amp;amp; Slow Pointers pattern uses two pointers moving through the data&lt;br&gt;
structure at different speeds.&lt;/p&gt;

&lt;p&gt;Typically:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;slow moves one step at a time&lt;/li&gt;
&lt;li&gt;fast moves two steps at a time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This pattern is most famous for detecting cycles in linked lists, but it is also&lt;br&gt;
used for finding the middle of a list, cycle length, and cycle start.&lt;/p&gt;

&lt;h2&gt;
  
  
  WHY IT WORKS
&lt;/h2&gt;

&lt;p&gt;If a cycle exists:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The fast pointer will eventually lap the slow pointer&lt;/li&gt;
&lt;li&gt;Both pointers will meet inside the cycle&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If no cycle exists:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The fast pointer will reach the end (null)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  TIME &amp;amp; SPACE
&lt;/h2&gt;

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

&lt;p&gt;EXAMPLE 1: Detect Cycle in a Linked List (Floyd’s Algorithm)&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Determine whether a linked list has a cycle.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Move slow by 1 step&lt;/li&gt;
&lt;li&gt;Move fast by 2 steps&lt;/li&gt;
&lt;li&gt;If they ever meet, a cycle exists&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;class ListNode:&lt;br&gt;
    def &lt;strong&gt;init&lt;/strong&gt;(self, val=0, next=None):&lt;br&gt;
        self.val = val&lt;br&gt;
        self.next = next&lt;/p&gt;

&lt;p&gt;def has_cycle(head):&lt;br&gt;
    slow = head&lt;br&gt;
    fast = head&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while fast is not None and fast.next is not None:
    slow = slow.next
    fast = fast.next.next

    if slow == fast:
        return True

return False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 2: Find the Starting Point of the Cycle&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
If a cycle exists, find the node where the cycle begins.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;When slow and fast meet, move one pointer to head&lt;/li&gt;
&lt;li&gt;Move both one step at a time&lt;/li&gt;
&lt;li&gt;The point where they meet again is the cycle start&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def detect_cycle_start(head):&lt;br&gt;
    slow = head&lt;br&gt;
    fast = head&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while fast is not None and fast.next is not None:
    slow = slow.next
    fast = fast.next.next

    if slow == fast:
        break
else:
    return None

slow = head
while slow != fast:
    slow = slow.next
    fast = fast.next

return slow
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 3: Find Length of the Cycle&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Given a linked list with a cycle, determine the length of the cycle.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After slow and fast meet, keep one pointer fixed&lt;/li&gt;
&lt;li&gt;Move the other pointer until it meets again&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def cycle_length(head):&lt;br&gt;
    slow = head&lt;br&gt;
    fast = head&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while fast is not None and fast.next is not None:
    slow = slow.next
    fast = fast.next.next

    if slow == fast:
        length = 1
        current = slow.next

        while current != slow:
            length += 1
            current = current.next

        return length

return 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;EXAMPLE 4: Find Middle of Linked List&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Return the middle node of a linked list.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slow moves 1 step&lt;/li&gt;
&lt;li&gt;Fast moves 2 steps&lt;/li&gt;
&lt;li&gt;When fast reaches end, slow is at middle&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def middle_node(head):&lt;br&gt;
    slow = head&lt;br&gt;
    fast = head&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while fast is not None and fast.next is not None:&lt;br&gt;
    slow = slow.next&lt;br&gt;
    fast = fast.next.next

&lt;p&gt;return slow&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  IDENTIFICATION CHECKLIST&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Ask these questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do two pointers move at different speeds?&lt;/li&gt;
&lt;li&gt;Is the structure sequential (linked list / array)?&lt;/li&gt;
&lt;li&gt;Is there a cycle or repetition possibility?&lt;/li&gt;
&lt;li&gt;Do I need middle or loop information?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If yes, Fast &amp;amp; Slow Pointers is the right pattern.&lt;/p&gt;

&lt;h2&gt;
  
  
  SUMMARY
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Two pointers, different speeds&lt;/li&gt;
&lt;li&gt;Guaranteed meeting point if cycle exists&lt;/li&gt;
&lt;li&gt;Elegant, math-backed solution&lt;/li&gt;
&lt;li&gt;Core interview pattern worth mastering&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>python</category>
    </item>
    <item>
      <title>Two Pointers (Opposite Ends)</title>
      <dc:creator>Jayaprasanna Roddam</dc:creator>
      <pubDate>Fri, 09 Jan 2026 10:24:11 +0000</pubDate>
      <link>https://dev.to/jayaprasanna_roddam/two-pointers-opposite-ends-40na</link>
      <guid>https://dev.to/jayaprasanna_roddam/two-pointers-opposite-ends-40na</guid>
      <description>&lt;p&gt;"""&lt;br&gt;
Two Pointers (Opposite Ends)&lt;/p&gt;

&lt;h2&gt;
  
  
  OVERVIEW
&lt;/h2&gt;

&lt;p&gt;This pattern uses two pointers that start from opposite ends of a data structure&lt;br&gt;
(array or string) and move toward each other.&lt;/p&gt;

&lt;p&gt;It is mainly used when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Order matters from both ends&lt;/li&gt;
&lt;li&gt;The data is sorted or symmetric&lt;/li&gt;
&lt;li&gt;You are checking pairs or mirrored positions&lt;/li&gt;
&lt;li&gt;You want to reduce time complexity from O(N^2) to O(N)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  POINTER SETUP
&lt;/h2&gt;

&lt;p&gt;left  -&amp;gt; starts at index 0&lt;br&gt;
right -&amp;gt; starts at index n - 1&lt;/p&gt;

&lt;h2&gt;
  
  
  LOOP CONDITION
&lt;/h2&gt;

&lt;p&gt;Continue while left &amp;lt; right&lt;/p&gt;

&lt;h2&gt;
  
  
  POINTER MOVEMENT LOGIC
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If current condition is satisfied: update answer and move pointers&lt;/li&gt;
&lt;li&gt;If value is too small: move left forward&lt;/li&gt;
&lt;li&gt;If value is too large: move right backward&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  TIME &amp;amp; SPACE
&lt;/h2&gt;

&lt;p&gt;Time Complexity  : O(N)&lt;br&gt;
Space Complexity : O(1)&lt;br&gt;
"""&lt;/p&gt;

&lt;p&gt;"""&lt;br&gt;
EXAMPLE 1: Two Sum (Sorted Array)&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Given a sorted array, determine if there exists a pair whose sum equals target.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If sum is smaller than target, increase left to get a bigger sum&lt;/li&gt;
&lt;li&gt;If sum is larger than target, decrease right to get a smaller sum
"""&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def two_sum_sorted(nums, target):&lt;br&gt;
    left = 0&lt;br&gt;
    right = len(nums) - 1&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while left &amp;lt; right:
    current_sum = nums[left] + nums[right]

    if current_sum == target:
        return True
    elif current_sum &amp;lt; target:
        left += 1
    else:
        right -= 1

return False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;"""&lt;br&gt;
EXAMPLE 2: Palindrome Check&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Check whether a string is a palindrome.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Characters at symmetric positions must match&lt;/li&gt;
&lt;li&gt;Any mismatch immediately returns false
"""&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def is_palindrome(s):&lt;br&gt;
    left = 0&lt;br&gt;
    right = len(s) - 1&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while left &amp;lt; right:
    if s[left] != s[right]:
        return False

    left += 1
    right -= 1

return True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;"""&lt;br&gt;
EXAMPLE 3: Container With Most Water&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Find two vertical lines that together with the x-axis form a container holding&lt;br&gt;
the maximum amount of water.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Area depends on width and the smaller height&lt;/li&gt;
&lt;li&gt;Move the pointer with the smaller height to try increasing area
"""&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def max_area(height):&lt;br&gt;
    left = 0&lt;br&gt;
    right = len(height) - 1&lt;br&gt;
    max_water = 0&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while left &amp;lt; right:
    width = right - left
    current_height = min(height[left], height[right])
    max_water = max(max_water, width * current_height)

    if height[left] &amp;lt; height[right]:
        left += 1
    else:
        right -= 1

return max_water
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;"""&lt;br&gt;
EXAMPLE 4: Reverse Array In-Place&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
Reverse an array without using extra space.&lt;/p&gt;

&lt;p&gt;Logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Swap elements at symmetric positions&lt;/li&gt;
&lt;li&gt;Move inward until pointers meet
"""&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;def reverse_array(arr):&lt;br&gt;
    left = 0&lt;br&gt;
    right = len(arr) - 1&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while left &amp;lt; right:
    arr[left], arr[right] = arr[right], arr[left]
    left += 1
    right -= 1

return arr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;"""&lt;/p&gt;

&lt;h2&gt;
  
  
  IDENTIFICATION CHECKLIST
&lt;/h2&gt;

&lt;p&gt;Ask these questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Are two elements compared together?&lt;/li&gt;
&lt;li&gt;Do we care about values from both ends?&lt;/li&gt;
&lt;li&gt;Can one side be discarded every step?&lt;/li&gt;
&lt;li&gt;Is the data sorted or symmetric?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If yes, this pattern fits.&lt;/p&gt;

&lt;h2&gt;
  
  
  SUMMARY
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Two pointers start from opposite ends&lt;/li&gt;
&lt;li&gt;Both move inward&lt;/li&gt;
&lt;li&gt;Each iteration removes impossible cases&lt;/li&gt;
&lt;li&gt;Clean, optimal, and easy to reason about
"""&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>career</category>
    </item>
    <item>
      <title>Two Pointers (Same Direction)</title>
      <dc:creator>Jayaprasanna Roddam</dc:creator>
      <pubDate>Fri, 09 Jan 2026 10:18:14 +0000</pubDate>
      <link>https://dev.to/jayaprasanna_roddam/two-pointers-same-direction-39i</link>
      <guid>https://dev.to/jayaprasanna_roddam/two-pointers-same-direction-39i</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Two Pointers (Same Direction)&lt;/strong&gt; pattern uses &lt;strong&gt;two indices moving forward together&lt;/strong&gt; through a data structure (usually an array or string).&lt;br&gt;&lt;br&gt;
Unlike opposite-direction pointers, &lt;strong&gt;both pointers only move left → right&lt;/strong&gt;, but at &lt;strong&gt;different speeds or based on conditions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This pattern is extremely common in &lt;strong&gt;linear-time optimizations&lt;/strong&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  When to Use Two Pointers (Same Direction)
&lt;/h2&gt;

&lt;p&gt;Use this pattern when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You process elements &lt;strong&gt;in order&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;You want to &lt;strong&gt;compare or relate two positions&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;You need to &lt;strong&gt;skip, filter, or track a range&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;You want to avoid nested loops (O(N²))&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Typical Problem Statements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Remove duplicates from sorted array&lt;/li&gt;
&lt;li&gt;Check if array is a subsequence of another&lt;/li&gt;
&lt;li&gt;Longest substring without repeating characters&lt;/li&gt;
&lt;li&gt;Merge two sorted arrays&lt;/li&gt;
&lt;li&gt;Partition array based on condition&lt;/li&gt;
&lt;li&gt;Fast &amp;amp; slow pointer problems (variant)&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Core Intuition
&lt;/h2&gt;

&lt;p&gt;Think of the pointers as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Reader &amp;amp; Writer&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Slow &amp;amp; Fast&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Anchor &amp;amp; Explorer&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One pointer represents a &lt;strong&gt;reference position&lt;/strong&gt;, the other &lt;strong&gt;scans ahead&lt;/strong&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  General Algorithm Template
&lt;/h2&gt;

&lt;p&gt;initialize slow = 0&lt;/p&gt;

&lt;p&gt;for fast in range(0, n):&lt;br&gt;
if condition satisfied:&lt;br&gt;
perform operation using slow &amp;amp; fast&lt;br&gt;
slow += 1&lt;/p&gt;

&lt;p&gt;Both pointers move forward, but:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fast&lt;/code&gt; moves &lt;strong&gt;every iteration&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;slow&lt;/code&gt; moves &lt;strong&gt;only when needed&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Key Properties
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Time Complexity: &lt;strong&gt;O(N)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Space Complexity: &lt;strong&gt;O(1)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Works best on &lt;strong&gt;sorted arrays or strings&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Avoids unnecessary comparisons&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Example 1: Remove Duplicates from Sorted Array
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;Given a sorted array, remove duplicates &lt;strong&gt;in-place&lt;/strong&gt; and return the new length.&lt;/p&gt;
&lt;h3&gt;
  
  
  Idea
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fast&lt;/code&gt; scans elements&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;slow&lt;/code&gt; marks the position to write unique values&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Code
&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;def&lt;/span&gt; &lt;span class="nf"&gt;remove_duplicates&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="ow"&gt;not&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;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="n"&gt;slow&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;fast&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;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;fast&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;slow&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="n"&gt;slow&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;slow&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;fast&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;slow&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;p&gt;Example 2: Check Subsequence&lt;br&gt;
Problem&lt;br&gt;
Check if string s is a subsequence of string t.&lt;/p&gt;

&lt;p&gt;Idea&lt;br&gt;
slow tracks position in s&lt;/p&gt;

&lt;p&gt;fast scans t&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def is_subsequence(s, t):
    slow = 0

    for fast in range(len(t)):
        if slow &amp;lt; len(s) and s[slow] == t[fast]:
            slow += 1

    return slow == len(s)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 3: Longest Substring Without Repeating Characters&lt;br&gt;
Problem&lt;br&gt;
Find the length of the longest substring without repeating characters.&lt;/p&gt;

&lt;p&gt;Idea&lt;br&gt;
left and right both move forward&lt;/p&gt;

&lt;p&gt;Use a set to maintain uniqueness&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def longest_unique_substring(s):
    seen = set()
    left = 0
    max_len = 0

    for right in range(len(s)):
        while s[right] in seen:
            seen.remove(s[left])
            left += 1

        seen.add(s[right])
        max_len = max(max_len, right - left + 1)

    return max_len
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 4: Merge Two Sorted Arrays&lt;br&gt;
Problem&lt;br&gt;
Merge two sorted arrays into one sorted array.&lt;/p&gt;

&lt;p&gt;Idea&lt;br&gt;
Pointer i for first array&lt;/p&gt;

&lt;p&gt;Pointer j for second array&lt;/p&gt;

&lt;p&gt;Both move forward&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def merge_sorted(a, b):
    i = j = 0
    result = []

    while i &amp;lt; len(a) and j &amp;lt; len(b):
        if a[i] &amp;lt;= b[j]:
            result.append(a[i])
            i += 1
        else:
            result.append(b[j])
            j += 1

    result.extend(a[i:])
    result.extend(b[j:])
    return result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Difference from Sliding Window&lt;br&gt;
Feature Two Pointers (Same Direction)   Sliding Window&lt;br&gt;
Window Concept  ❌ Optional    ✅ Required&lt;br&gt;
Condition   Simple comparison   Constraint-based&lt;br&gt;
Use Case    Filtering / matching    Optimization&lt;br&gt;
Example Remove duplicates   Longest subarray&lt;/p&gt;

&lt;p&gt;Common Mistakes&lt;br&gt;
Moving both pointers blindly&lt;/p&gt;

&lt;p&gt;Forgetting pointer bounds&lt;/p&gt;

&lt;p&gt;Using it for unsorted data when order matters&lt;/p&gt;

&lt;p&gt;Confusing it with opposite-direction pointers&lt;/p&gt;

&lt;p&gt;How to Identify This Pattern&lt;br&gt;
&lt;strong&gt;Ask yourself:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do I need to scan forward once?&lt;/li&gt;
&lt;li&gt;Can one pointer track progress?&lt;/li&gt;
&lt;li&gt;Is order important?&lt;/li&gt;
&lt;li&gt;Can I replace a nested loop?
If yes → Two Pointers (Same Direction)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Mental Model&lt;br&gt;
Imagine:&lt;/p&gt;

&lt;p&gt;O&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ne pointer walks&lt;/li&gt;
&lt;li&gt;Another pointer records&lt;/li&gt;
&lt;li&gt;Both move forward, never backward&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Summary&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Aspect    Two Pointers (Same Direction)&lt;/li&gt;
&lt;li&gt;Pointers  Both move left → right&lt;/li&gt;
&lt;li&gt;Speed Different&lt;/li&gt;
&lt;li&gt;Time  O(N)&lt;/li&gt;
&lt;li&gt;Space O(1)&lt;/li&gt;
&lt;li&gt;Best For  Filtering, matching, merging&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Final Thought&lt;br&gt;
This pattern is the bridge between brute force and optimal solutions.&lt;br&gt;
Once mastered, you’ll instinctively replace nested loops with clean linear scans.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>interview</category>
      <category>performance</category>
    </item>
    <item>
      <title>Sliding Window (Variable Size)</title>
      <dc:creator>Jayaprasanna Roddam</dc:creator>
      <pubDate>Fri, 09 Jan 2026 08:56:55 +0000</pubDate>
      <link>https://dev.to/jayaprasanna_roddam/sliding-window-variable-size-593k</link>
      <guid>https://dev.to/jayaprasanna_roddam/sliding-window-variable-size-593k</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Variable Size Sliding Window&lt;/strong&gt; technique is a powerful pattern used to solve problems involving &lt;strong&gt;subarrays or substrings&lt;/strong&gt; where the window size is &lt;strong&gt;not fixed&lt;/strong&gt; and depends on a &lt;strong&gt;condition&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Unlike fixed-size sliding windows, here the window &lt;strong&gt;expands and contracts dynamically&lt;/strong&gt; based on constraints such as sum, frequency, distinct count, etc.&lt;/p&gt;




&lt;h2&gt;
  
  
  When to Use Variable Sliding Window
&lt;/h2&gt;

&lt;p&gt;Use this pattern when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You are dealing with &lt;strong&gt;continuous subarrays / substrings&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Window size is &lt;strong&gt;not predefined&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;The problem asks for:

&lt;ul&gt;
&lt;li&gt;Longest / Shortest subarray&lt;/li&gt;
&lt;li&gt;At most K / At least K / Exactly K&lt;/li&gt;
&lt;li&gt;A constraint that must be maintained dynamically&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Typical Problem Statements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Longest subarray with sum ≤ K&lt;/li&gt;
&lt;li&gt;Smallest subarray with sum ≥ K&lt;/li&gt;
&lt;li&gt;Longest substring with at most K distinct characters&lt;/li&gt;
&lt;li&gt;Minimum window substring&lt;/li&gt;
&lt;li&gt;Longest substring without repeating characters&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Core Intuition
&lt;/h2&gt;

&lt;p&gt;We maintain a window &lt;code&gt;[left, right]&lt;/code&gt; such that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;right&lt;/code&gt; pointer &lt;strong&gt;expands&lt;/strong&gt; the window&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;left&lt;/code&gt; pointer &lt;strong&gt;shrinks&lt;/strong&gt; the window when constraints are violated&lt;/li&gt;
&lt;li&gt;The window always represents a &lt;strong&gt;valid candidate&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  General Algorithm Template
&lt;/h2&gt;

&lt;p&gt;initialize left = 0&lt;br&gt;
initialize required data structures (sum, hashmap, count, etc.)&lt;br&gt;
initialize answer&lt;/p&gt;

&lt;p&gt;for right in range(0, n):&lt;br&gt;
include arr[right] in window&lt;/p&gt;

&lt;p&gt;while window condition is violated:&lt;br&gt;
    remove arr[left] from window&lt;br&gt;
    left += 1&lt;/p&gt;

&lt;p&gt;update answer using (right - left + 1)&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Properties
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Each element is visited &lt;strong&gt;at most twice&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Time Complexity: &lt;strong&gt;O(N)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Space Complexity: &lt;strong&gt;O(1)&lt;/strong&gt; or &lt;strong&gt;O(K)&lt;/strong&gt; depending on data structures&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Example 1: Longest Subarray with Sum ≤ K
&lt;/h2&gt;

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

&lt;p&gt;Given an array of positive integers and an integer &lt;code&gt;K&lt;/code&gt;, find the length of the longest subarray whose sum is ≤ K.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Expand &lt;code&gt;right&lt;/code&gt; and add elements to &lt;code&gt;sum&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;sum &amp;gt; K&lt;/code&gt;, shrink from &lt;code&gt;left&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Update max length whenever window is valid&lt;/li&gt;
&lt;/ul&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
python
def longest_subarray_sum_at_most_k(nums, k):
    left = 0
    window_sum = 0
    max_len = 0

    for right in range(len(nums)):
        window_sum += nums[right]

        while window_sum &amp;gt; k:
            window_sum -= nums[left]
            left += 1

        max_len = max(max_len, right - left + 1)

    return max_len
Example 2: Smallest Subarray with Sum ≥ K
Problem
Find the length of the smallest subarray whose sum is ≥ K.

Approach
Expand window until sum ≥ K

Shrink from left to minimize window

Keep updating minimum length

Code
def smallest_subarray_sum_at_least_k(nums, k):
    left = 0
    window_sum = 0
    min_len = float('inf')

    for right in range(len(nums)):
        window_sum += nums[right]

        while window_sum &amp;gt;= k:
            min_len = min(min_len, right - left + 1)
            window_sum -= nums[left]
            left += 1

    return min_len if min_len != float('inf') else 0
Example 3: Longest Substring with At Most K Distinct Characters
Problem
Given a string s, find the length of the longest substring with at most K distinct characters.

Approach
Use a frequency hashmap

Expand right, add characters

If distinct count &amp;gt; K, shrink from left

Code

from collections import defaultdict

def longest_substring_k_distinct(s, k):
    left = 0
    freq = defaultdict(int)
    max_len = 0

    for right in range(len(s)):
        freq[s[right]] += 1

        while len(freq) &amp;gt; k:
            freq[s[left]] -= 1
            if freq[s[left]] == 0:
                del freq[s[left]]
            left += 1

        max_len = max(max_len, right - left + 1)

    return max_len


Exactly K Pattern (Important Trick)
Problems asking for exactly K can often be solved using:

Exactly K = At Most K - At Most (K - 1)
Example
Subarrays with exactly K distinct integers

Common Mistakes
Forgetting to shrink the window

Updating answer before the window becomes valid

Using this pattern for subsequence problems (it works only for subarrays/substrings)

Applying it to arrays with negative numbers (sum-based window breaks)

Checklist to Identify Variable Sliding Window
Ask yourself:

Is the data continuous?

Is the window size dynamic?

Is there a constraint to maintain?

Can expanding and shrinking solve it in linear time?

If yes → Sliding Window (Variable)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>python</category>
    </item>
    <item>
      <title>Sliding window (Fixed length)</title>
      <dc:creator>Jayaprasanna Roddam</dc:creator>
      <pubDate>Tue, 06 Jan 2026 16:36:31 +0000</pubDate>
      <link>https://dev.to/jayaprasanna_roddam/sliding-window-fixed-length-2pk4</link>
      <guid>https://dev.to/jayaprasanna_roddam/sliding-window-fixed-length-2pk4</guid>
      <description>&lt;h1&gt;
  
  
  Sliding Window (Fixed) — Complete Topic Explanation
&lt;/h1&gt;

&lt;p&gt;The &lt;strong&gt;Sliding Window (Fixed)&lt;/strong&gt; pattern is an algorithmic technique used to efficiently process &lt;strong&gt;all contiguous subarrays or substrings of a fixed size &lt;code&gt;k&lt;/code&gt;&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
It avoids repeated computation by reusing results from overlapping windows.&lt;/p&gt;




&lt;h2&gt;
  
  
  Core Idea
&lt;/h2&gt;

&lt;p&gt;You are given:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A sequence (array or string)&lt;/li&gt;
&lt;li&gt;A fixed window size &lt;code&gt;k&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your task:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Perform a computation (sum, max, min, count, frequency, condition check, etc.)&lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;every contiguous block of size &lt;code&gt;k&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of recomputing the result for each block independently, you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maintain a window of exactly &lt;code&gt;k&lt;/code&gt; elements&lt;/li&gt;
&lt;li&gt;Slide the window one position at a time&lt;/li&gt;
&lt;li&gt;Update the result incrementally&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why the Naive Approach Is Inefficient
&lt;/h2&gt;

&lt;p&gt;If:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Total elements = &lt;code&gt;n&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Window size = &lt;code&gt;k&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Number of windows = &lt;code&gt;(n - k + 1)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Work per window = &lt;code&gt;k&lt;/code&gt; operations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Total work:&lt;br&gt;
(n - k + 1) * k&lt;/p&gt;

&lt;p&gt;Asymptotically:&lt;br&gt;
O((n - k + 1) * k) ≈ O(nk)&lt;/p&gt;

&lt;p&gt;This becomes inefficient for large inputs.&lt;/p&gt;




&lt;h2&gt;
  
  
  Sliding Window Optimization Insight
&lt;/h2&gt;

&lt;p&gt;When the window moves forward by one position:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One element exits the window (left side)&lt;/li&gt;
&lt;li&gt;One element enters the window (right side)&lt;/li&gt;
&lt;li&gt;All other elements remain unchanged&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
Before: [a, b, c]&lt;br&gt;
After : [b, c, d]&lt;/p&gt;

&lt;p&gt;Instead of recomputing:&lt;br&gt;
b + c + d&lt;/p&gt;

&lt;p&gt;We update:&lt;br&gt;
new_value = old_value - a + d&lt;/p&gt;

&lt;p&gt;This update takes &lt;strong&gt;constant time&lt;/strong&gt;.&lt;/p&gt;




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

&lt;ol&gt;
&lt;li&gt;Initialize variables to track the window state&lt;/li&gt;
&lt;li&gt;Traverse the sequence from left to right&lt;/li&gt;
&lt;li&gt;Add the current element to the window&lt;/li&gt;
&lt;li&gt;Once the window size reaches &lt;code&gt;k&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;Process or record the window result&lt;/li&gt;
&lt;li&gt;Remove the leftmost element&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Continue until the end of the sequence&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Key Characteristics
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Window size is &lt;strong&gt;fixed&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Elements are &lt;strong&gt;contiguous&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Window slides &lt;strong&gt;one step at a time&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Computation is &lt;strong&gt;incrementally updated&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Eliminates nested loops&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Time and Space Complexity
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;Complexity&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Time&lt;/td&gt;
&lt;td&gt;&lt;code&gt;O(n)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Space&lt;/td&gt;
&lt;td&gt;&lt;code&gt;O(1)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  When to Use Fixed Sliding Window
&lt;/h2&gt;

&lt;p&gt;This pattern is applicable when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The problem mentions &lt;strong&gt;subarrays&lt;/strong&gt; or &lt;strong&gt;substrings&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Elements must be &lt;strong&gt;contiguous&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;fixed window size &lt;code&gt;k&lt;/code&gt;&lt;/strong&gt; is given&lt;/li&gt;
&lt;li&gt;The same computation repeats over each window&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Common Problem Types
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Maximum / minimum sum subarray of size &lt;code&gt;k&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Average of all subarrays of size &lt;code&gt;k&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Count of vowels or characters in substrings of size &lt;code&gt;k&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Count of distinct elements in each window&lt;/li&gt;
&lt;li&gt;First element satisfying a condition in every window&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Mental Model
&lt;/h2&gt;

&lt;p&gt;Imagine a &lt;strong&gt;transparent window of width &lt;code&gt;k&lt;/code&gt;&lt;/strong&gt; sliding across the data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You only see &lt;code&gt;k&lt;/code&gt; elements at a time&lt;/li&gt;
&lt;li&gt;Each move removes one element and adds one new element&lt;/li&gt;
&lt;li&gt;The result is updated instantly without recomputation&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Fixed Sliding Window is used for &lt;strong&gt;contiguous, fixed-size problems&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;It reduces time complexity from &lt;code&gt;O(nk)&lt;/code&gt; to &lt;code&gt;O(n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;It is simple, efficient, and widely applicable&lt;/li&gt;
&lt;li&gt;Mastering it unlocks many array and string interview problems&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
