<?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 Samar Anand on Medium]]></title>
        <description><![CDATA[Stories by Samar Anand on Medium]]></description>
        <link>https://medium.com/@samaranand?source=rss-4c0d8bc626e7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DPLrTF_phiarmjZkrXUJlw.png</url>
            <title>Stories by Samar Anand on Medium</title>
            <link>https://medium.com/@samaranand?source=rss-4c0d8bc626e7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 04:13:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@samaranand/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[The three range-query structures I re-learn every job hunt]]></title>
            <link>https://samaranand.medium.com/the-three-range-query-structures-i-re-learn-every-job-hunt-d865303eaa9d?source=rss-4c0d8bc626e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/d865303eaa9d</guid>
            <dc:creator><![CDATA[Samar Anand]]></dc:creator>
            <pubDate>Thu, 27 Aug 2026 20:15:38 GMT</pubDate>
            <atom:updated>2026-08-27T20:15:38.172Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Personal revision notes on sparse tables, Fenwick trees, and segment trees.</em></p><p>Every time I start interviewing again, I open five different blog posts, get confused about 0-indexing vs 1-indexing in someone else’s code, and re-derive the lowbit trick from scratch. This is the version I wish I’d bookmarked the first time — one place, one notation, all three structures, so future-me can skim it in fifteen minutes the night before a phone screen.</p><p>All three of these solve some flavor of “give me an answer over a range of an array, fast.” The thing that actually separates them isn’t how clever they are — it’s <em>what changes</em>. If the array never changes after you build your structure, you want a sparse table, because nothing beats O(1) per query. If single elements change one at a time, a Fenwick tree gets you there with less code than anything else. If you need range updates too — add 5 to everything between index 3 and 9 — you want a segment tree with lazy propagation, full stop.</p><p>Keep that one sentence in your head and the rest of this is just filling in the mechanics.</p><h3>Sparse Table</h3><p>Build once, query in O(1) forever. The catch: the array can never change again.</p><p>The whole idea rests on one observation — for an <strong>idempotent</strong> operation (min, max, gcd, AND, OR — anything where applying it to overlapping data twice doesn’t break the answer), you don’t need to cover a range with non-overlapping pieces. You can cover it with two overlapping pieces and it still works, because doing min twice on the same element changes nothing.</p><p>So instead of precomputing every possible range (that’s O(n²) ranges), you only precompute ranges whose length is a power of two. For array a of size n, you build a table sp[j][i] = the answer over a[i .. i + 2^j - 1]. There are only O(log n) values of j, so the table has O(n log n) entries.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0BSwA0eFIYOLUpl9eYskDg.png" /></figure><h4>Build — O(n log n)</h4><pre>int n;<br>vector&lt;int&gt; a(n);<br>vector&lt;vector&lt;int&gt;&gt; sp;   // sp[j][i] = min of a[i .. i + 2^j - 1]<br>int LOG;</pre><pre>void build() {<br>    LOG = 1;<br>    while ((1 &lt;&lt; LOG) &lt;= n) LOG++;<br>    sp.assign(LOG, vector&lt;int&gt;(n));<br>    sp[0] = a;<br>    for (int j = 1; j &lt; LOG; j++)<br>        for (int i = 0; i + (1 &lt;&lt; j) &lt;= n; i++)<br>            sp[j][i] = min(sp[j - 1][i], sp[j - 1][i + (1 &lt;&lt; (j - 1))]);<br>}</pre><h4>Query — O(1)</h4><pre>int query(int l, int r) {          // inclusive, 0-indexed<br>    int j = 31 - __builtin_clz(r - l + 1);   // largest j with 2^j &lt;= length<br>    return min(sp[j][l], sp[j][r - (1 &lt;&lt; j) + 1]);<br>}</pre><p><strong>Why this only works for min/max/gcd, not sum:</strong> Sum isn’t idempotent — if the two windows overlap, you’d double-count the overlapping elements. That’s exactly why sparse tables are the RMQ (range minimum query) structure and not the “range sum” structure. If someone asks for range sum with no updates, just use a prefix sum array — it’s simpler and does the same O(1) query.</p><p><strong>Complexity:</strong> Build O(n log n) · Query O(1) · Update — not supported · Space O(n log n)</p><p>Where you’ll actually see it on LeetCode: it’s rarer than the other two because most LC problems sneak in an update somewhere. The classic case is a static array with repeated min/max/gcd/OR queries.</p><p><em>Problems: LC 1521 — Find a Value of the Mysterious Function Closest to Target (sparse table on OR)</em></p><h3>Fenwick Tree (Binary Indexed Tree)</h3><p>The one to reach for when updates are single points and you need running totals. Fifteen lines, no recursion required.</p><p>This is the one that feels like a magic trick the first time and then just clicks forever after. The trick is: instead of storing a separate value per array index, each index i in the BIT array stores the sum of a chunk of the original array, and the chunk size is determined by the <strong>lowest set bit</strong> of i — what everyone calls lowbit(i) = i &amp; (-i).</p><p>Index 6 in binary is 110, lowbit is 010 = 2, so bit[6] covers 2 elements: indices 5 and 6. Index 8 is 1000, lowbit is 8, so bit[8] covers all 8 elements from 1 to 8. That&#39;s the entire structure. Updating an index means walking <em>up</em> to bigger chunks (add lowbit); querying a prefix means walking <em>down</em> through chunks (subtract lowbit).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WkF4WiP6h9Vk1ys3k0oIWw.png" /></figure><h4>Point update, prefix query — O(log n)</h4><pre>struct Fenwick {<br>    int n;<br>    vector&lt;long long&gt; bit;<br>    Fenwick(int n) : n(n), bit(n + 1, 0) {}</pre><pre>    void update(int i, long long delta) {      // i is 1-indexed<br>        for (; i &lt;= n; i += i &amp; (-i))<br>            bit[i] += delta;<br>    }</pre><pre>    long long query(int i) {                   // prefix sum a[1..i]<br>        long long s = 0;<br>        for (; i &gt; 0; i -= i &amp; (-i))<br>            s += bit[i];<br>        return s;<br>    }</pre><pre>    long long rangeQuery(int l, int r) {        // sum a[l..r], 1-indexed<br>        return query(r) - query(l - 1);<br>    }<br>};</pre><p><strong>Two variants that come up constantly on LC:</strong> Range update + point query — keep the BIT over a <em>difference array</em>. To add val to [l, r], do update(l, val) and update(r+1, -val); a point query is just query(i) — the prefix sum of the diff array. Range update + range query — needs two Fenwick trees and a bit more algebra — at that point it&#39;s honestly easier to just reach for a segment tree with lazy propagation.</p><p><strong>Complexity:</strong> Build O(n) or O(n log n) · Point update O(log n) · Prefix query O(log n) · Space O(n)</p><p><em>Problems: LC 307 (Range Sum Query, Mutable) · LC 315 (Count of Smaller Numbers After Self) · LC 493 (Reverse Pairs) · LC 1409 (Queries on a Permutation With Key)</em></p><h3>Segment Tree</h3><p>The generalist. More code than a Fenwick tree, but it’ll do sum, min, max, gcd, or anything associative — with range updates if you add lazy propagation.</p><p>Picture a binary tree sitting on top of the array. The root owns the whole range [0, n-1]. It splits into two children owning the left half and right half. Keep splitting until each leaf owns exactly one array element. Every internal node&#39;s value is just its operation applied to its two children — for sum, that&#39;s left + right.</p><p>A query for range [l, r] walks down from the root: if a node&#39;s range sits entirely inside [l, r], use it and stop descending; if it&#39;s entirely outside, ignore it; if it partially overlaps, recurse into both children. Because the tree has O(log n) depth, and at each level you touch at most a constant number of &quot;partially overlapping&quot; nodes, a query only ever touches O(log n) nodes total.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*13qobKJsQ6igAEl9eBWKyw.png" /></figure><h3>Array-based tree — build, point update, range query</h3><p>1-indexed tree array of size 4n is the easiest to get right under interview pressure — no need to think about tree-array sizing formulas.</p><pre>struct SegTree {<br>    int n;<br>    vector&lt;long long&gt; tree;<br>    SegTree(int n) : n(n), tree(4 * n, 0) {}</pre><pre>    void build(vector&lt;int&gt;&amp; a, int node, int l, int r) {<br>        if (l == r) { tree[node] = a[l]; return; }<br>        int mid = (l + r) / 2;<br>        build(a, 2 * node,     l,     mid);<br>        build(a, 2 * node + 1, mid+1, r);<br>        tree[node] = tree[2*node] + tree[2*node+1];<br>    }</pre><pre>    void update(int node, int l, int r, int idx, int val) {<br>        if (l == r) { tree[node] = val; return; }<br>        int mid = (l + r) / 2;<br>        if (idx &lt;= mid) update(2*node,     l,     mid, idx, val);<br>        else             update(2*node + 1, mid+1, r,   idx, val);<br>        tree[node] = tree[2*node] + tree[2*node+1];<br>    }</pre><pre>    long long query(int node, int l, int r, int ql, int qr) {<br>        if (qr &lt; l || r &lt; ql) return 0;              // no overlap<br>        if (ql &lt;= l &amp;&amp; r &lt;= qr) return tree[node];    // total overlap<br>        int mid = (l + r) / 2;                          // partial overlap<br>        return query(2*node, l, mid, ql, qr)<br>             + query(2*node + 1, mid+1, r, ql, qr);<br>    }<br>};</pre><pre>// usage: SegTree st(n); st.build(a, 1, 0, n-1);<br>// st.query(1, 0, n-1, l, r);  st.update(1, 0, n-1, idx, val);</pre><h3>Range update, range query — lazy propagation</h3><p>Add val to every element in [ql, qr] without touching every leaf. Stash the pending update in a lazy[] array and only push it down when a child actually needs to be visited.</p><pre>vector&lt;long long&gt; tree, lazy;</pre><pre>void push(int node, int l, int r) {<br>    if (lazy[node] == 0) return;<br>    tree[node] += (long long)(r - l + 1) * lazy[node];<br>    if (l != r) {                       // not a leaf — pass debt to children<br>        lazy[2*node]     += lazy[node];<br>        lazy[2*node + 1] += lazy[node];<br>    }<br>    lazy[node] = 0;<br>}</pre><pre>void updateRange(int node, int l, int r, int ql, int qr, int val) {<br>    push(node, l, r);<br>    if (qr &lt; l || r &lt; ql) return;<br>    if (ql &lt;= l &amp;&amp; r &lt;= qr) {<br>        lazy[node] += val;<br>        push(node, l, r);<br>        return;<br>    }<br>    int mid = (l + r) / 2;<br>    updateRange(2*node,     l,     mid, ql, qr, val);<br>    updateRange(2*node + 1, mid+1, r,   ql, qr, val);<br>    tree[node] = tree[2*node] + tree[2*node+1];<br>}</pre><pre>long long queryRange(int node, int l, int r, int ql, int qr) {<br>    push(node, l, r);<br>    if (qr &lt; l || r &lt; ql) return 0;<br>    if (ql &lt;= l &amp;&amp; r &lt;= qr) return tree[node];<br>    int mid = (l + r) / 2;<br>    return queryRange(2*node, l, mid, ql, qr)<br>         + queryRange(2*node + 1, mid+1, r, ql, qr);<br>}</pre><p><strong>The part I always forget:</strong> Call push() at the <em>top</em> of every function that touches a node — before you check whether the range matches, before you recurse. It&#39;s the one line that&#39;s easy to skip because the tree &quot;looks&quot; like it&#39;s working on small test cases and then quietly gives wrong answers once updates start overlapping.</p><p><strong>Complexity:</strong> Build O(n) · Point update O(log n) · Range update O(log n) with lazy · Range query O(log n) · Space O(n)</p><p><em>Problems: LC 307 (Range Sum Query, Mutable) · LC 327 (Count of Range Sum) · LC 715 (Range Module) · LC 732 (My Calendar III) · LC 218 (The Skyline Problem) · LC 2407 (Longest Increasing Subsequence II)</em></p><h3>Which one do I actually reach for?</h3><p>The question that matters is always about updates, not about the query itself.</p><ul><li><strong>Array never changes:</strong> Sparse table is the best choice — O(1) query. A Fenwick tree or segment tree would be overkill.</li><li><strong>Single-point updates, prefix/range sum:</strong> Fenwick tree is the best choice — least code. A segment tree works too but is more code than you need. A sparse table can’t handle updates at all.</li><li><strong>Range updates + range queries:</strong> Segment tree with lazy propagation is the best choice. A Fenwick tree gets awkward here (needs two BITs), and a sparse table still can’t update.</li><li><strong>Non-invertible op (min/max/gcd) with updates:</strong> Segment tree is the best choice. A Fenwick tree needs an invertible operation, so min/max/gcd don’t fit cleanly. A sparse table can’t update.</li><li><strong>Code you need to type from memory in an interview:</strong> Fenwick tree is shortest and most memorizable. Sparse table is short if you remember the log trick. Segment tree is the longest and easiest to fumble under pressure.</li></ul><h3>The condensed version (five minutes before the interview)</h3><ul><li><strong>Sparse table:</strong> sp[j][i] = op over a[i..i+2^j-1]. Build by doubling j. Query = op of two overlapping windows at j = log2(len). Only for idempotent ops, no updates.</li><li><strong>Fenwick tree:</strong> lowbit(i) = i &amp; (-i). Update walks up (i += lowbit(i)), query walks down (i -= lowbit(i)). Both O(log n). Needs an invertible op (sum, xor) since range = query(r) - query(l-1).</li><li><strong>Segment tree:</strong> tree array size 4n, 1-indexed nodes, recurse with (node, l, r). Three cases in every function: no overlap, full overlap, partial overlap (recurse both children). Add lazy[] and a push() call at the top of every function for range updates.</li><li>When genuinely stuck on which to build first: default to Fenwick tree if the op is a sum and updates are single points. It’s the least code and the least that can go wrong.</li></ul><p><em>Written for my own future reference after the fourth time re-deriving the lowbit trick mid-interview-prep. If it’s useful to you too, that’s a nice bonus.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d865303eaa9d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Git and Linux Commands: Essential Tools for Developers]]></title>
            <link>https://samaranand.medium.com/mastering-git-and-linux-commands-essential-tools-for-developers-b66c83a5afa1?source=rss-4c0d8bc626e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/b66c83a5afa1</guid>
            <category><![CDATA[github-tutorial]]></category>
            <category><![CDATA[linux-tutorial]]></category>
            <category><![CDATA[newbie]]></category>
            <category><![CDATA[github]]></category>
            <dc:creator><![CDATA[Samar Anand]]></dc:creator>
            <pubDate>Thu, 16 Mar 2023 03:20:57 GMT</pubDate>
            <atom:updated>2023-03-16T03:20:57.038Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*vdvdDNZ4SZfQoS3nF1gIDA.jpeg" /></figure><p>As a developer, using command-line interfaces (CLI) can significantly enhance your productivity. In this blog post, we will cover some of the most useful commands for both Github and Linux.</p><p><strong>Github Commands:</strong></p><ol><li>git clone &lt;repository-url&gt; : This command will create a local copy of the repository on your machine.</li><li>git add &lt;file&gt; : This command adds changes to the staging area.</li><li>git commit -m “&lt;commit-message&gt;” : This command commits the changes to the repository.</li><li>git push : This command will push the committed changes to the remote repository.</li><li>git pull : This command will pull the latest changes from the remote repository to your local copy.</li><li>git status : This command will show the current status of the repository.</li><li>git log : This command will display the commit history of the repository.</li><li>git branch &lt;branch-name&gt; : This command will create a new branch.</li><li>git checkout &lt;branch-name&gt; : This command will switch to a different branch.</li><li>git merge &lt;branch-name&gt; : This command will merge the specified branch into the current branch.</li></ol><p><strong>Linux Commands:</strong></p><ol><li>ls : This command lists the files and directories in the current directory.</li><li>cd &lt;directory-name&gt; : This command changes the current directory to the specified directory.</li><li>mkdir &lt;directory-name&gt; : This command creates a new directory with the specified name.</li><li>touch &lt;file-name&gt; : This command creates a new file with the specified name.</li><li>rm &lt;file-name&gt; : This command deletes the specified file.</li><li>rm -r &lt;directory-name&gt; : This command deletes the specified directory and all of its contents.</li><li>cp &lt;source&gt; &lt;destination&gt; : This command copies the specified file or directory to the destination.</li><li>mv &lt;source&gt; &lt;destination&gt; : This command moves the specified file or directory to the destination.</li><li>chmod &lt;permissions&gt; &lt;file-name&gt; : This command changes the permissions of the specified file.</li><li>ps -ef | grep &lt;process-name&gt; : This command shows the details of the specified process.</li></ol><p>These are some of the most valuable commands developers should be familiar with when working with GitHub and Linux. However, these lists are not exhaustive and there are many more commands that can be useful depending on the situation. It’s always a good idea to keep learning and exploring new commands to enhance your productivity.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b66c83a5afa1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“Why and How Developers Should Utilize GitHub for Software Development”]]></title>
            <link>https://samaranand.medium.com/why-and-how-developers-should-utilize-github-for-software-development-7de184bcd789?source=rss-4c0d8bc626e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/7de184bcd789</guid>
            <dc:creator><![CDATA[Samar Anand]]></dc:creator>
            <pubDate>Wed, 08 Feb 2023 18:32:33 GMT</pubDate>
            <atom:updated>2023-02-08T18:32:33.020Z</atom:updated>
            <content:encoded><![CDATA[<p>GitHub is a web-based platform that provides a repository for software projects and is widely used by developers to manage and store their projects. In today’s digital era, GitHub has become an essential tool for software development as it allows developers to collaborate and share their work easily. This blog will discuss why and how developers should utilize GitHub in their software development process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_Met6rq9jTwMl7r29mX-Yg.png" /></figure><p><strong><em>Why Use GitHub?</em></strong></p><ol><li><strong>Collaboration and Sharing:</strong> GitHub provides a platform for developers to collaborate and share their work with each other. They can share their code, provide feedback, and contribute to each other’s projects. This makes it easier for developers to work together, share their work, and learn from each other.</li><li><strong>Version Control:</strong> GitHub uses Git, a distributed version control system, to keep track of changes to the codebase. This allows developers to revert to previous versions of the code if needed and makes it easier to keep track of changes.</li><li><strong>Bug Tracking and Issue Management:</strong> GitHub provides a bug tracking and issue management system, which makes it easier for developers to report and track bugs in their code. The system also allows other developers to contribute to fixing the bugs and to keep track of their progress.</li><li><strong>Code Review:</strong> GitHub provides a code review system that allows developers to review each other’s code and provide feedback. This makes it easier for developers to maintain high-quality code and improve their coding skills.</li></ol><p><strong><em>How to Use GitHub?</em></strong></p><ol><li><strong>Create a GitHub Account:</strong> The first step to using GitHub is to create an account. This can be done easily by visiting the GitHub website and following the instructions to create an account.</li><li><strong>Create a Repository:</strong> Once you have an account, you can create a repository for your project. A repository is a place to store your code and collaborate with other developers.</li><li><strong>Push Your Code:</strong> After you have created a repository, you can push your code to the repository. This allows you to store your code in a central location and makes it easier to collaborate with other developers.</li><li><strong>Use the GitHub Workflow:</strong> GitHub provides a workflow that makes it easier to collaborate with other developers. The workflow involves creating branches for each new feature, committing changes to the code, and merging the branches into the main codebase.</li></ol><p>In conclusion, GitHub is a powerful tool for software development that provides a platform for collaboration, version control, bug tracking, and code review. Developers should utilize GitHub to take their software development to the next level and improve their coding skills.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7de184bcd789" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Authentication in Spring Boot: A comprehensive guide]]></title>
            <link>https://samaranand.medium.com/authentication-in-spring-boot-a-comprehensive-guide-af9ffd2335d9?source=rss-4c0d8bc626e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/af9ffd2335d9</guid>
            <dc:creator><![CDATA[Samar Anand]]></dc:creator>
            <pubDate>Sat, 21 Jan 2023 06:49:56 GMT</pubDate>
            <atom:updated>2023-01-21T07:10:41.029Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Authentication</em> is a critical aspect of any web application, and Spring Boot provides several ways to implement it. In this blog, we will explore some of the most popular authentication methods that can be used in a Spring Boot application.</p><ol><li><strong>Basic Authentication:</strong> This is the simplest form of authentication, where the user’s credentials are sent in the request header in the format “username:password”. However, this method is not recommended as it sends the credentials in plaintext and is not secure. It is also not recommended to use this method as it lacks flexibility and scalability.</li><li><strong>Form-based Authentication:</strong> In this method, the user’s credentials are sent in the request body, typically in a login form. Spring Security provides built-in support for this type of authentication, which includes handling login and logout requests, redirecting unauthenticated users to the login page, and managing user sessions. This method is suitable for traditional web applications and is easy to implement and maintain.</li><li><strong>OAuth2 Authentication:</strong> This method uses the OAuth2 protocol to authenticate users. OAuth2 is an open standard for authorization that allows users to share their resources without sharing their credentials. Spring Security provides built-in support for OAuth2, making it easy to integrate with popular OAuth2 providers such as Google, Facebook, and GitHub. This method is suitable for API-based applications and provides a secure way to access user’s data from different sources.</li><li><strong>JSON Web Token (JWT) Authentication:</strong> JWT is a compact and self-contained way to securely transmit information between parties. In this method, the server generates a JWT token after the user logs in, and the token is sent to the client in response. The client then sends the token in the request header for every subsequent request, and the server validates the token before allowing access to the protected resources. JWT is stateless, meaning that the server does not maintain any session state. This makes it less resource-intensive and more scalable than traditional session-based authentication. This method is suitable for stateless, RESTful applications and enables the client to access multiple servers with one token.</li><li><strong>Single Sign-On (SSO) Authentication:</strong> SSO is a method of authentication where a user logs in once and then can access multiple applications without having to log in again. This method can be implemented using OpenID Connect or Security Assertion Markup Language (SAML). Spring Security provides built-in support for these protocols, making it easy to integrate with popular SSO providers such as Okta and Auth0. SSO is suitable for applications that need to integrate with other systems and provides a seamless user experience.</li></ol><p>In addition to these popular authentication methods, there are other methods that can also be used in a Spring Boot application, such as:</p><ol><li><strong>LDAP Authentication:</strong> This method uses the Lightweight Directory Access Protocol (LDAP) to authenticate users. LDAP is a standard protocol for storing and retrieving user information, and it is commonly used in enterprise environments. Spring Security provides built-in support for LDAP, making it easy to integrate with LDAP servers. This method is suitable for applications that need to authenticate users against an existing LDAP directory.</li><li><strong>Two-Factor Authentication (2FA): </strong>This method adds an additional layer of security by requiring the user to provide two forms of identification before logging in. This can include something the user knows (e.g. a password) and something the user has (e.g. a token or mobile phone). Spring Security provides built-in support for 2FA, making it easy to implement this method. This method is suitable for applications that require a high level of security.</li><li><strong>Certificate-based Authentication:</strong> This method uses digital certificates to authenticate users. The user’s certificate is verified by the server before allowing access to the protected resources. Spring Security provides built-in support for certificate-based authentication, making it easy to implement this method. This method is suitable for applications that need to authenticate users in a secure and trusted environment.</li></ol><p>In conclusion, Spring Boot provides several ways to implement authentication in a web application, each with its own advantages and disadvantages. Basic authentication is the simplest method, but it is not secure. Form-based authentication is suitable for traditional web applications, while OAuth2 and JWT are suitable for API-based applications. SSO, LDAP, 2FA and Certificate-based authentication are suitable for specific use cases. It is important to evaluate your application requirements and choose the appropriate authentication method for your use case.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af9ffd2335d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sparse Table]]></title>
            <link>https://samaranand.medium.com/sparse-table-e18b26f65361?source=rss-4c0d8bc626e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/e18b26f65361</guid>
            <category><![CDATA[competitive-programming]]></category>
            <category><![CDATA[sparse-table]]></category>
            <category><![CDATA[range-query]]></category>
            <dc:creator><![CDATA[Samar Anand]]></dc:creator>
            <pubDate>Tue, 22 Jun 2021 06:08:19 GMT</pubDate>
            <atom:updated>2021-06-22T06:08:19.986Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Hey lads, today I will talk about Sparse Table (a Data Structure) which helps in solving range queries from static array (immutable arrays).</blockquote><p>First, let&#39;s see a <a href="https://cses.fi/problemset/task/1647">problem</a> from cses.fi which can be solved using this DS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y9JRe1gEFpyIgVTbyxeTrA.png" /></figure><p>Here we can see that there are n values and q query in which minimum value between range [a,b] is asked. here constraints are up to 2⋅10⁵. so, if we think about the brute force approach which will surely gonna give us a TLE verdict.</p><p>There are many data structure which we can be applied here but a sparse table is best because we can solve overlapping friendly function in O(1), and if it is not overlapping friendly then we can solve in O(logn). And, of course, the minimum function is an overlapping friendly function. So, we will be able to solve the query in O(1).</p><p>Sparse Table is easy to code and also, easy to remember.</p><p>For more details, I will refer to the <a href="https://cp-algorithms.com/">cp-algorithms.</a>com</p><p>The main idea behind the sparse table is to precompute the values for range queries with a power of two lengths. Since any number can be made from a power of 2, so any range can be answered from that table.</p><h4><strong>Pros:</strong></h4><ul><li>Easy to write and remember</li><li>Can answer in O(1)</li></ul><h4>Cons:</h4><ul><li>Not work in update query</li></ul><p><strong>Code </strong>👇️</p><pre>#include &quot;bits/stdc++.h&quot;<br>using namespace std;</pre><pre>using int64 = long long;</pre><pre>const char nwl = &#39;\n&#39;;<br>const int mxx = 2e5 + 123;</pre><pre>int n, q, arr[mxx], dp[30][mxx];</pre><pre>void buildTable() {<br> for (int i = 1; i &lt;= n; i++) {<br>  dp[0][i] = arr[i];<br> }<br> int k = log2(n);<br> for (int j = 1; j &lt;= k; j++) {<br>  int p = 1 &lt;&lt; (j - 1);<br>  for (int i = 1; i &lt;= n; i++) {<br>   if (i + 2 * p - 1 &gt; n) break;<br>   dp[j][i] = min(dp[j - 1][i], dp[j - 1][i + p]);<br>  }<br> }<br>}</pre><pre>int ansQuery(int l, int r) {<br> int c = log2(r - l + 1);<br> return min(dp[c][l], dp[c][r + 1 - (1 &lt;&lt; c)]);<br>}</pre><pre>int main() {<br> cin &gt;&gt; n &gt;&gt; q;<br> for (int i = 1; i &lt;= n; i++) {<br>  cin &gt;&gt; arr[i];<br> }</pre><pre>buildTable();</pre><pre>for (int i = 1; i &lt;= q; i++) {<br>  int l, r;<br>  cin &gt;&gt; l &gt;&gt; r;<br>  cout &lt;&lt; ansQuery(l, r) &lt;&lt; nwl;<br> }</pre><pre>return 0;<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e18b26f65361" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Segment Tree]]></title>
            <link>https://samaranand.medium.com/segment-tree-e2394e9a6578?source=rss-4c0d8bc626e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2394e9a6578</guid>
            <category><![CDATA[range-sum-query]]></category>
            <category><![CDATA[segment-tree]]></category>
            <category><![CDATA[range-query]]></category>
            <dc:creator><![CDATA[Samar Anand]]></dc:creator>
            <pubDate>Tue, 22 Jun 2021 05:53:49 GMT</pubDate>
            <atom:updated>2021-06-22T05:53:49.906Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>A Segment Tree is a data structure used mainly to find in-range queries. Since range queries can be solved is using prefix array but if there is an update, we can get help from the segment tree.</blockquote><blockquote>Like in with the segment tree we can get sum of ar[l…r] easily or also we can update the value of any pos b/w 0…n-1 with the new value.</blockquote><pre>/*<br>** <a href="http://twitter.com/samaranand">@samaranand</a> **<br>*/</pre><pre>#include &lt;bits/stdc++.h&gt;<br>using namespace std;<br>#define pb push_back<br>using vi = vector&lt;int&gt;;<br>const int N = 4 * (1e5 + 2);</pre><pre>// suppose if we have n = 1e5<br>// so we will need 4*n size of array to make a segment tree</pre><pre>int arr[N];</pre><pre>// we can implement segment tree on many type of functions<br>// but here i have assumed for the sum query</pre><pre>void build(vi &amp;v, int u, int tl, int tr) {<br> if (tl == tr) arr[u] = v[tl];<br> else {<br>  int tm = (tl + tr) / 2;<br>  build(v, 2 * u, tl, tm);<br>  build(v, 2 * u + 1, tm + 1, tr);<br>  arr[u] = arr[2 * u] + arr[2 * u + 1];<br> }<br>}</pre><pre>// pos is 0-inedxed position where new value is updated<br>void update(int u, int tl, int tr, int pos, int value) {<br> if (tl == tr) {<br>  arr[u] = value;<br> } else {<br>  int tm = (tl + tr) / 2;<br>  if (pos &lt;= tm) update(2 * u, tl, tm, pos, value);<br>  else update(2 * u + 1, tm + 1, tr, pos, value);<br>  arr[u] = arr[2 * u] + arr[2 * u + 1];<br> }<br>}</pre><pre>// l &amp; r is the 0-indexed range from where we want result sum<br>int resQuery(int u, int tl, int tr, int l, int r) {<br> if (l &gt; r) return 0;<br> if (tl == l and tr == r) return arr[u];<br> int tm = (tl + tr) / 2;<br> int a = resQuery(2 * u, tl, tm, l, min(tm, r));<br> int b = resQuery(2 * u + 1, tm + 1, tr, max(l, tm + 1), r);<br> return a + b;<br>}</pre><pre>int main() {<br> // total size og given array<br> int n; cin &gt;&gt; n;<br> vi v(n);<br> // taking input of array<br> for (int i = 0; i &lt; n; i++) cin &gt;&gt; v[i];</pre><pre>// calling build fun to build the segment tree<br> build(v, 1, 0, n - 1);</pre><pre>// total number of query<br> int q; cin &gt;&gt; q;</pre><pre>for (int i = 0; i &lt; q; i++) {<br>  int t;<br>  cin &gt;&gt; t;<br>  // here i assumed 2 type of query<br>  // 1st is update so t == 1<br>  // 2nd is range sum query so t == 2<br>  // Iassumed 0-indexing for query<br>  if (t == 1) {<br>   // for update query, we need pos &amp; value<br>   int pos, val;<br>   cin &gt;&gt; pos &gt;&gt; val;<br>   update(1, 0, n - 1, pos, val);<br>  } else {<br>   // for range query, we need l &amp; r<br>   int l, r;<br>   cin &gt;&gt; l &gt;&gt; r;<br>   cout &lt;&lt; resQuery(1, 0, n - 1, l, r) &lt;&lt; endl;<br>  }</pre><pre>}</pre><pre>return 0;<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2394e9a6578" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Competitive Programmin]]></title>
            <link>https://samaranand.medium.com/competitive-programmin-206ac43f0637?source=rss-4c0d8bc626e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/206ac43f0637</guid>
            <category><![CDATA[competitive-programming]]></category>
            <dc:creator><![CDATA[Samar Anand]]></dc:creator>
            <pubDate>Mon, 08 Jun 2020 05:03:43 GMT</pubDate>
            <atom:updated>2020-06-08T05:03:43.899Z</atom:updated>
            <content:encoded><![CDATA[<h3>Intro to Competitive Programming</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*OhrAmSvpDWk0gzb_08PkhQ.jpeg" /></figure><p>Every coder wants to be part of competitive programming formally we call it as CP. but what many beginners fear about is staring it as I used to do so. First whats the mean of competitive programming is? basically it is a type of mind sports in which contestants participate in a contest in which few logical and mathematical problems are given and participants need to solve it by writing source codes in the span of time. Nowadays it is very popular among young coders.</p><p>we have many online platforms for competitive programming but Codeforces, Codechef, AtCoder etc. are best. They are best for the collection of their questions. Codeforces is famous for their 2–3 hr contest in every 3–4 days while CodeChef is famous for Long Challenge which is conducted for 10 days. Same as at AtCoders and HackerEarth where the monthly challenge is conducted. and after contest, they used to release editorial that is the solution of all problems from their newbies can learn and grow.</p><p><strong>How to Start?</strong></p><ol><li>Learn a programming language. Best choice for CPs is C++ but you can learn Java too. also, Learn about the STL of C++.</li><li>Learn Data Structure</li><li>Learn Algorithms</li></ol><p>why C++? I say C++ because all the CP platforms accept the language C++ and also releases their editorial in C++. many good books for CPs are written in C++.</p><p><strong>few best books</strong></p><ol><li><a href="https://drive.google.com/file/d/1IEaiRM74ifWA1KjWSf_E1tandSj3VBns/view?usp=sharing">Competitive Programmer’s Handbook</a></li><li><a href="https://drive.google.com/file/d/18zNkKrmtzfkLhBdtw_3VswcOUUgCTNmI/view?usp=sharing">Competitive Programming 3 by Steven Halim &amp; Felix Halim</a></li><li><a href="https://drive.google.com/file/d/1CEtfL9OHiXlLB-HBSd1Krmlx5c6Tb2Ku/view?usp=sharing">Guide-to-Competitive-Programming-Learning-and-improving-Algorithms-through-Contests</a></li></ol><p>Contestants need to be very fast so they use what we call it as templates. Templates are few lines which are used frequently so coders write them and use the micros in the place.</p><blockquote><em>#include “bits/stdc++.h”<br>using namespace std;<br>#define pb push_back<br>#define F first<br>#define S second<br>//#define biGinf 9e+18<br>#define all(v) v.begin(),v.end()<br>#define rall(v) v.rbegin(),v.rend()<br>#define inpv(v) for(auto &amp;x:v) cin&gt;&gt;x<br>#define otpv(v) for(auto &amp;x:v) cout&lt;&lt;x&lt;&lt;nwl<br>#define otpv2(v) for(auto &amp;x:v) cout&lt;&lt;x&lt;&lt;” “<br>#define CASES int tt;cin&gt;&gt;tt; while(tt — )<br>#define for0(i,n) for(int i=0; i&lt;n; i++)<br>#define for1(i,n) for(int i=1; i&lt;n; i++)<br>#define sz(v) (int)(v).size()<br>using vi = vector &lt; int &gt;;<br>using vii = vector &lt; vi &gt;;<br>using pii = pair &lt; int , int &gt;;<br>using vpii = vector &lt; pii &gt;;<br>using int64 = long long;<br>//const int64 biGinf = 9e+18;<br>//const string yo = “YES”, no = “NO”;<br>const int inf = 2e9 + 7;<br>const char nwl = ‘\n’;</em></blockquote><blockquote><em>int main() {<br> ios_base::sync_with_stdio(false);<br> cin.tie(NULL);<br> //ur code</em></blockquote><blockquote><em>return 0;<br>}</em></blockquote><p>So it&#39;s all about the introduction of CP. also a good editor and coding environment helps. Some good coders use OS like Ubuntu and text editor like Sublime Text Editor. In place of the terminal, we can install guake in Ubuntu because it starts with one button F12 and very helpful. we can set up an Ubuntu environment for checking edge cases with random values. we can save snippets in the sublime text editor so don&#39;t need to copy and paste frequently. There are lots of stuff which matters but the main thing is that how much a guy practices. from start to last it&#39;s all about practices with good questions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=206ac43f0637" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>