<?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 CHAKRIKA ANOUSHA on Medium]]></title>
        <description><![CDATA[Stories by CHAKRIKA ANOUSHA on Medium]]></description>
        <link>https://medium.com/@anousha.21bce7250?source=rss-d03ebccce2df------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Q0sT2JyG4nGOvZP9</url>
            <title>Stories by CHAKRIKA ANOUSHA on Medium</title>
            <link>https://medium.com/@anousha.21bce7250?source=rss-d03ebccce2df------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 22:06:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anousha.21bce7250/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[2418. Sort the People]]></title>
            <link>https://medium.com/@anousha.21bce7250/2418-sort-the-people-b5a5db063c7e?source=rss-d03ebccce2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/b5a5db063c7e</guid>
            <category><![CDATA[hashmap-in-java]]></category>
            <category><![CDATA[leetcode-easy]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[sorting]]></category>
            <dc:creator><![CDATA[CHAKRIKA ANOUSHA]]></dc:creator>
            <pubDate>Tue, 23 Jul 2024 18:25:46 GMT</pubDate>
            <atom:updated>2024-07-23T18:25:46.067Z</atom:updated>
            <content:encoded><![CDATA[<p>A Beginner Friendly approach using HashMap</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*8ziqveiVrwztFbsKVybsfw.png" /><figcaption>Code Performance</figcaption></figure><h3>Problem Description</h3><p><strong>Level: Easy | Topics: </strong><a href="https://leetcode.com/tag/array/"><strong>Array</strong></a><strong>, </strong><a href="https://leetcode.com/tag/hash-table/"><strong>HashTable</strong></a><strong>, </strong><a href="https://leetcode.com/tag/string/"><strong>String</strong></a><strong>, </strong><a href="https://leetcode.com/tag/sorting/"><strong>Sorting</strong></a></p><p>You are given two arrays:</p><ul><li><strong>names</strong>: an array of strings representing people&#39;s names.</li><li><strong>heights</strong>: an array of integers representing the heights of these people.</li></ul><p>The goal is to return the names array sorted in <strong>descending</strong> order by the people&#39;s heights.</p><h3>Example:</h3><pre>Input: names = [&quot;Mary&quot;,&quot;John&quot;,&quot;Emma&quot;], heights = [180,165,170]<br>Output: [&quot;Mary&quot;,&quot;Emma&quot;,&quot;John&quot;]<br>Explanation: Mary is the tallest, followed by Emma and John.</pre><h3>Visualizing the solution:</h3><h4>1. Storing the data in Hashmap:</h4><p>map = &lt;(name1, height1), (name2, height2)…&gt;</p><pre>HashMap&lt;Integer, String&gt; map = new HashMap&lt;&gt;();<br>        for(int i =0; i&lt;n; i++){<br>            //using hashmap method - put<br>            //key : heights[i] &amp;&amp; value: names[i]<br>            map.put(heights[i], names[i]);<br>         }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/509/1*Yu1T3ULrJwpRP1mzUfiOhg.png" /><figcaption>HashMap Data Stroing</figcaption></figure><p><strong>2. Sorting the height array in descending order:</strong></p><pre>//using Arrays methods<br>Arrays.sort(heights);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/1*yD0Wb2nUhJ7W5-a3nKiZiA.png" /><figcaption>Sorting the height array</figcaption></figure><p><strong>3. Get the value from the Hashmap and Build the Result Array</strong>: Iterate over the sorted heights array in reverse order to build the result array of names in descending order of heights.</p><pre>//Using HashMap method-get<br>for(int i =0; i&lt;n; i++){<br>            res[i] = map.get(heights[n-i-1]);<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*vWxhKb8O26kcXI7-Je2sww.png" /><figcaption>geting the values</figcaption></figure><h3>Code:</h3><pre>class Solution {<br>    public String[] sortPeople(String[] names, int[] heights) {<br>        int n = names.length;<br>        HashMap&lt;Integer, String&gt; map = new HashMap&lt;&gt;();<br>        for(int i =0; i&lt;n; i++){<br>            map.put(heights[i], names[i]);<br>        }<br><br>        Arrays.sort(heights);<br><br>        String []res= new String[n];<br>        for(int i =0; i&lt;n; i++){<br>            res[i] = map.get(heights[n-i-1]);<br>        }<br>        return res;<br>        <br>    }<br>}</pre><p>This approach efficiently sorts the people by their heights in descending order by leveraging the fast retrieval capabilities of a HashMap and the sorting utility of Arrays.sort(). This method ensures that the solution is both simple and performant.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b5a5db063c7e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[948. Bag of Tokens]]></title>
            <link>https://medium.com/@anousha.21bce7250/948-bag-of-tokens-cf91c9601193?source=rss-d03ebccce2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf91c9601193</guid>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[greedy-algorithms]]></category>
            <category><![CDATA[leetcode-medium]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[CHAKRIKA ANOUSHA]]></dc:creator>
            <pubDate>Fri, 12 Jul 2024 19:08:39 GMT</pubDate>
            <atom:updated>2024-07-12T19:10:11.866Z</atom:updated>
            <content:encoded><![CDATA[<h4>Solving Using Greedy Approach:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/849/1*bQnfJ5CSpmf26DoBcbmRTQ.png" /><figcaption><a href="https://leetcode.com/problems/bag-of-tokens/description/">https://leetcode.com/problems/bag-of-tokens/description/</a></figcaption></figure><h3><strong>The problem statement from LeetCode reads as follows:</strong></h3><blockquote><em>You have an initial </em><strong><em>power</em></strong><em> and an array of </em><strong><em>tokens</em></strong><em> where </em><em>tokens[i] represents the value of the </em><em>i-th token (0-indexed). Your goal is to maximize your total score by potentially playing tokens in a particular order.</em></blockquote><blockquote><em>Each token can be played in one of two ways:<br>- </em>If your current power is at least tokens[i], you may play the i-th token face up, losing tokens[i] power and gaining 1 score.<br>- If your current score is at least 1, you may play the i-th token face down, gaining tokens[i] power and losing 1 score.</blockquote><blockquote><em>Return the largest possible score you can achieve after playing any number of tokens.</em></blockquote><h3>Let’s First Decode The Problem:</h3><p>Imagine you’re given a bag of tokens, each with a positive integer value. Your goal is to <strong>maximize your score</strong> through a series of token plays. Here’s the catch: <strong><em>you start with an initial power</em></strong>, and you can either spend power to play tokens face up (earning you points) or <strong><em>spend points to play tokens face down (regaining power). </em></strong>The challenge lies in deciding the optimal sequence of plays to maximize your score.</p><h3>The Greedy Approach: Key Insights</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gbX_HSVy9Y_7gNGKyWYnTQ.png" /></figure><h3>Step-by-Step Solution</h3><ol><li><strong>Sort the tokens</strong>.</li><li><strong>Two-Pointer Technique</strong>: <br>use the smallest tokens to gain points — <em>low</em> (start) — <em>i pointer</em><br>the largest tokens to regain power when necessary — <em>high</em> (end) — <em>j pointer</em></li></ol><blockquote><strong>Score’s priority — First check with power</strong>:</blockquote><blockquote>If power &gt;= token[i]: play the token face up, <br>score = score+1;gain 1 point,<br>power = power — token[i]reduce power.</blockquote><blockquote><strong>Check with score</strong>:</blockquote><blockquote>If score &gt;= 1: play the token face down, <br>power = power + token[j];gain power,<br>score = score -1; lose 1 point.</blockquote><p><strong>4. Return the maximum score</strong>.</p><h3>EXAMPLE:</h3><p>Let’s walk through the algorithm with an example. Suppose we have tokens [400, 200, 100, 300] and an initial power of 200.</p><h4><strong>1. Sort the tokens</strong>.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/649/1*-00fO0VJTOZxCVdXe8uxRA.png" /></figure><h4><strong>2. Two-Pointer Technique</strong>:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kpO-ifOpJb7KS7kJT7svNA.png" /></figure><h4>3. Return MaxScore:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*sXXMq1O6fv3ENtLfu8Us1A.png" /></figure><h3>CODE IMPLEMENTATION:</h3><pre>class Solution {<br>    public int bagOfTokensScore(int[] token, int power) {<br>        int n = token.length;<br>        int maxScore =0;<br><br>        //sort the array -&gt; to apply greedy smaller -&gt; bigger:<br>        Arrays.sort(token);<br><br>       //2 pointer: - greedy<br>       //power: checkup from smaller sider =&gt; score inc<br>       //and score gawakar=&gt; power adding up from bigger side <br><br>       int i =0; int j = n-1;<br>       int score = 0;<br><br>       while(i&lt;=j){<br>        //score inc first<br>        if(power&gt;=token[i]){<br>            power= power-token[i];<br>            score++;<br>            i++;<br>            maxScore = Math.max(score, maxScore);<br>        }//power incremwnt from back<br>        else if(score&gt;=1){<br>            power = power + token[j];<br>            score--;<br>            j--;<br>        }//no case possible<br>        else{<br>            return maxScore;<br>        }<br><br>       }<br>       return maxScore;<br>        <br>    }<br>}</pre><p>The “<strong>Bag of Tokens</strong>” problem showcases the<strong> power of greedy algorithms</strong> in simplifying complex challenges.</p><p>By making the right local decisions at each step, you can achieve an optimal solution efficiently. Give this approach a try and see how it enhances your problem-solving skills. Happy coding!</p><p>Happy coding!😊</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf91c9601193" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SEPTEMBER EDITION:]]></title>
            <link>https://medium.com/@anousha.21bce7250/september-edition-cee6b3f321b0?source=rss-d03ebccce2df------2</link>
            <guid isPermaLink="false">https://medium.com/p/cee6b3f321b0</guid>
            <category><![CDATA[counting-bits]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[leetcode-solution]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[leetcode-daily-challenge]]></category>
            <dc:creator><![CDATA[CHAKRIKA ANOUSHA]]></dc:creator>
            <pubDate>Fri, 01 Sep 2023 11:05:37 GMT</pubDate>
            <atom:updated>2023-09-01T11:06:41.141Z</atom:updated>
            <content:encoded><![CDATA[<h4>LeetCode Questions — Day 1. <a href="https://leetcode.com/problems/counting-bits/">338. Counting Bits</a></h4><blockquote>“Today is your opportunity to build the tomorrow you want.”</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WWnWXnqsAV7v5WSUCCh5fA.png" /></figure><p><a href="https://leetcode.com/problems/counting-bits/?envType=daily-question&amp;envId=2023-09-01">Counting Bits - LeetCode</a></p><h4>Day 1</h4><p><strong>About the Question: </strong>The question asks for</p><ol><li>an array of length n+1</li><li>where each element represents the count of 1’s in the binary representation of its index, ranging from 0 to n.</li></ol><p><strong>Example :</strong></p><pre>Input: n = 2<br>Output: [0,1,1]<br>Explanation:<br>0 --&gt; 0<br>1 --&gt; 1<br>2 --&gt; 10<br><br>Input: n = 5<br>Output: [0,1,1,2,1,2]<br>Explanation:<br>0 --&gt; 0<br>1 --&gt; 1<br>2 --&gt; 10<br>3 --&gt; 11<br>4 --&gt; 100<br>5 --&gt; 101</pre><p><strong>Approach:</strong></p><ol><li>Create an array ‘result’ of length n+1 — to store the count of 1’s in binary representations</li><li>Iterate through numbers from 0 to n.</li><li>Use the <strong>Integer.bitCount() </strong>method to count the number of 1’s in the binary representation of ‘i’.</li><li>Return the ‘result’ array with counts for each number.</li></ol><p><strong>Code:</strong></p><p>Java:</p><pre>class Solution {<br>    public int[] countBits(int n) {<br>        //create an array result - len = n+1<br>        int[] result = new int[n+1];<br>        //number to bits? =&gt; Integer.bitCount <br>        /* Function: Integer.bitCount(i) -  conts numbr of 1&#39;s in bianry rep&#39;*/<br>        for(int i =0; i&lt;=n; i++){<br>            result[i] = Integer.bitCount(i);<br>        }<br>        return result;<br>    }<br>}</pre><p>Python:</p><pre>lass Solution(object):<br>    def countBits(self, n):<br>        &quot;&quot;&quot;<br>        :type n: int<br>        :rtype: List[int]<br>        &quot;&quot;&quot;<br>        #create a list<br>        result = []<br>        for i in range(0,n+1):<br>            cnt = bin(i).count(&quot;1&quot;)<br>            result.append(cnt)<br>        return result</pre><p><strong>Time And Space Complexity:</strong></p><h3><strong>Important Note:</strong></h3><h4>Java:</h4><ol><li><strong>Integer.bitCount()</strong> : counts number of 1’s in the binary representation</li></ol><h4>Python:</h4><ol><li><strong>bin() : </strong>converts a number to binary</li><li><strong>count(i): </strong>counts number of “i” is encountered</li><li><strong>bin(i).count(1) :</strong> converts i to binary digits and counts number of times 1 is encountered</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cee6b3f321b0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>