<?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 Mallikarjuna on Medium]]></title>
        <description><![CDATA[Stories by Mallikarjuna on Medium]]></description>
        <link>https://medium.com/@spiderman05116?source=rss-bbe44895418c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1c3BQUM8nIg6u4Dqim743w.jpeg</url>
            <title>Stories by Mallikarjuna on Medium</title>
            <link>https://medium.com/@spiderman05116?source=rss-bbe44895418c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 23:14:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@spiderman05116/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[Gas Station (Greedy-problem)]]></title>
            <link>https://medium.com/@spiderman05116/gas-station-greedy-problem-94247174ef64?source=rss-bbe44895418c------2</link>
            <guid isPermaLink="false">https://medium.com/p/94247174ef64</guid>
            <category><![CDATA[greedy-approach]]></category>
            <category><![CDATA[gas-station]]></category>
            <dc:creator><![CDATA[Mallikarjuna]]></dc:creator>
            <pubDate>Sun, 30 Mar 2025 15:04:25 GMT</pubDate>
            <atom:updated>2025-03-30T15:04:25.018Z</atom:updated>
            <content:encoded><![CDATA[<p>There are some gas stations along a circular route. You are given two integer arrays <strong>gas[] </strong>denoted as the amount of gas present at each station and <strong>cost[] </strong>denoted as the cost of travelling to the next station. You have a car with an unlimited gas tank. You begin the journey with an empty tank from one of the gas stations. Return the index of the starting gas station if it’s possible to travel around the circuit without running out of gas at any station in a <strong>clockwise direction</strong>. If there is no such starting station exists, <strong>return -1</strong>.<br>Note:<strong> </strong>If a solution exists, it is guaranteed to be unique.</p><p><strong>Examples:</strong></p><pre>Input: gas[] = [4, 5, 7, 4], cost[]= [6, 6, 3, 5]<br>Output: 2<br>Explanation: It is possible to travel around the circuit from station at index 2. Amount of gas at station 2 is (0 + 7) = 7.<br>Travel to station 3. Available gas = (7 – 3 + 4) = 8.<br>Travel to station 0. Available gas = (8 – 5 + 4) = 7.<br>Travel to station 1. Available gas = (7 – 6 + 5) = 6.<br>Return to station 2. Available gas = (6 – 6) = 0.</pre><pre>Input: gas[] = [1, 2, 3, 4, 5], cost[] = [3, 4, 5, 1, 2]<br>Output: 3<br>Explanation: It is possible to travel around the circuit from station at index 3. Amount of gas at station 3 is (0 + 4) = 4.<br>Travel to station 4. Available gas = 4 – 1 + 5 = 8.<br>Travel to station 0. Available gas = 8 – 2 + 1 = 7.<br>Travel to station 1. Available gas= 7 – 3 + 2 = 6.<br>Travel to station 2. Available gas = 6 – 4 + 3 = 5.<br>Travel to station 3. The cost is 5. The gas is just enough to travel back to station 3.</pre><pre>Input: gas[] = [3, 9], cost[] = [7, 6]<br>Output: -1<br>Explanation: There is no gas station to start with such that you can complete the circuit.</pre><p><strong>Constraints:<br></strong>1 ≤ gas.size(), cost.size() ≤ 10^6<br>1 ≤ gas[i], cost[i] ≤ 10^3</p><h3>Naive Approach:</h3><p>class Solution {<br>public:<br> int startStation(vector&lt;int&gt; &amp;gas, vector&lt;int&gt; &amp;cost) {<br> int n = gas.size();<br> <br> // Step 1: Extend arrays to simulate circular route<br> for (int i = 0; i &lt; n — 1; i++) {<br> gas.push_back(gas[i]);<br> cost.push_back(cost[i]);<br> }</p><p>// Step 2: Try starting from each index<br> for (int i = 0; i &lt; n; i++) {<br> int currentGas = 0;<br> bool valid = true;</p><p>// Step 3: Travel n stations from the starting index<br> for (int j = i; j &lt; i + n; j++) {<br> currentGas += gas[j] — cost[j];<br> if (currentGas &lt; 0) { // Step 4: If gas goes negative, current start fails<br> valid = false;<br> break;<br> }<br> }<br> if (valid) return i; // Step 5: Return index if valid<br> }<br> return -1; // Step 6: No valid start found<br> }<br>};</p><h3>Greedy Approach:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hLBO1x9-Qylc7saX15f2xA.png" /></figure><h3>Why Does This Approach Always Work?</h3><h3>✅ Step 1: Understand the Naive Approach</h3><p>The naive approach often involves:</p><ul><li><strong>Brute-force search:</strong> Trying all possible solutions.</li><li><strong>Nested loops:</strong> Often leading to O(n²)or worse complexity.</li><li><strong>Redundant computations:</strong> Checking the same condition multiple times.</li></ul><h3>🚗 Gas Station Problem — Naive Approach Recap</h3><ol><li>Start from every index i in the array.</li><li>Check if you can complete a full circuit starting from i.</li><li>If yes, return i; otherwise, continue.</li><li>Time complexity: O(n²).</li></ol><h3>✅ Step 2: Identify Repeated Work or Overlap</h3><p>Before moving to a greedy approach, <strong>observe patterns</strong> in your naive solution:</p><ul><li>Are calculations repeated?</li><li>Can partial results be reused?</li><li>Does a local decision guarantee a global optimal solution?</li><li>In the gas station problem:</li><li>Checking from every index is inefficient.</li><li>Notice that if a start index fails, no index between start and failure can work.</li></ul><h3>✅ Step 3: Make Observations and Simplify</h3><p>For the gas station problem:</p><ol><li>If you cannot reach station j from station i, starting from any index between i and j is impossible</li><li>Instead of testing every index, focus on cumulative gas and cost.</li></ol><h3>✨ Key Insight:</h3><ul><li>If total gas available &gt;= total cost, a solution must exist.</li><li>If running out of gas at a station, skip all stations between start and failure.</li><li>Reset the start index to the next station.</li></ul><h3>✅ Step 4: Apply Greedy Approach</h3><p>The greedy approach often involves:</p><ol><li>Making the <strong>best local decision</strong> at each step.</li><li>Discarding impossible scenarios quickly.</li><li>Accumulating information efficiently.</li></ol><h3>🚗 Gas Station Problem — Greedy Approach</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/1*GNCcPEf3kAIsKXyjIjpdCg.png" /></figure><h3>✅ Step 5: General Approach for Problems</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/925/1*8FjzMcTq-GIE82q2Pq5XOA.png" /></figure><h3>🏆 Applying Greedy Methodology to Other Problems:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/827/1*N4_KiiDyR-4Wf0PNZTlTeA.png" /></figure><h3>🎉 Summary:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*qmAICfPIDZCsD3u5_B_hGA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94247174ef64" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Maximum Number of Points From Grid Queries(Leetcode : 2503)]]></title>
            <link>https://medium.com/@spiderman05116/maximum-number-of-points-from-grid-queries-leetcode-2503-a9143d6e883a?source=rss-bbe44895418c------2</link>
            <guid isPermaLink="false">https://medium.com/p/a9143d6e883a</guid>
            <dc:creator><![CDATA[Mallikarjuna]]></dc:creator>
            <pubDate>Fri, 28 Mar 2025 16:42:11 GMT</pubDate>
            <atom:updated>2025-03-28T16:42:11.102Z</atom:updated>
            <content:encoded><![CDATA[<h3><a href="https://leetcode.com/problems/maximum-number-of-points-from-grid-queries/">Maximum Number of Points From Grid Queries</a>(Leetcode : 2503)</h3><p>You are given an m x n integer matrix grid and an array queries of size k.</p><p>Find an array answer of size k such that for each integer queries[i] you start in the <strong>top left</strong> cell of the matrix and repeat the following process:</p><ul><li>If queries[i] is <strong>strictly</strong> greater than the value of the current cell that you are in, then you get one point if it is your first time visiting this cell, and you can move to any <strong>adjacent</strong> cell in all 4 directions: up, down, left, and right.</li><li>Otherwise, you do not get any points, and you end this process.</li></ul><p>After the process, answer[i] is the <strong>maximum</strong> number of points you can get. <strong>Note</strong> that for each query you are allowed to visit the same cell <strong>multiple</strong> times.</p><p>Return <em>the resulting array</em> answer.</p><p><strong>Example 1:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/0*TOGDjlTVCqti0dPr.png" /></figure><pre>Input: grid = [[1,2,3],[2,5,7],[3,5,1]], queries = [5,6,2]<br>Output: [5,8,1]<br>Explanation: The diagrams above show which cells we visit to get points for each query.</pre><p><strong>Constraints:</strong></p><ul><li>m == grid.length</li><li>n == grid[i].length</li><li>2 &lt;= m, n &lt;= 1000</li><li>4 &lt;= m * n &lt;= 105</li><li>k == queries.length</li><li>1 &lt;= k &lt;= 104</li><li>1 &lt;= grid[i][j], queries[i] &lt;= 106</li></ul><h3>*BRUTE-FORCE APPRROACH:</h3><h4>let’s see the BFS approach for this problem…</h4><p>we are taking the queue&lt;pair&lt;int,int&gt;&gt; for stoing the i,j index of particular value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*0rLikemaCrKVopKjesFIjA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*M-YSHYKUwcbSAAyMEjHGAA.png" /></figure><p>For each query we need to iterate over the grid in order to find the values which are less then queries value.</p><p><strong>Worst-Case Complexity of Current Approach:</strong></p><ul><li>Time Complexity : O(Q * (m*n))</li><li>[ from constraintsQ ≤ 10⁵ and m*n ≤ 10⁵ ]</li><li>In the worst case, this could be 10⁴ ×10⁵ =10⁹ operations, which is far too slow.</li></ul><h4>let’s see the DFS approach for this problem…</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dzNX3PLjalnZ6I7v0O1DsA.png" /></figure><p>DFS code is generally smaller then BFS but both takes the same time complexity.</p><h3>*OPTIMAL APPROACH:</h3><p>As we are iterating the matrix over and over for each query we are getting the TLE.so we are avoiding much iteration by sorting the queries and for the result at ith index query we use the (i-1)th index query result.</p><p>we are using the min heap for getting the min element at ith iteration and move forward if it is less then the queryValue otherwise directly update the result with before points.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*1h01aGVatbpz2yG-Onu1Eg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*T8y0z5fx3yjsowyXDka9wg.png" /></figure><p>Time Complexity : O(QlogQ + (m×n)log(m×n))</p><p>You use a priority queue (min-heap) to store grid values with coordinates. Each push and pop operation on the heap costs O(log⁡S), where S is the size of the heap.</p><p>where QlogQ is for sorting and (m×n)log(m×n)) for insertions and deletions in heap of size (m×n) the Time complexity is log(m×n) and we are doing it in (m×n) elements of grid.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a9143d6e883a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Minimum Platforms (GFG)]]></title>
            <link>https://medium.com/@spiderman05116/minimum-platforms-gfg-e16462c580da?source=rss-bbe44895418c------2</link>
            <guid isPermaLink="false">https://medium.com/p/e16462c580da</guid>
            <dc:creator><![CDATA[Mallikarjuna]]></dc:creator>
            <pubDate>Thu, 27 Mar 2025 14:21:36 GMT</pubDate>
            <atom:updated>2025-03-27T14:21:36.874Z</atom:updated>
            <content:encoded><![CDATA[<p>I solved a problem from GFG today with naive and optimal approach.Let’s see how I go from naive to optimal..</p><h4>Problem Statement:</h4><p>You are given the arrival times <strong>arr[]</strong> and departure times <strong>dep[]</strong> of all trains that arrive at a railway station on the same day. Your task is to determine the minimum number of platforms required at the station to ensure that no train is kept waiting.</p><p>At any given time, the same platform cannot be used for both the arrival of one train and the departure of another. Therefore, when two trains arrive at the same time, or when one arrives before another departs, additional platforms are required to accommodate both trains.</p><p><strong>Examples:</strong></p><pre>Input: arr[] = [900, 940, 950, 1100, 1500, 1800], dep[] = [910, 1200, 1120, 1130, 1900, 2000]<br>Output: 3<br>Explanation: There are three trains during the time 9:40 to 12:00. So we need a minimum of 3 platforms.</pre><pre>Input: arr[] = [900, 1235, 1100], dep[] = [1000, 1240, 1200]<br>Output: 1<br>Explanation: All train times are mutually exclusive. So we need only one platform</pre><pre>Input: arr[] = [1000, 935, 1100], dep[] = [1200, 1240, 1130]<br>Output: 3<br>Explanation: All 3 trains have to be there from 11:00 to 11:30</pre><pre>Constraints:<br>1≤ number of trains ≤ 50000<br>0000 ≤ arr[i] ≤ dep[i] ≤ 2359<br>Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters represent hour (between 00 to 23 ) and the last two characters represent minutes (this will be &lt;= 59 and &gt;= 0).</pre><p>From the above examples and prroblem statement we understand that when ever the arrival time of a train is lesser then the departer time of another train then it should be in another train track.</p><h3>//Brute-Force Approach</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/927/1*qz2lPRCMsejuzh6yMhDPYg.png" /></figure><p><strong>Time complexity is O(N²)</strong> due to the nested loops when merging overlapping intervals, which leads to <strong>TLE (Time Limit Exceeded)</strong> for large inputs. Instead, you should use an <strong>optimized sweep-line approach</strong> with sorting and a min-heap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*un4TLUR9alm6ppOgVZ2wSg.png" /></figure><h3>Time Complexity Analysis</h3><ul><li><strong>Sorting</strong> arrival and departure arrays → <strong>O(N log N)</strong></li><li><strong>Iterating through trains</strong> and using a min-heap → <strong>O(N log N)</strong></li><li><strong>Overall Complexity</strong>: <strong>O(N log N)</strong> (Much faster than O(N²))</li></ul><h3>Sweep Line Algorithm (Event-Based Processing)</h3><p>The <strong>Sweep Line Algorithm</strong> is an efficient technique used to solve problems involving <strong>intervals</strong> (start and end times) by sweeping a “vertical line” across sorted events in increasing order of time.</p><blockquote>🔹 When to Use Sweep Line Algorithm?</blockquote><blockquote>Use it when:</blockquote><blockquote>✔ <strong>You have a set of intervals with start &amp; end times</strong><br> ✔ <strong>You need to count overlapping intervals efficiently</strong><br> ✔ <strong>You need to process events in sorted order</strong></blockquote><blockquote>🔹 <strong>Common Problems:</strong></blockquote><blockquote><strong>Train Platform Problem</strong> 🚉</blockquote><blockquote><strong>Meeting Rooms (Leetcode 253)</strong> 📅</blockquote><blockquote><strong>Maximum Overlapping Intervals</strong> 📊</blockquote><blockquote><strong>Skyline Problem (Leetcode 218)</strong> 🏙</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e16462c580da" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 4-LeetCode]]></title>
            <link>https://medium.com/@spiderman05116/day-4-leetcode-74b8eb56a197?source=rss-bbe44895418c------2</link>
            <guid isPermaLink="false">https://medium.com/p/74b8eb56a197</guid>
            <category><![CDATA[sad]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[potd]]></category>
            <dc:creator><![CDATA[Mallikarjuna]]></dc:creator>
            <pubDate>Fri, 13 Dec 2024 14:26:56 GMT</pubDate>
            <atom:updated>2024-12-13T14:26:56.516Z</atom:updated>
            <content:encoded><![CDATA[<h3>Problem: Find Score of an Array After Marking All Elements</h3><p>You are given an array nums consisting of positive integers.</p><p>Starting with score = 0, apply the following algorithm:</p><ul><li>Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.</li><li>Add the value of the chosen integer to score.</li><li>Mark <strong>the chosen element and its two adjacent elements if they exist</strong>.</li><li>Repeat until all the array elements are marked.</li></ul><p>Return <em>the score you get after applying the above algorithm</em>.</p><h3>Intuition:</h3><p>As we need to mark the adjacent indices along with the index we should use the Vector to now which index is marked or not.</p><p>According to the problem we need to sort the array to get the smallest value first but When we are sorting an array we obviously lose the connection between the index and the element of that index in array so in order to make sure that the element index is attached with the element we should create an vector whose element is a pair of both the array element value and its index.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_xlt1e72_4cgL93HqPAkIw.png" /></figure><h4>Algorithm:</h4><ul><li>create an vector elements with pair as element such that pair&lt;array[index],index&gt;.</li><li>sort that vector.</li><li>Now create an visited vector of size of elements vector and initilize as false as we are not marking any index.</li><li>iterate the elements vector, add the value to answer and mark the vector element index and index before and after if exist in visisted vector.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/906/1*t0JHnMFF0Qx4GtRxhJH1hw.png" /></figure><p><strong>Time Complexity is O(n log n)</strong></p><p>sorting takes the O(nlogn)</p><p><strong>Space Complexity is O(n)</strong></p><p>hope this is helpful..</p><p>thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74b8eb56a197" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 3 - LeetCode]]></title>
            <link>https://medium.com/@spiderman05116/day-3-leetcode-c49fe040a246?source=rss-bbe44895418c------2</link>
            <guid isPermaLink="false">https://medium.com/p/c49fe040a246</guid>
            <category><![CDATA[sad]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[easy]]></category>
            <category><![CDATA[potd]]></category>
            <category><![CDATA[max-heap]]></category>
            <dc:creator><![CDATA[Mallikarjuna]]></dc:creator>
            <pubDate>Thu, 12 Dec 2024 13:11:16 GMT</pubDate>
            <atom:updated>2024-12-12T13:11:16.346Z</atom:updated>
            <content:encoded><![CDATA[<h3>Problem: Take Gifts From the Richest Pile</h3><p>You are given an integer array gifts denoting the number of gifts in various piles. Every second, you do the following:</p><ul><li>Choose the pile with the maximum number of gifts.</li><li>If there is more than one pile with the maximum number of gifts, choose any.</li><li>Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts.</li></ul><p>Return <em>the number of gifts remaining after </em>k<em> seconds.</em></p><h3>Intuition:</h3><p>As we need to take the <strong><em>maximum number of gifts</em> <em>pile</em></strong><em> </em>to make that into a <strong><em>floor of square root of that number</em></strong> for up-to k times we need to use the <strong>max heap</strong> data structure.</p><p>Max heap : It is a priority queue which always stores the maximum value at the top of the queue.</p><h3>Algorithm:</h3><ul><li>Create a max heap.</li><li>Push all the elements of given array into the max heap.</li><li>Up-to K number of times, take the top of max heap and convert into the floor of square root of that value then push that value into the max heap.</li><li>Shift all the elements of heap into the array.</li><li>Find the sum of all the elements then return that sum.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*GQYyd3NrnGkDpkwAa6Gr8g.png" /></figure><h3>Time complexity : O(n)</h3><p>As we iterate whole array.</p><h3>Space complexity : O(n)</h3><p>As we use the max heap data structure to store the value.</p><p>Hope you got this , thank you for reading …</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c49fe040a246" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 2 of  LeetCode]]></title>
            <link>https://medium.com/@spiderman05116/day-2-of-leetcode-cc9dab9a90fc?source=rss-bbe44895418c------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc9dab9a90fc</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[dequeue]]></category>
            <category><![CDATA[sad]]></category>
            <dc:creator><![CDATA[Mallikarjuna]]></dc:creator>
            <pubDate>Wed, 11 Dec 2024 15:30:16 GMT</pubDate>
            <atom:updated>2024-12-11T15:30:16.131Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Day 2 of LeetCode</strong></h3><h3>Problem :</h3><p>You are given a <strong>0-indexed</strong> array nums and a <strong>non-negative</strong> integer k.</p><p>In one operation, you can do the following:</p><ul><li>Choose an index i that <strong>hasn&#39;t been chosen before</strong> from the range [0, nums.length - 1].</li><li>Replace nums[i] with any integer from the range [nums[i] - k, nums[i] + k].</li></ul><p>The <strong>beauty</strong> of the array is the length of the longest subsequence consisting of equal elements.</p><p>Return <em>the </em><strong><em>maximum</em></strong><em> possible beauty of the array </em>nums<em> after applying the operation any number of times.</em></p><p><strong>Note</strong> that you can apply the operation to each index <strong>only once</strong>.</p><p>A <strong>subsequence</strong> of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements.</p><h3>Approach 1:</h3><p>Here beauty is depends on the longest subsequence with equal elements.</p><p>For every element nums[i] we have a range to replace that with any of the range [nums[i] - k, nums[i] + k] element.</p><p>so we have to find the overlapping ranges to find the beauty.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*LhFZ5_UZNL0GTd9At1A3SQ.png" /><figcaption>as the two intervals overlapping the beauty is two</figcaption></figure><p>Algorithm:</p><p>→ find the intervals</p><p>→ sort the intervals</p><p>→find the number of intervals intersecting</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FVunY_iq3sdn00qgXh7tGg.png" /><figcaption>with time complexity O(n logn)</figcaption></figure><p>Deque is used to find the number of intervals intersecting. Actually we can do this with nested for loops also but it increase the time complexity.</p><p>As constraints size is 10⁵, O(n²) time complexity may not accepted.</p><h3>Approach 2:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IVSgZA6oxu1nNYcV8v1dlg.png" /></figure><p>Instead the forming the intervals array find the farest element (y) to the element (x) to make the beauty more.</p><p>Algorithm:</p><p>→sort the given array</p><p>→find the element (y) for every element (x) with the formula, x = current</p><p>element and y = x + 2* k</p><p>→use the binary search to find the farest index which support the y ≤ x+2*k</p><p>→update the size</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/953/1*EeInKKVSnkNf-FY5Nv1aYg.png" /></figure><p>hope this is helpful..</p><p>Thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc9dab9a90fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 1 of leetcode]]></title>
            <link>https://medium.com/@spiderman05116/day-1-of-doing-leetcode-ad40c2cc6aaa?source=rss-bbe44895418c------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad40c2cc6aaa</guid>
            <dc:creator><![CDATA[Mallikarjuna]]></dc:creator>
            <pubDate>Tue, 10 Dec 2024 13:28:16 GMT</pubDate>
            <atom:updated>2024-12-13T14:05:57.246Z</atom:updated>
            <content:encoded><![CDATA[<p>Problem:Longest palindromic substring (5)</p><h3>Approach 1:</h3><p>First I implemented a solution using naive approach. The time complexity of it is O(n^3).</p><p>First generate all the substrings [O(n^2)] and then check whether the substring is palindrome or not [O(n)].If it is palindrome then consider it’s length.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*LbTyHOuXJkSQbUR8qlBXtQ.png" /></figure><p>As the constraints size is 10³, O(n^3) time complexity is not accepted.</p><h3>Approach 2:</h3><p>In this condition, we need to reduce the for loops. As we are checking the string again and again to know it is palindrome or not we can use the dynamic programming technique to reduce the for loop.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/719/1*3_VO6fthM6lE8byf9xZp8Q.png" /></figure><p>It’s accepted but the problem with this is the space complexity. It’s has space complexity of O(n^3) and time complexity of O(n^2).</p><h3>Approach 3:</h3><p>We can reduce the space time complexity by using <strong>Expand-Around —Centre</strong> method.For odd palindrome string there is only one center but for even palindrome string there are two centers. So the left and right centers for odd palindrome is same but is different for even palindromes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z-z4Q_NDwUCp4OsiuZwGOQ.png" /></figure><p>Every time the centre character of the left and right part of the string is same then decrement the left(l -- ) and increment the right centers (r++).</p><p>Hope this is helpful.</p><p>We will meet tomorrow.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad40c2cc6aaa" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>