<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pranav Biradar on Medium]]></title>
        <description><![CDATA[Stories by Pranav Biradar on Medium]]></description>
        <link>https://medium.com/@bypanu?source=rss-afc8fef92662------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Pranav Biradar on Medium</title>
            <link>https://medium.com/@bypanu?source=rss-afc8fef92662------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 14:39:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bypanu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Insertion Sort Algorithm]]></title>
            <link>https://bypanu.medium.com/insertion-sort-algorithm-0c48bda3221c?source=rss-afc8fef92662------2</link>
            <guid isPermaLink="false">https://medium.com/p/0c48bda3221c</guid>
            <category><![CDATA[problem-solving-skills]]></category>
            <category><![CDATA[dsa-problem]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[objective-c]]></category>
            <category><![CDATA[insertion-sort]]></category>
            <dc:creator><![CDATA[Pranav Biradar]]></dc:creator>
            <pubDate>Sun, 16 Aug 2026 17:40:22 GMT</pubDate>
            <atom:updated>2026-08-17T08:40:13.204Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://bypanu-io.github.io/AlgoLab-Visualization/">AlgoLab - Sorting &amp; Searching Algorithm Laboratory</a></p><ul><li><strong>Intuition :</strong></li></ul><p>Insertion Sort takes one element at a time and inserts into the correct position in the already sorted part.</p><ul><li><strong>Step — by — Step explanation:</strong></li></ul><p>For example, [ 5 , 3 , 4 , 1 ]</p><ul><li>Take 3 → insert before 5 → [3, 5, 4, 1]</li><li>Take 4 → insert between 3 and 5 → [3, 4, 5, 1]</li><li>Take 1 → insert at beginning → [1, 3, 4, 5]<br><strong>Step 1:</strong> Start from index 1<br><em>for (int i = 1; i &lt; arr.length; i++)</em><br>We start from i = 1 because the first element 5 is considered already sorted.<br>[5 | 3 4 1]<br> ↑<br>sorted part.<br><strong>Step 2:</strong> Store current element<br><em>int key = arr[i];</em><br>For i = 1:<br>key = 3<br>We want to find the correct position for 3.<br><strong>Step 3:</strong> Take previous index<br><em>int j = i - 1;</em><br>So:<br>j = 0<br>arr[j] = 5<br><strong>Step 4:</strong> Compare<br><em>while (j &gt;= 0 &amp;&amp; arr[j] &gt; key)</em><br>Check:<br>5 &gt; 3 → true<br>So we move 5 one position right.<br><em>arr[j + 1] = arr[j];</em><br>Array becomes:<br>[5, 5, 4, 1]<br>Then:<br><em>j--;</em><br>Now j = -1.<br><strong>Step 5:</strong> Insert the key<br><em>arr[j + 1] = key;</em><br>Since j = -1:<br>arr[0] = 3<br>Array:<br>[3, 5, 4, 1]<br><strong>Step 6:</strong> Next element<br>Now:<br>[3, 5 | 4 1]<br>key = 4<br>Compare:<br>5 &gt; 4 → shift 5<br>[3, 5, 5, 1]<br>Then insert 4:<br>[3, 4, 5, 1]<br><strong>Step 7:</strong> Last element<br>[3, 4, 5 | 1]<br>key = 1<br>Shift:<br>5 → right<br>4 → right<br>3 → right<br>Then insert 1:<br>[1, 3, 4, 5]</li><li><strong>Code in java :</strong></li></ul><pre>import java.util.Scanner;<br><br>public class InsertionSort {<br><br>    static void insertionSort(int[] arr) {<br>        for (int i = 1; i &lt; arr.length; i++) {<br>            int key = arr[i];<br>            int j = i - 1;<br><br>            while (j &gt;= 0 &amp;&amp; arr[j] &gt; key) {<br>                arr[j + 1] = arr[j];<br>                j--;<br>            }<br><br>            arr[j + 1] = key;<br>        }<br>    }<br><br>    public static void main(String[] args) {<br>        Scanner sc = new Scanner(System.in);<br><br>        int n = sc.nextInt();<br>        int[] arr = new int[n];<br><br>        for (int i = 0; i &lt; n; i++) {<br>            arr[i] = sc.nextInt();<br>        }<br><br>        insertionSort(arr);<br><br>        for (int i = 0; i &lt; n; i++) {<br>            System.out.print(arr[i] + &quot; &quot;);<br>        }<br>    }<br>}</pre><ul><li><strong>Code in C + +:</strong></li></ul><pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>void insertionSort(int arr[], int n) {<br><br>    for (int i = 1; i &lt; n; i++) {<br>        int key = arr[i];<br>        int j = i - 1;<br><br>        while (j &gt;= 0 &amp;&amp; arr[j] &gt; key) {<br>            arr[j + 1] = arr[j];<br>            j--;<br>        }<br><br>        arr[j + 1] = key;<br>    }<br>}<br><br>int main() {<br>    int n;<br>    cin &gt;&gt; n;<br><br>    int arr[n];<br><br>    for (int i = 0; i &lt; n; i++) {<br>        cin &gt;&gt; arr[i];<br>    }<br><br>    insertionSort(arr, n);<br><br>    for (int i = 0; i &lt; n; i++) {<br>        cout &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;<br>    }<br><br>    return 0;<br>}</pre><ul><li><strong>Time and Space Complexity:</strong></li></ul><p><strong>Time:</strong> Best O(n) when the array is already sorted, because each element needs only one comparison. Average and worst case are O(n²) because elements may need to be shifted many positions.<br><strong>Space:</strong> O(1) because only key and j are used as extra variables, so no additional array is required</p><blockquote>I hope you will understand……best of luck on your journey .</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0c48bda3221c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Selection Sort Algorithm]]></title>
            <link>https://bypanu.medium.com/selection-sort-algorithm-8abb019f42e0?source=rss-afc8fef92662------2</link>
            <guid isPermaLink="false">https://medium.com/p/8abb019f42e0</guid>
            <category><![CDATA[problem-solving-skills]]></category>
            <category><![CDATA[selection-sort]]></category>
            <category><![CDATA[dsa-problem]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Pranav Biradar]]></dc:creator>
            <pubDate>Wed, 12 Aug 2026 15:13:48 GMT</pubDate>
            <atom:updated>2026-08-12T15:13:48.586Z</atom:updated>
            <content:encoded><![CDATA[<ul><li><strong>Idea of Selection Sort :</strong></li></ul><p>Selection sort repeatedly finds the smallest element from the unsorted part and puts it at the correct position.</p><ul><li><strong>Example:</strong></li></ul><p>[64, 25, 12, 22, 11]</p><ul><li><strong>Step -by - Step Explanation:</strong></li><li>Pass 1: Find smallest from entire array → 11</li><li>Swap 11 with 64<br>[11, 25, 12, 22, 64]</li><li>Pass 2: Find smallest from remaining part → 12</li><li>Swap 12 with 25<br>[11, 12, 25, 22, 64]</li><li>Pass 3: Find smallest → 22<br>[11, 12, 22, 25, 64]</li><li>Pass 4: Find smallest → 25<br>[11, 12, 22, 25, 64]</li></ul><p>Then we got the sorted array which is</p><p>[11,12,22,25,64].</p><ul><li><strong>Algorithm:</strong></li></ul><ol><li>Start from index i = 0.<br>Assume i contains the minimum.</li><li>Search the remaining array for a smaller element.</li><li>Store its index.</li><li>Swap the minimum element with arr[i].</li><li>Repeat until the array is sorted.</li></ol><ul><li><strong>Code in Java :</strong></li></ul><pre>import java.util.Scanner;<br><br>public class SelectionSort {<br><br>    static void selectionSort(int[] arr) {<br>        int n = arr.length;<br><br>        for (int i = 0; i &lt; n - 1; i++) {<br>            int minIndex = i;<br><br>            for (int j = i + 1; j &lt; n; j++) {<br>                if (arr[j] &lt; arr[minIndex]) {<br>                    minIndex = j;<br>                }<br>            }<br><br>            int temp = arr[i];<br>            arr[i] = arr[minIndex];<br>            arr[minIndex] = temp;<br>        }<br>    }<br><br>    public static void main(String[] args) {<br>        Scanner sc = new Scanner(System.in);<br><br>        int n = sc.nextInt();<br>        int[] arr = new int[n];<br><br>        for (int i = 0; i &lt; n; i++) {<br>            arr[i] = sc.nextInt();<br>        }<br><br>        selectionSort(arr);<br><br>        for (int i = 0; i &lt; n; i++) {<br>            System.out.print(arr[i] + &quot; &quot;);<br>        }<br>    }<br>}</pre><ul><li><strong>Code in C + +:</strong></li></ul><pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>void selectionSort(int arr[], int n) {<br><br>    for (int i = 0; i &lt; n - 1; i++) {<br>        int minIndex = i;<br><br>        for (int j = i + 1; j &lt; n; j++) {<br>            if (arr[j] &lt; arr[minIndex]) {<br>                minIndex = j;<br>            }<br>        }<br><br>        int temp = arr[i];<br>        arr[i] = arr[minIndex];<br>        arr[minIndex] = temp;<br>    }<br>}<br><br>int main() {<br>    int n;<br>    cin &gt;&gt; n;<br><br>    int arr[n];<br><br>    for (int i = 0; i &lt; n; i++) {<br>        cin &gt;&gt; arr[i];<br>    }<br><br>    selectionSort(arr, n);<br><br>    for (int i = 0; i &lt; n; i++) {<br>        cout &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;<br>    }<br><br>    return 0;<br>}</pre><ul><li><strong>Logic:</strong><br>Selection Sort finds the smallest element from the unsorted part of the array and places it at the correct position. In each pass, minIndex stores the index of the smallest element. After searching, the smallest element is swapped with the element at the current position. This process is repeated until the array is sorted.</li><li><strong>Pattern of this Selection Sorting:</strong></li></ul><p><em>minIndex → find minimum → swap</em></p><ul><li><strong>Time and Space Complexity:</strong></li></ul><p><strong>Time Complexity:</strong> Selection Sort has O(n²) time complexity because it uses two nested loops. The outer loop runs n times, and for every position, the inner loop searches through the remaining elements to find the minimum. Therefore, approximately n × n comparisons are made.</p><p><strong>Space Complexity:</strong> Selection Sort has O(1) space complexity because it sorts the array in-place and uses only a few extra variables such as minIndex and temp, regardless of the input size.</p><p><em>I hope you will understand…..Best of luck for your journey.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8abb019f42e0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bubble Sort Algorithm]]></title>
            <link>https://bypanu.medium.com/bubble-sort-algorithm-602fb4c2b27f?source=rss-afc8fef92662------2</link>
            <guid isPermaLink="false">https://medium.com/p/602fb4c2b27f</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[dsa-problem]]></category>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[bubble-sort-algorithm]]></category>
            <dc:creator><![CDATA[Pranav Biradar]]></dc:creator>
            <pubDate>Tue, 11 Aug 2026 14:49:27 GMT</pubDate>
            <atom:updated>2026-08-11T14:49:27.849Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/sort-an-array">Sort an Array - LeetCode</a></p><p>It is first basic sorting algorithm.</p><p>It is the approach of sorting the given integer array .</p><p><strong>Example:</strong></p><p>The given array is , {5,4,1,3,2} we want to arrange in increasing order or decreasing order that means sort the array.</p><ul><li>{5,4,1,3,2} , unsorted array</li><li>{1,2,3,4,5} , increasing order</li><li>{5,4,3,2,1} , decreasing order</li></ul><p><strong>Intuition of bubble sort:</strong></p><p>Large elements come to the end of the array by swapping with adjacent elements.</p><p><strong>Algorithm of bubble sort:</strong></p><ul><li>Start from the first element of the array.</li><li>Compare the current element with the next element.</li><li>If the current element is greater than the next element, swap them.</li><li>Move to the next pair and repeat until the end of the array.</li></ul><p>Repeat the entire process until no swaps are needed.</p><p>Let’s sort {5,4,1,3,2 } using Bubble Sort in ascending order.<br>Initial array:<br>5 4 1 3 2</p><ul><li>Pass 1<br>5 &gt; 4 → swap → 4 5 1 3 2<br>5 &gt; 1 → swap → 4 1 5 3 2<br>5 &gt; 3 → swap → 4 1 3 5 2<br>5 &gt; 2 → swap → 4 1 3 2 5<br>After Pass 1: 4 1 3 2 5<br>Pass 2<br>4 &gt; 1 → swap → 1 4 3 2 5<br>4 &gt; 3 → swap → 1 3 4 2 5<br>4 &gt; 2 → swap → 1 3 2 4 5<br>After Pass 2: 1 3 2 4 5</li><li>Pass 3<br>1 &lt; 3 → no swap → 1 3 2 4 5<br>3 &gt; 2 → swap → 1 2 3 4 5<br>After Pass 3: 1 2 3 4 5</li><li>Pass 4<br>1 &lt; 2 → no swap<br>2 &lt; 3 → no swap<br>After Pass 4: 1 2 3 4 5</li></ul><p><strong>Code Ascending Order :</strong></p><pre>public class BubbleSort {<br>    public static void main(String[] args) {<br><br>        int[] arr = {5, 4, 1, 3, 2};<br><br>        for (int i = 0; i &lt; arr.length - 1; i++) {<br><br>            for (int j = 0; j &lt; arr.length - 1 - i; j++) {<br><br>                if (arr[j] &gt; arr[j + 1]) {<br>                    // Swap elements<br>                    int temp = arr[j];<br>                    arr[j] = arr[j + 1];<br>                    arr[j + 1] = temp;<br>                }<br>            }<br>        }<br><br>        // Print sorted array<br>        System.out.print(&quot;Sorted array: &quot;);<br><br>        for (int i = 0; i &lt; arr.length; i++) {<br>            System.out.print(arr[i] + &quot; &quot;);<br>        }<br>    }<br>}</pre><p><strong>Time Complexity:</strong></p><p>Best Case : O(n)</p><p>Worst Case : O(n^2)</p><p>Above the leetcode question you can try to solve own in descending order. ( Use bubble sort ) .</p><blockquote>I hope you will understand…..Best of luck for your journey.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=602fb4c2b27f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Print x to the power of n (Using recursion optimize solution)]]></title>
            <link>https://bypanu.medium.com/print-x-to-the-power-of-n-using-recursion-optimize-solution-ee27b192800a?source=rss-afc8fef92662------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee27b192800a</guid>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[competitive-programming]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[x-to-the-power-of-n]]></category>
            <dc:creator><![CDATA[Pranav Biradar]]></dc:creator>
            <pubDate>Fri, 17 Jul 2026 14:29:00 GMT</pubDate>
            <atom:updated>2026-07-17T14:29:00.064Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/powx-n">Pow(x, n) - LeetCode</a></p><p>It is basic question but using recursion some tricks and formula is there to find the x to the power of n we know that using the for loop it is very easy method in this we use recursive function.</p><p><strong>Problem Statement :</strong></p><p>Find the x to the power of n.</p><p><strong>Example 1:</strong></p><pre>Input: x = 2, n = 10<br>Output: 1024</pre><p><strong>First Approach :</strong></p><p>Some tip to solve any recursive problem:</p><ol><li>Find the recursive function. It is the modified the function that we will find the solution.</li><li>Find the Base case. In this condition that tells the function when to stop calling itself. Without this, the function will run infinitely and crash the program.</li></ol><p>So in this problem,</p><ol><li>Recursive function : x * pow(x , x(n-1)).</li><li>Base Case : x = 0 return 1</li></ol><p>Java code :</p><pre>class Main {<br>  public static int pow(int x,int n){<br>    if(n==0){<br>      return 1;<br>}<br>   return x * pow(x,n-1);<br>}<br>  public static void main (String []args){<br>    int x = 2;<br>    int n = 10;<br>    System.out.println(pow(x,n));<br>  }<br>}</pre><p><strong>Stack Analysis :</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kMZTMVdrJWDjoHZDCBtV_Q.png" /></figure><p><strong>Time Complexity</strong></p><ul><li><strong>O(n)</strong> — There are n + 1 recursive calls.</li></ul><p><strong>Space Complexity</strong></p><ul><li><strong>O(n)</strong> — The call stack stores n + 1 stack frames before the base case is reached.</li></ul><h4><strong>Second Approach :</strong></h4><h4>Optimized Idea</h4><p>Instead of reducing the exponent by <strong>1</strong>, we can reduce it by <strong>half</strong>.</p><p>This is based on two mathematical properties.</p><h4>Case 1: Even Exponent</h4><p>If n is even,</p><pre>xⁿ = (xⁿ⁄²) × (xⁿ⁄²)</pre><h4>Example</h4><pre>2¹⁰</pre><pre>= (2⁵) × (2⁵)</pre><pre>= 32 × 32</pre><pre>= 1024</pre><h4>Case 2: Odd Exponent</h4><p>If n is odd,</p><pre>xⁿ = x × (xⁿ⁻¹⁄²) × (xⁿ⁻¹⁄²)</pre><h4>Example</h4><pre>2⁵</pre><pre>= 2 × (2²) × (2²)</pre><pre>= 2 × 4 × 4</pre><pre>= 32</pre><h4>Why Store halfPower?</h4><p>A common mistake is writing:</p><pre>return pow(x, n / 2) * pow(x, n / 2);</pre><p>Here, pow(x, n/2) is computed <strong>twice</strong>, leading to unnecessary recursive calls.</p><p>Instead, compute it once and reuse it.</p><pre>int halfPower = pow(x, n / 2);</pre><p>Now use:</p><pre>halfPower * halfPower</pre><p>This avoids duplicate work and makes the algorithm much more efficient.</p><p><strong>Optimized Java Code:</strong></p><pre>class Main {<br><br>    public static int pow(int x, int n) {<br><br>        if (n == 0) {<br>            return 1;<br>        }<br><br>        int halfPower = pow(x, n / 2);<br><br>        if (n % 2 == 0) {<br>            return halfPower * halfPower;<br>        } else {<br>            return x * halfPower * halfPower;<br>        }<br>    }<br><br>    public static void main(String[] args) {<br><br>        int x = 2;<br>        int n = 10;<br><br>        System.out.println(pow(x, n));<br>    }<br>}</pre><p><strong>Stack Analysis :</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zYdIDVDbYH9ZTLQjxH6ULA.png" /></figure><h3>Final Thoughts</h3><p>Understanding recursion is important, but understanding <strong>how to optimize recursion</strong> is what distinguishes a good solution from an excellent one. Exponentiation by Squaring is a perfect example of using mathematical insight to dramatically improve performance.</p><p>If you found this article helpful, consider following me on Medium for more articles on Java, Data Structures, Algorithms, and Competitive Programming.</p><p>I hope you will understand.</p><p><em>Best of luck for your journey……</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee27b192800a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Maximum Average Subarray I ( Using fixed size Sliding Window)]]></title>
            <link>https://bypanu.medium.com/maximum-average-subarray-i-using-fixed-size-sliding-window-d19fe61b7c3e?source=rss-afc8fef92662------2</link>
            <guid isPermaLink="false">https://medium.com/p/d19fe61b7c3e</guid>
            <category><![CDATA[maximum-avearge-subarray]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[sliding-window-algorithm]]></category>
            <category><![CDATA[dsa-problem]]></category>
            <category><![CDATA[dsa-patterns]]></category>
            <dc:creator><![CDATA[Pranav Biradar]]></dc:creator>
            <pubDate>Sun, 12 Jul 2026 19:03:55 GMT</pubDate>
            <atom:updated>2026-07-12T19:03:55.270Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://leetcode.com/problems/maximum-average-subarray-i">Maximum Average Subarray I - LeetCode</a></p><p>Sliding Window is the DSA pattern approach which is used in problem like that to find the subarray or substring of contiguous element.</p><p><strong>Problem Statement :</strong></p><p>You are given an integer array nums consisting of n elements, and an integer k.</p><p>Find a contiguous subarray whose <strong>length is equal to</strong> k that has the maximum average value and return <em>this value</em>. Any answer with a calculation error less than 10-5 will be accepted.</p><p><strong>Example 1:</strong></p><pre>Input: nums = [1,12,-5,-6,50,3], k = 4<br>Output: 12.75000<br>Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75</pre><p><strong>Example 2:</strong></p><pre>Input: nums = [5], k = 1<br>Output: 5.00000</pre><p><strong>Constraints:</strong></p><ul><li>n == nums.length</li><li>1 &lt;= k &lt;= n &lt;= 105</li><li>-104 &lt;= nums[i] &lt;= 104</li></ul><p><strong>Problem Statement Explanation:</strong></p><p><strong>Input :</strong></p><p>Array is given nums and k integer is given as a input and k is the length of the subarray.</p><p><strong>To Find:</strong></p><p>We have to find that maximum sum of the average element which is the subarray of the nums which is k length of the subarray.</p><p>Example,</p><pre>Input: nums = [1,12,-5,-6,50,3], k = 4<br>Output: 12.75000<br>Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75</pre><p>In the nums [ 1,12,-5,-6,50,3] , k is the length of subarray which is 4 in this example,</p><p>subarray 1 : [1,12,-5,-6] here sum is 1+12 -5 -6 = 2 maximum sum is 2</p><p>subarray 2 : [12,-5,-6,50] here sum is 12–5–6+50 = 51 maximum sum is 51</p><p>subarray 3 : [-5,-6,50,3] here sum is -5–6+50+3 = 42 maximum sum is 42</p><p>subarray 4 : [-6,50,3] this subarray is not valid because it’s subarray length is 3 and we want it length 4 .</p><p>Then in conclusion maximum subarray of sum is 51 which is subarray 2 . Then we have calculate the average of the sum that 51/4 = 12.75000.</p><p>And output will be : 12.75000.</p><p><strong>My Intuition:</strong></p><p>We use<em> </em><strong><em>Fixed Sliding Window Pattern ,</em></strong></p><p>Why Sliding Window Pattern use ?</p><p>Sliding Window solve the problem of repeatable task in the example of ,</p><pre>Input: nums = [1,12,-5,-6,50,3], k = 4<br>Output: 12.75000<br>Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75</pre><p>In the brute force approach,</p><pre>public class Solution {<br>    public double findMaxAverageBruteForce(int[] nums, int k) {<br>        double maxSum = 0;<br>        int n = nums.length;<br>        <br>        for (int i = 0; i &lt;= n - k; i++) {<br>            int currentSum = 0;<br>            for (int j = i; j &lt; i + k; j++) {<br>                currentSum += nums[j];<br>            }<br>            maxSum = Math.max(maxSum, currentSum);<br>        }<br>        <br>        return maxSum / k;<br>    }<br>}</pre><p>In Brute Force approach we calculate same element sum every iterator for example,</p><p>1st Iterator : 1 + 12 -5 -6 = 2</p><p>2nd Iterator : 12 -5 -6 + 50 = 51</p><p>in 2nd iterator 12–5–6 is calculate in 1st iterator also then we do need to repeat calculate in 2nd iterator , we can also do that 1 element is remove and add next element that is 50.</p><p>So in this situation come Sliding Window .</p><p><strong><em>Conclusion of Sliding Window is :</em></strong></p><p>One element is remove</p><p>One element is add .</p><p><strong><em>Java Code Optimization:</em></strong></p><pre>public class Solution {<br>    public double findMaxAverageOptimize(int[] nums, int k) {<br>        int sum =0;<br>        for(int i=0;i&lt;k;i++){<br>            sum = sum + nums[i];<br>        }<br>        int max_sum =sum;<br>        for(int i=k;i&lt;nums.length;i++){<br>            sum = sum + nums[i] - nums[i-k];<br>            if(sum&gt;max_sum){<br>                max_sum = sum;<br>            }<br>        }<br>        return (double)max_sum/k;<br>    }<br>}</pre><p>In the brute force approach, when you shift your view from one window to the next, you recalculate the sum of the overlapping elements.</p><p>The sliding window technique eliminates this redundancy. Think of it like a moving frame: [<a href="https://levelup.gitconnected.com/understanding-the-sliding-window-technique-a-comprehensive-guide-with-examples-and-real-world-b54c9c2695b9">1</a>, <a href="https://unstop.com/blog/sliding-window-algorithm">2</a>]</p><ol><li><strong>Compute</strong> the sum of the very first window of size k.</li><li><strong>Slide</strong> the window one position to the right.</li><li><strong>Update</strong> the sum efficiently: <strong>Subtract</strong> the element leaving the window from the left, and <strong>add</strong> the new element entering from the right. [<a href="https://medium.com/@raveenpanditha/dsa-patterns-01-sliding-window-pattern-complete-guide-d8aaca74e266">1</a>, <a href="https://levelup.gitconnected.com/prefix-sum-and-sliding-window-most-common-array-problem-soving-techniques-7d5c78d433e0">2</a>, <a href="https://levelup.gitconnected.com/understanding-the-sliding-window-technique-a-comprehensive-guide-with-examples-and-real-world-b54c9c2695b9">3</a>, <a href="https://www.educative.io/answers/how-to-implement-a-sliding-window-algorithm-in-cpp">4</a>, <a href="https://blog.devgenius.io/dsa-preparation-pattern-wise-day-1-sliding-window-c27f12685261">5</a>]</li></ol><p><strong><em>Step-by-Step Walkthrough</em></strong></p><p>Consider this example array: nums = [1, 12, -5, -6, 50, 3], with k = 4.</p><p><strong>Step 1: Establish the Baseline Window</strong></p><p>Calculate the sum of the first k elements (indices 0 to 3). [<a href="https://medium.com/@sevvalkatirci99/boost-your-algorithm-skills-with-sliding-window-in-c-d3cb98edcbcb">1</a>]</p><ul><li><strong>Window:</strong> [1, 12, -5, -6]</li><li><strong>Current Sum:</strong> 1 + 12 + (-5) + (-6) = 2</li><li><strong>Max Sum tracked:</strong> 2</li></ul><p><strong>Step 2: Slide to the Second Window</strong></p><p>Shift the window to indices 1 to 4. The element 1 leaves the left side, and 50 enters the right side.</p><ul><li><strong>Update formula:</strong> New Sum = Previous Sum - Left Element + Right Element</li><li><strong>Calculation:</strong> 2 - 1 + 50 = 51</li><li><strong>Window looks like:</strong> [12, -5, -6, 50]</li><li><strong>Max Sum tracked:</strong> Update from 2 to 51</li></ul><p><strong>Step 3: Slide to the Third Window</strong></p><p>Shift the window to indices 2 to 5. The element 12 leaves, and 3 enters.</p><ul><li><strong>Calculation:</strong> 51 - 12 + 3 = 42</li><li><strong>Window looks like:</strong> [-5, -6, 50, 3]</li><li><strong>Max Sum tracked:</strong> Stays 51 (since 51 &gt; 42)</li></ul><p><strong>Step 4: Calculate the Final Average</strong></p><p>The loop finishes because the window has reached the end of the array.</p><ul><li><strong>Final Result:</strong> 51 / 4 = 12.75</li></ul><p><strong><em>Complexity Analysis:</em></strong></p><ul><li><strong>Time Complexity:</strong> {O}(n). We pass through the array exactly once.</li><li><strong>Space Complexity:</strong> {O}(1). No extra space or secondary data structures are created.</li></ul><p><em>I hope you will understand . Best of luck for your journey …….</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d19fe61b7c3e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sort Colors (LeetCode 75) – Frequency Count Approach]]></title>
            <link>https://bypanu.medium.com/sort-colors-leetcode-75-frequency-count-approach-4c1305c22a0d?source=rss-afc8fef92662------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c1305c22a0d</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[dsa-patterns]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[sort-colors]]></category>
            <dc:creator><![CDATA[Pranav Biradar]]></dc:creator>
            <pubDate>Sat, 11 Jul 2026 18:53:32 GMT</pubDate>
            <atom:updated>2026-07-11T18:53:32.639Z</atom:updated>
            <content:encoded><![CDATA[<p>Introduction</p><p>The Sort Colors problem is one of the most popular array problems on LeetCode. The array contains only three distinct values: 0, 1, and 2, where 0 represents Red, 1 represents White, and 2 represents Blue. The objective is to sort the array so that all 0s appear first, followed by all 1s, and finally all 2s.</p><p>One of the simplest ways to solve this problem is by using the Frequency Count approach, which is based on the idea of Counting Sort.</p><p>---</p><p>How the Frequency Count Approach Works</p><p>The main idea is very straightforward. Instead of comparing or swapping elements, we first count how many times each value appears in the array.</p><p>Since the array contains only three possible values, we maintain three counters:</p><p>- zero to count the number of 0s<br>- one to count the number of 1s<br>- two to count the number of 2s</p><p>We traverse the array once and update these counters according to the current element.</p><p>For example, if the input array is:</p><p>&quot;[2, 0, 2, 1, 1, 0]&quot;</p><p>After scanning the array, we obtain the following frequencies:</p><p>- Number of 0s = 2<br>- Number of 1s = 2<br>- Number of 2s = 2</p><p>At this point, we know exactly how many times each value should appear in the sorted array.</p><p>---</p><p>Reconstructing the Array</p><p>Once the frequencies are known, we overwrite the original array.</p><p>First, we fill the array with all the 0s.</p><p>Next, we fill the remaining positions with all the 1s.</p><p>Finally, we fill the last positions with all the 2s.</p><p>After rewriting the array, the final result becomes:</p><p>&quot;[0, 0, 1, 1, 2, 2]&quot;</p><p>Since every element is written exactly once in sorted order, the array becomes completely sorted.</p><p>---</p><p>Why This Approach Works?</p><p>This algorithm does not rely on comparisons between elements. Instead, it counts how many times each value appears and then reconstructs the array using those counts.</p><p>Because the problem guarantees that the array contains only the values 0, 1, and 2, counting their frequencies is sufficient to produce the correct sorted order.</p><p>---</p><p>Time Complexity</p><p>The algorithm consists of two passes over the array.</p><p>During the first pass, we count the occurrences of each value. This requires O(n) time because every element is visited exactly once.</p><p>During the second pass, we rewrite the array with the counted values. Every position in the array is updated exactly once, which also requires O(n) time.</p><p>Therefore, the overall Time Complexity is:</p><p>O(n)</p><p>---</p><p>Space Complexity</p><p>The algorithm uses only three integer variables to store the frequencies of 0, 1, and 2.</p><p>No additional arrays, lists, or hash maps are created.</p><p>Therefore, the Space Complexity is:</p><p>O(1)</p><p>---</p><p>Advantages</p><p>The Frequency Count approach is simple and easy to understand, making it an excellent solution for beginners. It runs in linear time, uses constant extra space, and avoids unnecessary comparisons or swaps. Because of its simplicity, it is often the first approach taught for solving the Sort Colors problem.</p><p>---</p><p>Limitations</p><p>This method works efficiently because the array contains only three distinct values. If the array contained a large range of different numbers, maintaining a separate counter for every possible value would not be practical without using additional memory.</p><p>Java Code :</p><pre>class Solution {<br>    public void sortColors(int[] nums) {<br><br>        int zero = 0;<br>        int one = 0;<br>        int two = 0;<br><br>        // Step 1: Count the frequency of each color<br>        for (int num : nums) {<br>            if (num == 0) {<br>                zero++;<br>            } else if (num == 1) {<br>                one++;<br>            } else {<br>                two++;<br>            }<br>        }<br><br>        // Step 2: Rewrite the array<br>        int index = 0;<br><br>        while (zero &gt; 0) {<br>            nums[index] = 0;<br>            index++;<br>            zero--;<br>        }<br><br>        while (one &gt; 0) {<br>            nums[index] = 1;<br>            index++;<br>            one--;<br>        }<br><br>        while (two &gt; 0) {<br>            nums[index] = 2;<br>            index++;<br>            two--;<br>        }<br>    }<br>}</pre><p>---</p><p>Complexity Summary</p><p>Time Complexity: O(n)</p><p>Space Complexity: O(1)</p><p>---</p><p>Conclusion</p><p>The Frequency Count approach is one of the easiest ways to solve the Sort Colors problem. The algorithm first counts the occurrences of 0s, 1s, and 2s, and then reconstructs the array in sorted order. Since every element is processed a constant number of times, the solution runs in O(n) time while using only O(1) extra space.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c1305c22a0d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>