<?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 Saad Al Nafis Siddiky on Medium]]></title>
        <description><![CDATA[Stories by Saad Al Nafis Siddiky on Medium]]></description>
        <link>https://medium.com/@nafissiddiky2?source=rss-4ab4e65e7f15------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ZOnfI0jfLuKNmecmi9GbxA.jpeg</url>
            <title>Stories by Saad Al Nafis Siddiky on Medium</title>
            <link>https://medium.com/@nafissiddiky2?source=rss-4ab4e65e7f15------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 12:52:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nafissiddiky2/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[Codeforces 1807A — Plus or Minus solution in cpp]]></title>
            <link>https://medium.com/@nafissiddiky2/codeforces-1807a-plus-or-minus-solution-in-cpp-5633326a8824?source=rss-4ab4e65e7f15------2</link>
            <guid isPermaLink="false">https://medium.com/p/5633326a8824</guid>
            <category><![CDATA[cpp-programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[codeforces]]></category>
            <category><![CDATA[cps]]></category>
            <dc:creator><![CDATA[Saad Al Nafis Siddiky]]></dc:creator>
            <pubDate>Sun, 29 Nov 2026 05:02:32 GMT</pubDate>
            <atom:updated>2026-11-29T05:02:32.400Z</atom:updated>
            <content:encoded><![CDATA[<h3>Problem Summary</h3><p>Given three integers a, b, c, determine whether c was obtained by a + b or a - b. Exactly one of the two is guaranteed to be true.</p><p>Input: t test cases, each with a b c<br>Output: + if a + b == c, otherwise -.</p><h3>Key Insight</h3><p>Direct check: if a + b == c → print +; else → print -. No need to test a - b == c explicitly — the problem guarantees exactly one holds.</p><h3>Examples</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*mic_v72u7Eflt_K7Yu1KfQ.png" /></figure><h3>Algorithm</h3><ol><li>Read t.</li><li>For each test case:</li></ol><ul><li>Read a, b, c.</li><li>If a + b == c → print +.</li><li>Else → print -.</li></ul><h3>Code Walkthrough</h3><pre>int a, b, c;<br>cin &gt;&gt; a &gt;&gt; b &gt;&gt; c;<br>if (a + b == c) {<br>    cout &lt;&lt; &quot;+&quot; &lt;&lt; endl;    // sum matches<br>} else {<br>    cout &lt;&lt; &quot;-&quot; &lt;&lt; endl;    // guaranteed to be a - b<br>}</pre><ul><li>The else branch doesn&#39;t need to verify a - b == c — the problem guarantees it.</li></ul><h3>Complexity</h3><ul><li>Time: O(1) per test case</li><li>Space: O(1)</li></ul><h3>Edge Cases</h3><ul><li>Negative results (e.g., a=3, b=5, c=-2) → a+b=8 ≠ -2 → - ✅ (3−5=−2)</li><li>c = 0 → handled naturally ✅</li><li>a = b, c = 2a → + ✅</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5633326a8824" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Codeforces 1692A — Marathon solution in cpp]]></title>
            <link>https://medium.com/@nafissiddiky2/codeforces-1692a-marathon-solution-in-cpp-56bcef94dd1d?source=rss-4ab4e65e7f15------2</link>
            <guid isPermaLink="false">https://medium.com/p/56bcef94dd1d</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[cps]]></category>
            <category><![CDATA[cpp-programming]]></category>
            <category><![CDATA[codeforces]]></category>
            <dc:creator><![CDATA[Saad Al Nafis Siddiky]]></dc:creator>
            <pubDate>Sat, 28 Nov 2026 13:28:55 GMT</pubDate>
            <atom:updated>2026-11-28T13:28:55.675Z</atom:updated>
            <content:encoded><![CDATA[<h3>Problem Summary</h3><p>Four runners participate in a marathon. Given their distances covered (a, b, c, d), where a is the distance of Timur (the first runner), count how many other runners covered strictly more distance than Timur.</p><p>Input: t test cases, each with 4 integers a b c d<br>Output: Count of runners ahead of Timur (distance &gt; a).</p><h3>Key Insight</h3><p>Simply compare a[0] against a[1], a[2], a[3]. Count how many are strictly greater. Equality doesn&#39;t count.</p><h3>Examples</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*Tl5QrQ5SSRiD__3pH_LrZg.png" /></figure><h3>Algorithm</h3><ol><li>Read t.</li><li>For each test case:</li></ol><ul><li>Read a[0..3].</li><li>res = 0.</li><li>For i = 1 to 3: if a[0] &lt; a[i] → res++.</li></ul><p>3.Print res.</p><h3>Code Walkthrough</h3><pre>int a[4];<br>for (int i = 0; i &lt; 4; i++) cin &gt;&gt; a[i];<br><br>int res = 0;<br>for (int i = 1; i &lt; 4; i++) {<br>    if (a[0] &lt; a[i]) {<br>        res++;          // this runner is ahead of Timur<br>    }<br>    else {<br>        continue;       // redundant — loop would continue anyway<br>    }<br>}<br>cout &lt;&lt; res &lt;&lt; endl;</pre><ul><li>a[0] = Timur&#39;s distance (reference).</li><li>Loop starts at index 1, skipping Timur himself.</li><li>The else { continue; } is redundant — the loop continues naturally. Can be removed.</li></ul><h3>Complexity</h3><ul><li>Time: O(1) per test case (fixed 3 comparisons).</li><li>Space: O(1)</li></ul><h3>Edge Cases</h3><ul><li>Ties (someone equals Timur’s distance) → not counted (strict &lt;) ✅</li><li>Timur has the max distance → res = 0 ✅</li><li>All equal → res = 0 ✅</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56bcef94dd1d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Codeforces 2048A? — Grapefruit Joy (Juice vs Fresh) solution in cpp]]></title>
            <link>https://medium.com/@nafissiddiky2/codeforces-2048a-grapefruit-joy-juice-vs-fresh-solution-in-cpp-e4c90c53e183?source=rss-4ab4e65e7f15------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4c90c53e183</guid>
            <category><![CDATA[cpp-programming]]></category>
            <category><![CDATA[codeforces]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[cps]]></category>
            <category><![CDATA[problem-solving]]></category>
            <dc:creator><![CDATA[Saad Al Nafis Siddiky]]></dc:creator>
            <pubDate>Fri, 27 Nov 2026 17:42:26 GMT</pubDate>
            <atom:updated>2026-11-27T17:42:26.110Z</atom:updated>
            <content:encoded><![CDATA[<h3>Problem Summary</h3><p>Given n grapefruits with taste values a_i, you can either:</p><ul><li>Eat a grapefruit fresh → gain a_i joy.</li><li>Squeeze any k grapefruits into a juice glass → gain p joy.</li></ul><p>Each grapefruit is used at most once. You can make any number of glasses. Maximize total joy.</p><p>Input: t test cases; each has n, k, p, then n taste values.<br>Output: Maximum joy per test case.</p><h3>Key Insight</h3><p>Sort the array ascending. If we make exactly j glasses of juice:</p><ul><li>It’s optimal to use the smallest j × k fruits for juice (sacrifice the least fresh-joy).</li><li>The remaining n − j×k largest fruits are eaten fresh.</li><li>Total = j × p + (sum of the largest n − j×k fruits).</li></ul><p>Try all valid j from 0 to ⌊n/k⌋ and take the maximum.</p><blockquote><em>Why smallest for juice? Because juice gives a flat </em><em>p regardless of which fruits go in — so always sacrifice the least valuable ones fresh.</em></blockquote><h3>Examples</h3><p>Input: 3 2 10 → 9 1 2</p><ul><li>Sorted: 1 2 9</li><li>j=0: 12</li><li>j=1: 10 + (sum of top 1) = 10 + 9 = 19 ✅</li></ul><p>Input: 4 2 5 → 1 1 9 9</p><ul><li>Sorted: 1 1 9 9</li><li>j=0: 20</li><li>j=1: 5 + 9 + 9 = 23 ✅</li><li>j=2: 10 + 0 = 10</li></ul><h3>Algorithm</h3><ol><li>Read n, k, p and array a.</li><li>Sort a ascending.</li><li>Build prefix sums pref[i] = sum of first i elements.</li><li>ans = pref[n] (all fresh, 0 glasses).</li><li>For j = 1 to n/k:</li></ol><ul><li>Juice uses smallest j×k → their fresh-value is pref[j×k].</li><li>Fresh sum = pref[n] - pref[j×k].</li><li>Total = j×p + (pref[n] − pref[j×k]).</li><li>Update ans = max(ans, total).</li></ul><p>6. Print ans.</p><h3>Code Walkthrough</h3><pre>sort(a, a + n);<br><br>pref[0] = 0;<br>for (int i = 0; i &lt; n; i++)<br>    pref[i+1] = pref[i] + a[i];         // prefix sums<br><br>ll ans = pref[n];                        // baseline: eat all fresh<br>int maxJ = n / k;                        // max glasses possible<br><br>for (int j = 1; j &lt;= maxJ; j++) {<br>    ll jp = (ll)j * p;                   // joy from j glasses<br>    ll fp = pref[n] - pref[j * k];       // fresh sum of largest (n - j*k)<br>    ll total = jp + fp;<br>    if (total &gt; ans) ans = total;<br>}<br>cout &lt;&lt; ans &lt;&lt; &quot;\n&quot;;</pre><ul><li>pref[n] - pref[j*k] → sum of elements from index j*k to n-1 = the largest n − j*k fruits (eaten fresh).</li><li>(ll)j * p → cast to long long to avoid overflow (p up to 10⁹, j up to 10⁵).</li><li>Answer fits in long long ✅ (problem explicitly warns about 32-bit overflow).</li></ul><h3>Complexity</h3><ul><li>Time: O(n log n) per test case (sorting) + O(n) for prefix + O(n/k) for loop. Total O(n log n); summed over all tests still ≤ O(2×1⁰⁵ log) ✅</li><li>Space: O(n) for a and pref.</li></ul><h3>Edge Cases</h3><ul><li>k &gt; n → maxJ = 0 → answer = all fresh ✅</li><li>p very large → all juice possible, j = n/k wins ✅</li><li>All a_i = 1, p = 1 → fresh and juice tie ✅</li><li>n = k → j = 1 → max(p, sum) ✅</li></ul><h3>💡 Alternative Without Prefix Array</h3><p>Since we only need pref[n] - pref[j*k] (suffix sums), you could compute the total once and subtract progressively, but the prefix array approach is clean and correct.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4c90c53e183" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Codeforces 1955A? — Sunny and Aubrey’s Picnic (Berry Placement) solution in cpp]]></title>
            <link>https://medium.com/@nafissiddiky2/codeforces-1955a-sunny-and-aubreys-picnic-berry-placement-solution-in-cpp-bbb027254e18?source=rss-4ab4e65e7f15------2</link>
            <guid isPermaLink="false">https://medium.com/p/bbb027254e18</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[cps]]></category>
            <category><![CDATA[codeforces]]></category>
            <category><![CDATA[cpp-programming]]></category>
            <dc:creator><![CDATA[Saad Al Nafis Siddiky]]></dc:creator>
            <pubDate>Fri, 27 Nov 2026 17:38:32 GMT</pubDate>
            <atom:updated>2026-11-27T17:38:32.320Z</atom:updated>
            <content:encoded><![CDATA[<h3>Problem Summary</h3><p>A round toast has l slices (0 to l−1). Aubrey places strawberries at positions 0, a, 2a, 3a, … (mod l) until it cycles back. Sunny places blueberries at 0, b, 2b, 3b, … (mod l) until his cycle repeats. Count slices that get both a strawberry and a blueberry.</p><p>Input: t test cases, each with l, a, b (2 ≤ l ≤ 22, 1 ≤ a, b ≤ 21)<br>Output: Count of ideal (both-berry) slices per test case.</p><h3>Key Insight</h3><p>The set of positions visited by step a on a circle of size l is exactly the multiples of gcd(a, l) — i.e., {0, g₁, 2g₁, …} where g₁ = gcd(a, l). Similarly for blueberries with g₂ = gcd(b, l).</p><p>A slice has both berries iff its position is a multiple of both g₁ and g₂ — i.e., a multiple of lcm(g₁, g₂).</p><p>Within [0, l), the count of such multiples is l / lcm(g₁, g₂).</p><h3>Examples</h3><p>Input: 6 2 3</p><ul><li>g₁ = gcd(2,6) = 2 → strawberries at {0, 2, 4}</li><li>g₂ = gcd(3,6) = 3 → blueberries at {0, 3}</li><li>L = lcm(2,3) = 6 → multiples of 6 in [0,6) = {0} → count = 6/6 = 1 ✅</li></ul><p>Input: 10 4 7</p><ul><li>g₁ = gcd(4,10) = 2, g₂ = gcd(7,10) = 1</li><li>L = lcm(2,1) = 2 → count = 10/2 = 5 ✅</li></ul><p>Input: 4 10 21</p><ul><li>g₁ = gcd(10,4) = 2, g₂ = gcd(21,4) = 1</li><li>L = lcm(2,1) = 2 → count = 4/2 = 2 ✅</li></ul><h3>Algorithm</h3><ol><li>Read t.</li><li>For each test case:</li></ol><ul><li>g1 = gcd(a, l)</li><li>g2 = gcd(b, l)</li><li>L = lcm(g1, g2)</li><li>Answer = l / L.</li></ul><p>3. Print.</p><h3>Code Walkthrough</h3><pre>int l, a, b;<br>cin &gt;&gt; l &gt;&gt; a &gt;&gt; b;<br><br>int g1 = __gcd(a, l);       // step of strawberry set<br>int g2 = __gcd(b, l);       // step of blueberry set<br>int L  = lcm(g1, g2);       // common step of intersection<br>cout &lt;&lt; l / L &lt;&lt; endl;      // count of common multiples in [0, l)</pre><ul><li>__gcd(a, l) → the &quot;actual step&quot; on the circle (positions are all multiples of this).</li><li>lcm(g1, g2) → the step of the intersection set (multiples of both).</li><li>Number of multiples of L in [0, l) = l / L (integer division; L divides l since both g₁, g₂ | l).</li></ul><h3>Complexity</h3><ul><li>Time: O(log(min(a,l)) + log(min(b,l))) per test case — for gcd/lcm.</li><li>Space: O(1)</li></ul><h3>Edge Cases</h3><ul><li>a = 1 or b = 1 → g = 1 → step 1 visits everything; intersection = other set fully ✅</li><li>a or b multiple of l → g = l → visits only slice 0. Intersection count handled by lcm = l → l/l = 1 ✅</li><li>g₁ = g₂ → L = g₁ → count = l/g₁ (full overlap) ✅</li></ul><h3>💡 Why l / L Is Always an Integer</h3><p>Since g₁ | l and g₂ | l, we have lcm(g₁, g₂) | l (both divide l, so their lcm also divides l). So integer division is exact — no rounding issues.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bbb027254e18" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Codeforces 1154A — Restoring Three Numbers solution in cpp]]></title>
            <link>https://medium.com/@nafissiddiky2/codeforces-1154a-restoring-three-numbers-solution-in-cpp-9fcd88b4671d?source=rss-4ab4e65e7f15------2</link>
            <guid isPermaLink="false">https://medium.com/p/9fcd88b4671d</guid>
            <category><![CDATA[cpp-programming]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[codeforces]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Saad Al Nafis Siddiky]]></dc:creator>
            <pubDate>Fri, 27 Nov 2026 09:36:00 GMT</pubDate>
            <atom:updated>2026-11-27T09:36:00.843Z</atom:updated>
            <content:encoded><![CDATA[<h3>roblem Summary</h3><p>Given four integers a₁, a₂, a₃, a₄ which are the pairwise sums of three hidden positive integers x, y, z, specifically:</p><ul><li>x + y, x + z, y + z, and x + y + z</li></ul><p>Find and output any valid triple x, y, z.</p><p>Input: Four integers (the four sums, in any order)<br>Output: Three integers x y z (any valid triple).</p><h3>Key Insight</h3><p>The largest of the four numbers must be x + y + z (since it&#39;s the sum of all three positives, it&#39;s always the biggest).</p><p>If we sort so that a[3] is the largest (total), then:</p><ul><li>x = total - a[1]</li><li>y = total - a[2]</li><li>z = total - a[0]</li></ul><p>Because if a[0], a[1], a[2] are the three pairwise sums, subtracting each from the total leaves the remaining variable.</p><h3>Examples</h3><p>Input: 3 6 5 4</p><ul><li>Sorted: 3 4 5 6 → total = 6</li><li>6 - 4 = 2, 6 - 5 = 1, 6 - 3 = 3</li></ul><p>Output: 2 1 3 ✅ (check: 2+1=3 ✓, 2+3=5 ✓, 1+3=4 ✓, 2+1+3=6 ✓)</p><h3>Algorithm</h3><ol><li>Read 4 integers into array a.</li><li>Sort ascending.</li><li>total = a[3] (largest = x+y+z).</li><li>Print total - a[1], total - a[2], total - a[0].</li></ol><h3>Code Walkthrough</h3><pre>int a[4];<br>for (int i = 0; i &lt; 4; i++) cin &gt;&gt; a[i];<br>sort(a, a + 4);                              // ascending order<br>cout &lt;&lt; (a[3] - a[1]) &lt;&lt; &quot; &quot;<br>     &lt;&lt; (a[3] - a[2]) &lt;&lt; &quot; &quot;<br>     &lt;&lt; (a[3] - a[0]) &lt;&lt; endl;</pre><ul><li>After sorting: a[0] ≤ a[1] ≤ a[2] ≤ a[3]</li><li>a[3] = x + y + z (total)</li><li>Subtracting each of the three pairwise sums from the total yields the three variables.</li></ul><h3>Complexity</h3><ul><li>Time: O(1) — sorting 4 elements is constant.</li><li>Space: O(1)</li></ul><h3>Edge Cases</h3><ul><li>Equal values (e.g., x = y = z = 1 → sums 2 2 2 3) → sorted: 2 2 2 3, output 1 1 1 ✅</li><li>Order of output doesn’t matter — any valid x, y, z accepted ✅</li></ul><h3>💡 Why the Output Order Is Fine</h3><p>The problem says “print any order” — so subtracting in the specific order a[1], a[2], a[0] is arbitrary but correct. All three values come out right.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9fcd88b4671d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Codeforces 1676A — Lucky? solution in cpp]]></title>
            <link>https://medium.com/@nafissiddiky2/codeforces-1676a-lucky-solution-in-cpp-649e555d7bbf?source=rss-4ab4e65e7f15------2</link>
            <guid isPermaLink="false">https://medium.com/p/649e555d7bbf</guid>
            <category><![CDATA[cpp-programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[programming-contest]]></category>
            <category><![CDATA[codeforces]]></category>
            <dc:creator><![CDATA[Saad Al Nafis Siddiky]]></dc:creator>
            <pubDate>Thu, 26 Nov 2026 07:15:45 GMT</pubDate>
            <atom:updated>2026-11-26T07:15:45.688Z</atom:updated>
            <content:encoded><![CDATA[<h3>Problem Summary</h3><p>Given a 6-digit ticket number, determine if it’s “lucky” — meaning the sum of the first three digits equals the sum of the last three digits.</p><p>Input: t test cases, each with a 6-digit number n<br>Output: YES if lucky, else NO</p><h3>Key Insight</h3><p>Split the 6-digit number into two 3-digit halves, compute digit sum of each, compare.</p><ul><li>First half = n / 1000 (integer division drops last 3 digits)</li><li>Last half = n % 1000 (remainder keeps last 3 digits)</li></ul><h3>Examples</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/764/1*TgY5bavEIch5v7twuZfFZw.png" /></figure><h3>Algorithm</h3><ol><li>Read t.</li><li>For each test case:</li></ol><ul><li>Read n as integer.</li><li>fn = n / 1000, ln = n % 1000.</li><li>Sum digits of fn → f.</li><li>Sum digits of ln → l.</li><li>If f == l → YES, else NO.</li></ul><h3>Code Walkthrough</h3><pre>int n; cin &gt;&gt; n;<br>int f = 0, l = 0;<br>int fn = n / 1000;   // first 3 digits<br>int ln = n % 1000;   // last 3 digits<br><br>while (fn != 0) {<br>    f += fn % 10;    // extract last digit, add to sum<br>    fn /= 10;        // drop last digit<br>}<br>while (ln != 0) {<br>    l += ln % 10;<br>    ln /= 10;<br>}<br><br>if (f == l) cout &lt;&lt; &quot;YES&quot; &lt;&lt; endl;<br>else        cout &lt;&lt; &quot;NO&quot;  &lt;&lt; endl;</pre><ul><li>n / 1000 → e.g., 213132 / 1000 = 213 ✅</li><li>n % 1000 → e.g., 213132 % 1000 = 132 ✅</li><li>Digit-sum loops use the standard % 10 + / 10 pattern.</li></ul><h3>Complexity</h3><ul><li>Time: O(1) per test case — fixed 3 digits each half.</li><li>Space: O(1)</li></ul><h3>Edge Cases</h3><ul><li>Leading zeros, e.g., 001001 → fn = 1, ln = 1 → both sum to 1 → YES ✅</li><li>All zeros 000000 → fn = 0, ln = 0 → loops don&#39;t run → f = l = 0 → YES ✅</li><li>n read as int — max 999999 fits comfortably ✅</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=649e555d7bbf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Codeforces 732A — Buy a Shovel solution in cpp]]></title>
            <link>https://medium.com/@nafissiddiky2/codeforces-732a-buy-a-shovel-solution-in-cpp-c835bff126e0?source=rss-4ab4e65e7f15------2</link>
            <guid isPermaLink="false">https://medium.com/p/c835bff126e0</guid>
            <category><![CDATA[cps]]></category>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[codeforces]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[cpp-programming]]></category>
            <dc:creator><![CDATA[Saad Al Nafis Siddiky]]></dc:creator>
            <pubDate>Wed, 25 Nov 2026 14:32:48 GMT</pubDate>
            <atom:updated>2026-11-25T14:32:48.253Z</atom:updated>
            <content:encoded><![CDATA[<h3>Problem Summary</h3><p>You have an infinite supply of 10-burle coins and exactly one r-burle coin. A shovel costs k burles. Find the minimum number of shovels you must buy so that you can pay the exact total without needing change.</p><p>Input: k (shovel price) and r (the single coin denomination, 1 ≤ r ≤ 9)<br>Output: Minimum number of shovels to buy.</p><h3>Key Insight</h3><p>You can pay exactly if the total cost k × i ends in:</p><ul><li>0 → payable using only 10-burle coins ✅</li><li>r → payable using one r-coin + 10-burle coins ✅</li></ul><p>So we want the smallest i ≥ 1 such that (k × i) % 10 == 0 or (k × i) % 10 == r.</p><h3>Examples</h3><p>Input: k = 117, r = 3</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*qoBBK4xboHhFffSmtkpayQ.png" /></figure><p>Output: 9</p><h3>Algorithm</h3><ol><li>Read k and r.</li><li>For i = 1, 2, 3, ...:</li></ol><ul><li>If (k × i) % 10 == 0 → print i, stop.</li><li>If (k × i) % 10 == r → print i, stop.</li></ul><p>4. (The loop is guaranteed to terminate within ≤ 10 iterations.)</p><h3>Code Walkthrough</h3><pre>int k, r;<br>cin &gt;&gt; k &gt;&gt; r;<br>int tem = 1000;                  // arbitrary upper bound<br>for (int i = 1; i &lt; tem; i++) {<br>    if ((k * i) % 10 == 0) {     // total is multiple of 10 → pay with 10s<br>        cout &lt;&lt; i &lt;&lt; endl;<br>        break;<br>    }<br>    else if (((k * i) - r) % 10 == 0) {  // last digit == r → use the r-coin<br>        cout &lt;&lt; i &lt;&lt; endl;<br>        break;<br>    }<br>}</pre><ul><li>(k*i) % 10 == 0 → last digit is 0.</li><li>((k*i) - r) % 10 == 0 → equivalent to (k*i) % 10 == r → last digit is r.</li></ul><h3>Complexity</h3><ul><li>Time: O(1) — at most 10 iterations (since k × 10 always ends in 0).</li><li>Space: O(1)</li></ul><h3>Edge Cases</h3><ul><li>r = 0? Not possible (r ≥ 1), so the two conditions don&#39;t overlap trivially — but even if they did, the first branch catches it.</li><li>k already ends in 0 (e.g., k=10) → i=1 → immediate YES ✅</li><li>k ends in r (e.g., k=7, r=7) → i=1 → second condition matches ✅</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c835bff126e0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Codeforces 1903A — Halloumi Boxes solution in cpp]]></title>
            <link>https://medium.com/@nafissiddiky2/codeforces-1903a-halloumi-boxes-solution-in-cpp-4e9c8524e061?source=rss-4ab4e65e7f15------2</link>
            <guid isPermaLink="false">https://medium.com/p/4e9c8524e061</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[cpp-programming]]></category>
            <category><![CDATA[cps]]></category>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[codeforces]]></category>
            <dc:creator><![CDATA[Saad Al Nafis Siddiky]]></dc:creator>
            <pubDate>Tue, 24 Nov 2026 12:41:45 GMT</pubDate>
            <atom:updated>2026-11-24T12:41:45.768Z</atom:updated>
            <content:encoded><![CDATA[<h3>Problem Summary</h3><p>Given an array a of n integers and an integer k, you may perform any number of operations. In one operation, you can reverse any subarray of length at most k. Determine if the array can be sorted in non-decreasing order.</p><p>Input: t test cases; each has n, k, then n integers<br>Output: YES if sortable, else NO</p><h3>Key Insight</h3><ul><li>If k ≥ 2: A reversal of length 2 is just an adjacent swap. With adjacent swaps you can perform bubble sort → any array can be sorted → always YES.</li><li>If k == 1: Reversing length-1 segments does nothing → array stays unchanged → answer is YES only if the array is already sorted.</li></ul><h3>Examples</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/778/1*CFiZ9wjJYHv8BXyZFj62aw.png" /></figure><h3>Algorithm</h3><ol><li>Read t.</li><li>For each test case:</li></ol><ul><li>Read n, k, and array a.</li><li>If k &gt;= 2 → print YES.</li><li>Else (k == 1):</li><li>Check if a is non-decreasing.</li><li>Print YES if sorted, else NO.</li></ul><h3>Code Walkthrough</h3><pre>int n, k;<br>cin &gt;&gt; n &gt;&gt; k;<br>int a[n];<br>for (int i = 0; i &lt; n; i++) cin &gt;&gt; a[i];<br><br>if (k &gt;= 2) {<br>    cout &lt;&lt; &quot;YES&quot; &lt;&lt; endl;        // adjacent swaps possible → always sortable<br>}<br>else {<br>    bool s = true;<br>    for (int i = 1; i &lt; n; i++) {<br>        if (a[i] &lt; a[i-1]) {      // found a descent → not sorted<br>            s = false;<br>            break;<br>        }<br>    }<br>    if (s) cout &lt;&lt; &quot;YES&quot; &lt;&lt; endl;<br>    else   cout &lt;&lt; &quot;NO&quot;  &lt;&lt; endl;<br>}</pre><ul><li>k &gt;= 2 → shortcut to YES (the core observation).</li><li>k == 1 → fallback: linear check for sortedness.</li><li>s flag flips to false at the first violation and breaks early.</li></ul><h3>Complexity</h3><ul><li>Time: O(n) per test case — reading array O(n), sortedness check O(n).</li><li>Space: O(n) for the array.</li></ul><h3>Edge Cases</h3><ul><li>n = 1 → always sorted → YES regardless of k ✅</li><li>k = 1 with duplicates like 1 1 2 → non-decreasing (&lt; is strict) → YES ✅</li><li>k ≥ 2 even with any wild array → always YES ✅</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4e9c8524e061" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Codeforces 1669A — Division? solution in cpp]]></title>
            <link>https://medium.com/@nafissiddiky2/codeforces-1669a-division-solution-in-cpp-c82df20da906?source=rss-4ab4e65e7f15------2</link>
            <guid isPermaLink="false">https://medium.com/p/c82df20da906</guid>
            <category><![CDATA[codeforces]]></category>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[cpp-programming]]></category>
            <category><![CDATA[cps]]></category>
            <dc:creator><![CDATA[Saad Al Nafis Siddiky]]></dc:creator>
            <pubDate>Mon, 23 Nov 2026 05:11:24 GMT</pubDate>
            <atom:updated>2026-11-23T05:11:24.771Z</atom:updated>
            <content:encoded><![CDATA[<h3>Problem Summary</h3><p>Given a Codeforces rating r, classify it into one of four divisions:</p><ul><li>Division 1: r ≥ 1900</li><li>Division 2: 1600 ≤ r ≤ 1899</li><li>Division 3: 1400 ≤ r ≤ 1599</li><li>Division 4: r ≤ 1399</li></ul><p>Input: t test cases, each with an integer rating r<br>Output: The division string for each rating.</p><h3>Key Insight</h3><p>The ranges are contiguous and ordered. Use a chain of if / else if from highest to lowest — the first matching condition wins, so you only need to check the lower bound of each range (the upper bound is implied by the previous else).</p><h3>Examples</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*ThRu0iNoDUfIze04kmhGIg.png" /></figure><h3>Algorithm</h3><ol><li>Read t.</li><li>For each test case:</li></ol><ul><li>Read r.</li><li>If r &gt;= 1900 → Division 1</li><li>Else if r &gt;= 1600 → Division 2</li><li>Else if r &gt;= 1400 → Division 3</li><li>Else → Division 4</li></ul><p>3. Print the result.</p><h3>Code Walkthrough</h3><pre>while (t--) {<br>    int r;<br>    cin &gt;&gt; r;<br>    if (r &gt;= 1900) {<br>        cout &lt;&lt; &quot;Division 1&quot; &lt;&lt; endl;<br>    }<br>    else if (r &gt;= 1600 &amp;&amp; r &lt;= 1899) {   // upper bound redundant<br>        cout &lt;&lt; &quot;Division 2&quot; &lt;&lt; endl;<br>    }<br>    else if (r &gt;= 1400 &amp;&amp; r &lt;= 1599) {   // upper bound redundant<br>        cout &lt;&lt; &quot;Division 3&quot; &lt;&lt; endl;<br>    }<br>    else {<br>        cout &lt;&lt; &quot;Division 4&quot; &lt;&lt; endl;    // catches everything ≤ 1399<br>    }<br>}</pre><ul><li>The &amp;&amp; r &lt;= 1899 and &amp;&amp; r &lt;= 1599 parts are redundant — if r were ≥ 1900, the first branch would have caught it. Removing them is cleaner but doesn&#39;t change correctness.</li></ul><h3>Complexity</h3><ul><li>Time: O(t)</li><li>Space: O(1)</li></ul><h3>Edge Cases</h3><ul><li>Boundary values (1900, 1600, 1400, 1899, 1599, 1399) → all handled correctly by &gt;= checks ✅</li><li>Negative ratings → Division 4 ✅</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c82df20da906" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Codeforces 750A — New Year and Hurry solution in cpp]]></title>
            <link>https://medium.com/@nafissiddiky2/codeforces-750a-new-year-and-hurry-solution-in-cpp-a7cd2013ae06?source=rss-4ab4e65e7f15------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7cd2013ae06</guid>
            <category><![CDATA[cpp-programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[codeforces]]></category>
            <category><![CDATA[cps]]></category>
            <dc:creator><![CDATA[Saad Al Nafis Siddiky]]></dc:creator>
            <pubDate>Mon, 23 Nov 2026 05:01:33 GMT</pubDate>
            <atom:updated>2026-11-23T05:01:33.066Z</atom:updated>
            <content:encoded><![CDATA[<h3>Problem Summary</h3><p>A person has 240 minutes total before midnight. They need k minutes to travel to a party, leaving 240 - k minutes for solving problems. The i-th problem takes 5 × i minutes. Find the maximum number of problems they can solve.</p><p>Input: n (number of problems, 1 ≤ n ≤ 10) and k (travel time, 1 ≤ k ≤ 240)<br>Output: Maximum problems solvable.</p><h3>Key Insight</h3><p>Problems take increasing time: 5, 10, 15, 20, … minutes. Simulate solving problems in order, subtracting 5×i from available time. Stop as soon as time runs out.</p><h3>Examples</h3><p>Input: 3 222</p><ul><li>Available time = 240 − 222 = 18</li><li>Problem 1: 18 − 5 = 13 ≥ 0 ✅</li><li>Problem 2: 13 − 10 = 3 ≥ 0 ✅</li><li>Problem 3: 3 − 15 = −12 ❌ → break</li></ul><p>Output: 2</p><h3>Algorithm</h3><ol><li>Read n and k.</li><li>time = 240 - k (minutes available for solving).</li><li>count = 0.</li><li>For i = 1 to n:</li></ol><ul><li>time -= 5 * i</li><li>If time &gt;= 0 → count++, else break.</li></ul><p>5. Print count.</p><h3>Code Walkthrough</h3><pre>int n, k;<br>cin &gt;&gt; n &gt;&gt; k;<br>int time = 240 - k;        // remaining minutes for problems<br>int count = 0;<br>for (int i = 1; i &lt;= n; i++) {<br>    time -= 5 * i;         // time needed for problem i<br>    if (time &gt;= 0) {<br>        count++;           // enough time → solve it<br>    } else {<br>        break;             // no time left → stop (problems get harder)<br>    }<br>}<br>cout &lt;&lt; count &lt;&lt; endl;</pre><ul><li>5 * i → time for the i-th problem.</li><li>break is valid because times are monotonically increasing — once one fails, all later ones fail too.</li></ul><h3>Complexity</h3><ul><li>Time: O(n) — at most 10 iterations.</li><li>Space: O(1)</li></ul><h3>Edge Cases</h3><ul><li>k = 240 → time = 0 → first problem fails → count = 0 ✅</li><li>n small but k small → can solve all n ✅</li><li>k = 1 → plenty of time → solves n problems ✅</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7cd2013ae06" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>