<?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:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[DMITRII’s Substack]]></title><description><![CDATA[I post a problem that I solve every single day]]></description><link>https://dmitriisamoilenko.substack.com</link><image><url>https://substackcdn.com/image/fetch/$s_!veqy!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7119d0c1-ffa4-4846-9b7d-c7cef7d87b92_813x453.png</url><title>DMITRII’s Substack</title><link>https://dmitriisamoilenko.substack.com</link></image><generator>Substack</generator><lastBuildDate>Thu, 30 Jul 2026 07:29:40 GMT</lastBuildDate><atom:link href="https://dmitriisamoilenko.substack.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[DMITRII SAMOILENKO]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[dmitriisamoilenko@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[dmitriisamoilenko@substack.com]]></itunes:email><itunes:name><![CDATA[DMITRII SAMOILENKO]]></itunes:name></itunes:owner><itunes:author><![CDATA[DMITRII SAMOILENKO]]></itunes:author><googleplay:owner><![CDATA[dmitriisamoilenko@substack.com]]></googleplay:owner><googleplay:email><![CDATA[dmitriisamoilenko@substack.com]]></googleplay:email><googleplay:author><![CDATA[DMITRII SAMOILENKO]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[# 29.07.2026 [3518. Smallest Palindromic Rearrangement II]]]></title><description><![CDATA[K-th palindrome rearrangement]]></description><link>https://dmitriisamoilenko.substack.com/p/29072026-3518-smallest-palindromic</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/29072026-3518-smallest-palindromic</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Wed, 29 Jul 2026 09:17:19 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!H51C!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf80eca7-a69d-420b-b594-116cc2f7d84b_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>29.07.2026</h1><p><a href="https://leetcode.com/problems/smallest-palindromic-rearrangement-ii/solutions/8427960/kotlin-rust-by-samoylenkodmitry-rw9d/">3518. Smallest Palindromic Rearrangement II</a> hard</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!H51C!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf80eca7-a69d-420b-b594-116cc2f7d84b_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!H51C!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf80eca7-a69d-420b-b594-116cc2f7d84b_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!H51C!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf80eca7-a69d-420b-b594-116cc2f7d84b_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!H51C!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf80eca7-a69d-420b-b594-116cc2f7d84b_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!H51C!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf80eca7-a69d-420b-b594-116cc2f7d84b_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!H51C!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf80eca7-a69d-420b-b594-116cc2f7d84b_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bf80eca7-a69d-420b-b594-116cc2f7d84b_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;29.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="29.07.2026" title="29.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!H51C!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf80eca7-a69d-420b-b594-116cc2f7d84b_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!H51C!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf80eca7-a69d-420b-b594-116cc2f7d84b_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!H51C!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf80eca7-a69d-420b-b594-116cc2f7d84b_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!H51C!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf80eca7-a69d-420b-b594-116cc2f7d84b_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-Nguogeufyeo" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;Nguogeufyeo&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/Nguogeufyeo?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1435</p><h4>Problem TLDR</h4><p>K-th palindrome rearrangement</p><h4>Intuition</h4><p>Didn&#8217;t solved (even after yesterday preparation)<br>```j<br>// i looked at this problem yesterday<br>// let&#8217;s see if i could solve it today...<br>//<br>// btw yesterday acceptance rate in leetcode was 14%, now it is 30%<br>//<br>// this is a hard math statistics problem<br>// first take the half of the frequencies<br>//<br>// now we have to construct it letter by letter from &#8216;a&#8217; to &#8216;z&#8217;<br>// to take a letter the count of possible permutations of leftovers f&#8217;s <br>// should be less than k<br>//<br>// now the hard part is to count how many permutations we have<br>//<br>// p = n!/f!(n-f)! n - total count, f - frequency we take<br>//<br>// and we need to take entire leftover frequencies, meaning it is <br>// p1*p2*p3<br>//<br>// n!/f!(n-f)! is calculated with Pascal&#8217;s Triangle<br>// new row = previus left + right<br>// <br>// 26 minute: wrong answer <br>// 40 minute: MLE (pascal triangle n^2)<br>```<br>* count frequencies - the problem now is the K-th permutation of frequencies (halved)<br>* construct string letter by letter; consider each letter in &#8216;a&#8217;..&#8217;z&#8217;, take the first where the *suffix* permutations count of remaining frequency is bigger or equal than K</p><h4>Approach</h4><p>* number of permutations of remaining frequency is just taking each letter indifidually n!/f[c]!(n-f[c])! and muliplying, then adjust n -= f[c]<br>* n!/f!(n-f)! could be computed as res *= n-i+1/i without pascal triangle because we can and should stop when reaching the K</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(n)</p></li><li><p><strong>Space complexity:</strong> O(n)</p></li></ul><h4>Code</h4><pre><code><code>    fun smallestPalindrome(s: String, k: Int): String {
        var K = 1L*k; val max = K+1; val f = IntArray(26); for (c in s) ++f[c-'a']
        if (f.count { it%2 &gt; 0} &gt; 1) return ""; val mid = (0..25).firstOrNull { f[it]%2 &gt; 0}
        fun C(n: Int, r: Int) = (1..min(r,n-r)).fold(1L){ a,i -&gt; min(max, a*(n-i+1)/i) }
        fun P() = f.fold(1L to f.sum()) {(res,n), v -&gt; min(max, res*C(n,v)) to  n - v}.first
        for (i in 0..25) f[i] /= 2; if (K &gt; P()) return ""
        val half = buildString {
            for (step in 1..s.length/2) for (c in 0..25) if (f[c]&gt;0) {
                --f[c]; val cnt = P(); if (cnt &gt;= K){append('a'+c);break}; ++f[c]; K -= cnt
            }
        }
        return half + (mid?.let{'a'+it}?:"") + half.reversed()
    }</code></code></pre><pre><code><code>    pub fn smallest_palindrome(s: String, k: i32) -&gt; String {
        let (mut K, M, mut f, mut h)=(k as i64, k as i64+1, [0i64;26], vec![]);  
        for b in s.bytes() { f[(b-97)as usize]+=1}; let mid = (0..26).find(|&amp;i|f[i]%2&gt;0);
        if f.iter().map(|v|v%2).sum::&lt;i64&gt;()&gt;1 { return "".into()}; for v in &amp;mut f {*v/=2}
        let C=|n:i64,r:i64|(1..=r.min(n-r)).fold(1,|a,i|M.min(a*(n-i+1)/i));
        let P=|f:&amp;[i64]|f.iter().fold((1,f.iter().sum()),|(r,n),&amp;v|(M.min(r*C(n,v)),n-v)).0;
        if K &gt; P(&amp;f) { return "".into()}
        for _ in 0..s.len()/2 { for i in 0..26 { if f[i] &gt; 0 {
            f[i]-=1; let cnt = P(&amp;f); if cnt&gt;=K {h.push(97+i as u8);break};f[i]+=1; K-=cnt }}}
        let mut res = h.clone(); if let Some(i) = mid { res.push(97+i as u8)}
        res.extend(h.into_iter().rev()); String::from_utf8(res).unwrap()
    }</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 28.07.2026 [3517. Smallest Palindromic Rearrangement I]]]></title><description><![CDATA[Smallest palindrome rearrangement]]></description><link>https://dmitriisamoilenko.substack.com/p/28072026-3517-smallest-palindromic</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/28072026-3517-smallest-palindromic</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Tue, 28 Jul 2026 07:56:42 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!vlkZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e497e70-b2c4-41d8-89ac-4c8c5ab1824e_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>28.07.2026</h1><p><a href="https://leetcode.com/problems/smallest-palindromic-rearrangement-i/solutions/8425649/kotlin-rust-by-samoylenkodmitry-ur04/">3517. Smallest Palindromic Rearrangement I</a> medium</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vlkZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e497e70-b2c4-41d8-89ac-4c8c5ab1824e_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vlkZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e497e70-b2c4-41d8-89ac-4c8c5ab1824e_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!vlkZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e497e70-b2c4-41d8-89ac-4c8c5ab1824e_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!vlkZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e497e70-b2c4-41d8-89ac-4c8c5ab1824e_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!vlkZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e497e70-b2c4-41d8-89ac-4c8c5ab1824e_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vlkZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e497e70-b2c4-41d8-89ac-4c8c5ab1824e_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5e497e70-b2c4-41d8-89ac-4c8c5ab1824e_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;28.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="28.07.2026" title="28.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!vlkZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e497e70-b2c4-41d8-89ac-4c8c5ab1824e_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!vlkZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e497e70-b2c4-41d8-89ac-4c8c5ab1824e_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!vlkZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e497e70-b2c4-41d8-89ac-4c8c5ab1824e_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!vlkZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e497e70-b2c4-41d8-89ac-4c8c5ab1824e_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-VDUrEFWlksE" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;VDUrEFWlksE&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/VDUrEFWlksE?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1434</p><h4>Problem TLDR</h4><p>Smallest palindrome rearrangement</p><h4>Intuition</h4><p>Take first half and sort it.</p><h4>Approach</h4><p>* tip: prepare yourself for the k-th rearrangement</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(n)</p></li><li><p><strong>Space complexity:</strong> O(n)</p></li></ul><h4>Code</h4><pre><code><code>    fun smallestPalindrome(s: String) = run {
        val h = s.take(s.length/2).map{it}.sorted().joinToString("")
        h + (if (s.length%2&gt;0) s[s.length/2] else "") + h.reversed()
    }</code></code></pre><pre><code><code>    pub fn smallest_palindrome(s: String) -&gt; String {
        let n = s.len(); let h = s[..n/2].chars().sorted();
        h.clone().chain(s[n/2..(n+1)/2].chars()).chain(h.rev()).collect()
    }</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 27.07.2026 [1464. Maximum Product of Two Elements in an Array]]]></title><description><![CDATA[Max product in array]]></description><link>https://dmitriisamoilenko.substack.com/p/27072026-1464-maximum-product-of</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/27072026-1464-maximum-product-of</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Mon, 27 Jul 2026 07:21:32 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!skKb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb05b69b0-e56e-4136-a3ec-3947eb98a32e_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>27.07.2026</h1><p><a href="https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/solutions/8423239/kotlin-rust-by-samoylenkodmitry-il0u/">1464. Maximum Product of Two Elements in an Array</a> easy</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!skKb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb05b69b0-e56e-4136-a3ec-3947eb98a32e_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!skKb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb05b69b0-e56e-4136-a3ec-3947eb98a32e_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!skKb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb05b69b0-e56e-4136-a3ec-3947eb98a32e_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!skKb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb05b69b0-e56e-4136-a3ec-3947eb98a32e_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!skKb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb05b69b0-e56e-4136-a3ec-3947eb98a32e_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!skKb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb05b69b0-e56e-4136-a3ec-3947eb98a32e_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b05b69b0-e56e-4136-a3ec-3947eb98a32e_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;27.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="27.07.2026" title="27.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!skKb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb05b69b0-e56e-4136-a3ec-3947eb98a32e_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!skKb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb05b69b0-e56e-4136-a3ec-3947eb98a32e_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!skKb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb05b69b0-e56e-4136-a3ec-3947eb98a32e_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!skKb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb05b69b0-e56e-4136-a3ec-3947eb98a32e_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-85Lxmbl2hKY" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;85Lxmbl2hKY&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/85Lxmbl2hKY?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1433</p><h4>Problem TLDR</h4><p>Max product in array</p><h4>Intuition</h4><p>Brute-force: iterate in a nested &#8216;for&#8217; loops. Accepted.</p><h4>Approach</h4><p>* we can sort and take two max values<br>* we can scan and find two max values</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(n)</p></li><li><p><strong>Space complexity:</strong> O(1)</p></li></ul><h4>Code</h4><pre><code><code>    fun maxProduct(n: IntArray) =
    n.sorted().run{(last()-1)*(get(n.size-2)-1)}</code></code></pre><pre><code><code>    pub fn max_product(n: Vec&lt;i32&gt;) -&gt; i32 {
        n.iter().map(|&amp;x|x-1).k_largest(2).product::&lt;i32&gt;()
    }</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 26.07.2026 [628. Maximum Product of Three Numbers]]]></title><description><![CDATA[Max product of 3 numbers]]></description><link>https://dmitriisamoilenko.substack.com/p/26072026-628-maximum-product-of-three</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/26072026-628-maximum-product-of-three</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Sun, 26 Jul 2026 09:37:35 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!x7vQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F365b65b2-d131-4df2-8b42-8e3ce46f2963_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>26.07.2026</h1><p><a href="https://leetcode.com/problems/maximum-product-of-three-numbers/solutions/8421251/kotlin-rust-by-samoylenkodmitry-ym93/">628. Maximum Product of Three Numbers</a> easy</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!x7vQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F365b65b2-d131-4df2-8b42-8e3ce46f2963_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!x7vQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F365b65b2-d131-4df2-8b42-8e3ce46f2963_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!x7vQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F365b65b2-d131-4df2-8b42-8e3ce46f2963_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!x7vQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F365b65b2-d131-4df2-8b42-8e3ce46f2963_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!x7vQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F365b65b2-d131-4df2-8b42-8e3ce46f2963_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!x7vQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F365b65b2-d131-4df2-8b42-8e3ce46f2963_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/365b65b2-d131-4df2-8b42-8e3ce46f2963_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;26.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="26.07.2026" title="26.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!x7vQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F365b65b2-d131-4df2-8b42-8e3ce46f2963_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!x7vQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F365b65b2-d131-4df2-8b42-8e3ce46f2963_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!x7vQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F365b65b2-d131-4df2-8b42-8e3ce46f2963_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!x7vQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F365b65b2-d131-4df2-8b42-8e3ce46f2963_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-DDMgAKSD1V0" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;DDMgAKSD1V0&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/DDMgAKSD1V0?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1432</p><h4>Problem TLDR</h4><p>Max product of 3 numbers</p><h4>Intuition</h4><p>Take 3 largest and compare with taking two smallest and a one largest.</p><h4>Approach</h4><p>* Rust itertools has k_largest, s_smallest (the results are sorted differently)</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(n)</p></li><li><p><strong>Space complexity:</strong> O(1)</p></li></ul><h4>Code</h4><pre><code><code>    fun maximumProduct(n: IntArray) = run { n.sort()
    val l = n.size-1; max(n[0]*n[1]*n[l], n[l]*n[l-1]*n[l-2])}</code></code></pre><pre><code><code>    pub fn maximum_product(n: Vec&lt;i32&gt;) -&gt; i32 {
        let (a,b) = n.iter().copied().k_smallest(2).collect_tuple().unwrap();
        let (z,y,x) = n.iter().copied().k_largest(3).collect_tuple().unwrap();
        (a*b*z).max(x*y*z)
    }</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 25.07.2026 [3536. Maximum Product of Two Digits]]]></title><description><![CDATA[Product of two largest digits in n]]></description><link>https://dmitriisamoilenko.substack.com/p/25072026-3536-maximum-product-of</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/25072026-3536-maximum-product-of</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Sat, 25 Jul 2026 09:18:52 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!uN8s!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F017e39f2-e792-454c-b446-50583421c799_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>25.07.2026</h1><p><a href="https://leetcode.com/problems/maximum-product-of-two-digits/solutions/8418932/kotlin-rust-by-samoylenkodmitry-ylz1/">3536. Maximum Product of Two Digits</a> easy</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uN8s!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F017e39f2-e792-454c-b446-50583421c799_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uN8s!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F017e39f2-e792-454c-b446-50583421c799_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!uN8s!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F017e39f2-e792-454c-b446-50583421c799_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!uN8s!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F017e39f2-e792-454c-b446-50583421c799_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!uN8s!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F017e39f2-e792-454c-b446-50583421c799_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uN8s!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F017e39f2-e792-454c-b446-50583421c799_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/017e39f2-e792-454c-b446-50583421c799_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;25.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="25.07.2026" title="25.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!uN8s!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F017e39f2-e792-454c-b446-50583421c799_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!uN8s!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F017e39f2-e792-454c-b446-50583421c799_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!uN8s!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F017e39f2-e792-454c-b446-50583421c799_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!uN8s!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F017e39f2-e792-454c-b446-50583421c799_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-xvdJKKuH04w" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;xvdJKKuH04w&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/xvdJKKuH04w?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1431</p><h4>Problem TLDR</h4><p>Product of two largest digits in n</p><h4>Intuition</h4><p>Convert to string. Sort.</p><h4>Approach</h4><p>* Rust itertools has k_largest</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(logn*log log n)</p></li><li><p><strong>Space complexity:</strong> O(logn)</p></li></ul><h4>Code</h4><pre><code><code>    fun maxProduct(n: Int) = 
    "$n".map{it-'0'}.sorted().takeLast(2).let{it[0]*it[1]}</code></code></pre><pre><code><code>    pub fn max_product(n: i32) -&gt; i32 {
        format!("0{n}").bytes().k_largest(2).map(|b| b as i32 - 48).product()
    }</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 24.07.2026 [3514. Number of Unique XOR Triplets II]]]></title><description><![CDATA[Uniq xor of tripplets in array]]></description><link>https://dmitriisamoilenko.substack.com/p/24072026-3514-number-of-unique-xor</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/24072026-3514-number-of-unique-xor</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Fri, 24 Jul 2026 08:37:37 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!ukCL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63eec11a-68b6-4d9c-8350-96049bc47257_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>24.07.2026</h1><p><a href="https://leetcode.com/problems/number-of-unique-xor-triplets-ii/solutions/8416716/kotlin-rust-by-samoylenkodmitry-09gn/">3514. Number of Unique XOR Triplets II</a> medium</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ukCL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63eec11a-68b6-4d9c-8350-96049bc47257_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ukCL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63eec11a-68b6-4d9c-8350-96049bc47257_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!ukCL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63eec11a-68b6-4d9c-8350-96049bc47257_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!ukCL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63eec11a-68b6-4d9c-8350-96049bc47257_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!ukCL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63eec11a-68b6-4d9c-8350-96049bc47257_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ukCL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63eec11a-68b6-4d9c-8350-96049bc47257_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/63eec11a-68b6-4d9c-8350-96049bc47257_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;24.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="24.07.2026" title="24.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!ukCL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63eec11a-68b6-4d9c-8350-96049bc47257_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!ukCL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63eec11a-68b6-4d9c-8350-96049bc47257_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!ukCL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63eec11a-68b6-4d9c-8350-96049bc47257_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!ukCL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63eec11a-68b6-4d9c-8350-96049bc47257_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-g8eSf8yuXjQ" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;g8eSf8yuXjQ&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/g8eSf8yuXjQ?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1430</p><h4>Problem TLDR</h4><p>Uniq xor of tripplets in array</p><h4>Intuition</h4><p>The number of uniq xors is very compressable, we can assume that converting n^2 pairwise to set gives a small colleciton</p><h4>Approach</h4><p>* to speed up more use boolean array/bitset</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(n^2)</p></li><li><p><strong>Space complexity:</strong> O(n^2)</p></li></ul><h4>Code</h4><pre><code><code>    fun uniqueXorTriplets(n: IntArray) = buildSet { 
        for (x in buildSet { for (x in n) for (y in n) add(x xor y) }) 
            for (y in n) add(x xor y) }.size</code></code></pre><pre><code><code>    pub fn unique_xor_triplets(mut n: Vec&lt;i32&gt;) -&gt; i32 {
        n.sort_unstable(); n.dedup();
        let mut s = [false; 1 &lt;&lt; 12]; let mut t = s.clone();
        for i in 0..n.len() { for j in i..n.len() { s[(n[i] ^ n[j]) as usize] = true }}
        for x in 0..s.len() { if s[x] { for &amp;v in &amp;n { t[x ^ v as usize] = true }}}
        t.iter().filter(|&amp;&amp;b| b).count() as _
    }</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 23.07.2026 [3513. Number of Unique XOR Triplets I]]]></title><description><![CDATA[Uniq xor of tripplets of 1..n]]></description><link>https://dmitriisamoilenko.substack.com/p/23072026-3513-number-of-unique-xor</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/23072026-3513-number-of-unique-xor</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Thu, 23 Jul 2026 07:20:11 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!VhTj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b080788-e4b1-446f-8926-ef058e9a111a_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>23.07.2026</h1><p><a href="https://leetcode.com/problems/number-of-unique-xor-triplets-i/solutions/8414589/kotlin-rust-by-samoylenkodmitry-npoq/">3513. Number of Unique XOR Triplets I</a> medium</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VhTj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b080788-e4b1-446f-8926-ef058e9a111a_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VhTj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b080788-e4b1-446f-8926-ef058e9a111a_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!VhTj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b080788-e4b1-446f-8926-ef058e9a111a_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!VhTj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b080788-e4b1-446f-8926-ef058e9a111a_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!VhTj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b080788-e4b1-446f-8926-ef058e9a111a_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VhTj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b080788-e4b1-446f-8926-ef058e9a111a_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1b080788-e4b1-446f-8926-ef058e9a111a_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;23.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="23.07.2026" title="23.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!VhTj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b080788-e4b1-446f-8926-ef058e9a111a_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!VhTj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b080788-e4b1-446f-8926-ef058e9a111a_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!VhTj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b080788-e4b1-446f-8926-ef058e9a111a_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!VhTj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b080788-e4b1-446f-8926-ef058e9a111a_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-PNn1JVcV7wM" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;PNn1JVcV7wM&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/PNn1JVcV7wM?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1429</p><h4>Problem TLDR</h4><p>Uniq xor of tripplets of 1..n</p><h4>Intuition</h4><p>```j<br>// for xor order doesnt matter<br>// nums are the same 1..n<br>// 1 xor 1 = 0<br>// 1 ^ 0 = 1<br>// 1 ^ 0 ^ 1 = 0<br>// 1 ^ 0 ^ 0 = 1<br>// 1 ^ 1 ^ 1 = 1<br>// 1 ^ 1 ^ 0 = 0<br>// uniq?<br>```<br>Order doesnt matter. The maximum value 0b1xxxxx allows to construct any intermediate value by taking 1 xor 1 xor n[i] plus tail bits can construct any value of up to 2*max.</p><h4>Approach</h4><p>* simulate and see the results, then derive whats happening</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(logn)</p></li><li><p><strong>Space complexity:</strong> O(1)</p></li></ul><h4>Code</h4><pre><code><code>    fun uniqueXorTriplets(n: IntArray) =
    if (n.size &lt;3) n.size else 2*n.size.takeHighestOneBit()</code></code></pre><pre><code><code>    pub fn unique_xor_triplets(n: Vec&lt;i32&gt;) -&gt; i32 {
        let l = n.len() as i32; if l &lt; 3 { l } else { 1 &lt;&lt; (32 - l.leading_zeros()) }
    }</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 22.07.2026 [3501. Maximize Active Section with Trade II]]]></title><description><![CDATA[Queries l..r of max ones after replacing surrounding zeros in range]]></description><link>https://dmitriisamoilenko.substack.com/p/22072026-3501-maximize-active-section</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/22072026-3501-maximize-active-section</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Wed, 22 Jul 2026 09:54:24 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!ewhH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348699f2-2b22-4172-99a8-31cf4f7ecb0c_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>22.07.2026</h1><p><a href="https://leetcode.com/problems/maximize-active-section-with-trade-ii/solutions/8412830/kotlin-by-samoylenkodmitry-er7o/">3501. Maximize Active Section with Trade II</a> hard</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ewhH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348699f2-2b22-4172-99a8-31cf4f7ecb0c_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ewhH!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348699f2-2b22-4172-99a8-31cf4f7ecb0c_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!ewhH!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348699f2-2b22-4172-99a8-31cf4f7ecb0c_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!ewhH!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348699f2-2b22-4172-99a8-31cf4f7ecb0c_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!ewhH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348699f2-2b22-4172-99a8-31cf4f7ecb0c_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ewhH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348699f2-2b22-4172-99a8-31cf4f7ecb0c_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/348699f2-2b22-4172-99a8-31cf4f7ecb0c_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;22.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="22.07.2026" title="22.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!ewhH!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348699f2-2b22-4172-99a8-31cf4f7ecb0c_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!ewhH!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348699f2-2b22-4172-99a8-31cf4f7ecb0c_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!ewhH!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348699f2-2b22-4172-99a8-31cf4f7ecb0c_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!ewhH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348699f2-2b22-4172-99a8-31cf4f7ecb0c_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-8j-w662x-3g" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;8j-w662x-3g&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/8j-w662x-3g?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1428</p><h4>Problem TLDR</h4><p>Queries l..r of max ones after replacing surrounding zeros in range</p><h4>Intuition</h4><p>Didn&#8217;t solve.<br>```j<br>// 10 minute: how to shrink the sliding window and update max?<br>// 16 minute: no idea, lets look at hints - segment tree, no hope for that<br>```<br>Sort ranges by R. Scan string, track current zeros group and previous zeros group start and lengths.<br>BIT answers the question what is the max value in the suffix from L. We are filling the BIT by current position so it is always the range L..R.<br>Update BIT at start of the left group with value of both group sizes.<br>Query BIT by L of the query. <br><br>Corner case: BIT misses the first group if L happens to be at zeros. To overcome, track zeros counts and next zeros position for each position suffixes.</p><h4>Approach</h4><p>* threre are segment tree and sparse table solution which i didn&#8217;t get yet.</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(nlogn)</p></li><li><p><strong>Space complexity:</strong> O(n)</p></li></ul><h4>Code</h4><pre><code><code>    fun maxActiveSectionsAfterTrade(s: String, q: Array&lt;IntArray&gt;) = run {
        val n = s.length; val ones = s.count { it == '1' }; val bit = IntArray(n + 2)
        fun up(i: Int, v: Int) { var x = i + 1; while (x &gt; 0) { bit[x] = max(bit[x], v); x -= x and -x } }
        fun qu(i: Int)=run{ var x = i + 1; var v = 0; while (x &lt;= n + 1) { v = max(v, bit[x]); x += x and -x };v}
        val z = IntArray(n + 1); val nxt = IntArray(n + 1) { n }; val byR = q.indices.groupBy { q[it][1] }
        for (j in n - 1 downTo 0) if (s[j] == '0') { z[j] = 1 + z[j + 1]; nxt[j] = j } else nxt[j] = nxt[j + 1]
        val res = IntArray(q.size); var pStart = 0; var pLen = 0; var cLen = 0
        for (i in s.indices) {
            if (s[i] == '0') { cLen++; if (pLen &gt; 0) up(pStart, pLen + cLen) }
            else if (cLen &gt; 0) { pStart = i - cLen; pLen = cLen; cLen = 0 }
            byR[i]?.forEach { qi -&gt;
                val l = q[qi][0]; val g2 = if (s[l] == '0') nxt[l + z[l]] else n
                val g = if (g2 &lt;= i) minOf(z[l], i - l + 1) + minOf(z[g2], i - g2 + 1) else 0
                res[qi] = ones + maxOf(qu(l), g)
            }
        }; res
    }</code></code></pre><pre><code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 21.07.2026 [3499. Maximize Active Section with Trade I]]]></title><description><![CDATA[Max ones after replacing surrounding zeros]]></description><link>https://dmitriisamoilenko.substack.com/p/21072026-3499-maximize-active-section</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/21072026-3499-maximize-active-section</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Tue, 21 Jul 2026 07:46:01 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!MVvc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4569789e-b156-4430-b3e5-19a776148559_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>21.07.2026</h1><p><a href="https://leetcode.com/problems/maximize-active-section-with-trade-i/solutions/8410725/kotlin-rust-by-samoylenkodmitry-s4nd/">3499. Maximize Active Section with Trade I</a> medium</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MVvc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4569789e-b156-4430-b3e5-19a776148559_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MVvc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4569789e-b156-4430-b3e5-19a776148559_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!MVvc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4569789e-b156-4430-b3e5-19a776148559_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!MVvc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4569789e-b156-4430-b3e5-19a776148559_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!MVvc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4569789e-b156-4430-b3e5-19a776148559_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MVvc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4569789e-b156-4430-b3e5-19a776148559_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4569789e-b156-4430-b3e5-19a776148559_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;21.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="21.07.2026" title="21.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!MVvc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4569789e-b156-4430-b3e5-19a776148559_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!MVvc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4569789e-b156-4430-b3e5-19a776148559_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!MVvc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4569789e-b156-4430-b3e5-19a776148559_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!MVvc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4569789e-b156-4430-b3e5-19a776148559_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-jG3cmEjBY7c" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;jG3cmEjBY7c&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/jG3cmEjBY7c?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1427</p><h4>Problem TLDR</h4><p>Max ones after replacing surrounding zeros</p><h4>Intuition</h4><p>Count max of surrounding zeros plus total ones.</p><h4>Approach</h4><p>* only 4 variables necessary, but we can count ones in the same loop, that adds variables</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(n)</p></li><li><p><strong>Space complexity:</strong> O(1)</p></li></ul><h4>Code</h4><pre><code><code>    fun maxActiveSectionsAfterTrade(s: String): Int {
        var pp = 0; var p = 0; var c = 0; var ch = '1'
        return s.count {it=='1'}+s.maxOf { x -&gt;
            if (x == ch) c++  else { pp = p; p = c; c = 1; ch = x }
            if (x == '0' &amp;&amp; p&gt;0 &amp;&amp; pp&gt;0) pp+c else 0
        }
    }</code></code></pre><pre><code><code>    pub fn max_active_sections_after_trade(s: String) -&gt; i32 {
        let (mut pp, mut p, mut c, mut k) = (0, 0, 0, 0);
        s.bytes().filter(|&amp;b| b == 49).count() as i32 + s.bytes().map(|b| {
            if b == k { c += 1 } else { (pp, p, c, k) = (p, c, 1, b) }
            if b == 48 &amp;&amp; pp &gt; 0 { pp + c } else { 0 }
        }).max().unwrap_or(0) 
    }</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 20.07.2026 [1260. Shift 2D Grid]]]></title><description><![CDATA[Shift right 2d grid]]></description><link>https://dmitriisamoilenko.substack.com/p/20072026-1260-shift-2d-grid</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/20072026-1260-shift-2d-grid</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Mon, 20 Jul 2026 07:22:44 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!ZsJ-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb491c5c8-ca3f-4e26-92ac-392af4dc7654_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>20.07.2026</h1><p><a href="https://leetcode.com/problems/shift-2d-grid/solutions/8408688/kotlin-rust-by-samoylenkodmitry-zipm/">1260. Shift 2D Grid</a> easy</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZsJ-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb491c5c8-ca3f-4e26-92ac-392af4dc7654_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZsJ-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb491c5c8-ca3f-4e26-92ac-392af4dc7654_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!ZsJ-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb491c5c8-ca3f-4e26-92ac-392af4dc7654_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!ZsJ-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb491c5c8-ca3f-4e26-92ac-392af4dc7654_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!ZsJ-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb491c5c8-ca3f-4e26-92ac-392af4dc7654_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZsJ-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb491c5c8-ca3f-4e26-92ac-392af4dc7654_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b491c5c8-ca3f-4e26-92ac-392af4dc7654_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;20.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="20.07.2026" title="20.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!ZsJ-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb491c5c8-ca3f-4e26-92ac-392af4dc7654_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!ZsJ-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb491c5c8-ca3f-4e26-92ac-392af4dc7654_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!ZsJ-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb491c5c8-ca3f-4e26-92ac-392af4dc7654_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!ZsJ-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb491c5c8-ca3f-4e26-92ac-392af4dc7654_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-R-CR5XVl50I" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;R-CR5XVl50I&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/R-CR5XVl50I?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1426</p><h4>Problem TLDR</h4><p>Shift right 2d grid</p><h4>Intuition</h4><p>Flatten, rotate, chunk.</p><h4>Approach</h4><p>* don&#8217;t forget % size</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(n)</p></li><li><p><strong>Space complexity:</strong> O(n)</p></li></ul><h4>Code</h4><pre><code><code>    fun shiftGrid(g: Array&lt;IntArray&gt;, k: Int) = g.flatMap{it.asList()}
    .run{(takeLast(k%size) + dropLast(k%size)).chunked(g[0].size)}</code></code></pre><pre><code><code>    pub fn shift_grid(g: Vec&lt;Vec&lt;i32&gt;&gt;, k: i32) -&gt; Vec&lt;Vec&lt;i32&gt;&gt; {
        let mut v = g.concat(); let k = k as usize % v.len(); v.rotate_right(k);
        v.chunks(g[0].len()).map(|c|c.to_vec()).collect()
    }</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 19.07.2026 [1081. Smallest Subsequence of Distinct Characters]]]></title><description><![CDATA[Smallest uniq-chars subsequence]]></description><link>https://dmitriisamoilenko.substack.com/p/19072026-1081-smallest-subsequence</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/19072026-1081-smallest-subsequence</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Sun, 19 Jul 2026 09:29:08 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!PpeL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6583c0f9-c169-4950-83d5-4b78ef3ee437_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>19.07.2026</h1><p><a href="https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/solutions/8407086/kotlin-rust-by-samoylenkodmitry-n5q1/">1081. Smallest Subsequence of Distinct Characters</a> medium</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PpeL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6583c0f9-c169-4950-83d5-4b78ef3ee437_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PpeL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6583c0f9-c169-4950-83d5-4b78ef3ee437_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!PpeL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6583c0f9-c169-4950-83d5-4b78ef3ee437_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!PpeL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6583c0f9-c169-4950-83d5-4b78ef3ee437_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!PpeL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6583c0f9-c169-4950-83d5-4b78ef3ee437_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PpeL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6583c0f9-c169-4950-83d5-4b78ef3ee437_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6583c0f9-c169-4950-83d5-4b78ef3ee437_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;19.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="19.07.2026" title="19.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!PpeL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6583c0f9-c169-4950-83d5-4b78ef3ee437_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!PpeL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6583c0f9-c169-4950-83d5-4b78ef3ee437_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!PpeL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6583c0f9-c169-4950-83d5-4b78ef3ee437_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!PpeL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6583c0f9-c169-4950-83d5-4b78ef3ee437_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-NoN0DU7Wk1U" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;NoN0DU7Wk1U&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/NoN0DU7Wk1U?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1425</p><h4>Problem TLDR</h4><p>Smallest uniq-chars subsequence</p><h4>Intuition</h4><p>Didn&#8217;t solve. Stack-based algorightm was a little bit unexpected for me.<br>```j<br>// 46 minute TLE, n^3 algo, full search dp <br>// 26^26 = 6*10^36<br>// basically i give up at 1:28<br>// the solution is stack-based<br>// remove from stack if we find a better variant and stack peek <br>// can be found forward<br>```<br>Greedily take every char. <br>If current is better than the last taken, consider popping the last if there is the same in the suffix.</p><h4>Approach</h4><p>* hashset is not necessary, the result is less than 26</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(n)</p></li><li><p><strong>Space complexity:</strong> O(1)</p></li></ul><h4>Code</h4><pre><code><code>    fun smallestSubsequence(s: String) = buildString {
        val lastPos = IntArray(26); for (i in s.indices) lastPos[s[i]-'a'] = i
        for (i in s.indices) if (s[i] !in this) {
            while (length &gt; 0 &amp;&amp; last() &gt; s[i] &amp;&amp; lastPos[last()-'a'] &gt; i)
                setLength(lastIndex)
            append(s[i])
        }
    }</code></code></pre><pre><code><code>    pub fn smallest_subsequence(s: String) -&gt; String {
        let (mut last, mut res) = ([0; 128], String::new());
        for (i, b) in s.bytes().enumerate() { last[b as usize] = i; }
        for (i, b) in s.bytes().enumerate() { if !res.contains(b as char) {
            while res.bytes().last().map_or(false, |l| l &gt; b &amp;&amp; last[l as usize] &gt; i) {
                res.pop();
            }
            res.push(b as char);
        }} res
    }</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 18.07.2026 [1979. Find Greatest Common Divisor of Array]]]></title><description><![CDATA[GCD of min & max]]></description><link>https://dmitriisamoilenko.substack.com/p/18072026-1979-find-greatest-common</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/18072026-1979-find-greatest-common</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Sat, 18 Jul 2026 07:23:07 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!33Tu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f45260e-dbfa-4436-8b7d-e9dab85df586_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>18.07.2026</h1><p><a href="https://leetcode.com/problems/find-greatest-common-divisor-of-array/solutions/8404572/kotlin-rust-by-samoylenkodmitry-nj6r/">1979. Find Greatest Common Divisor of Array</a> easy</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!33Tu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f45260e-dbfa-4436-8b7d-e9dab85df586_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!33Tu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f45260e-dbfa-4436-8b7d-e9dab85df586_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!33Tu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f45260e-dbfa-4436-8b7d-e9dab85df586_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!33Tu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f45260e-dbfa-4436-8b7d-e9dab85df586_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!33Tu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f45260e-dbfa-4436-8b7d-e9dab85df586_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!33Tu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f45260e-dbfa-4436-8b7d-e9dab85df586_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4f45260e-dbfa-4436-8b7d-e9dab85df586_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;18.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="18.07.2026" title="18.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!33Tu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f45260e-dbfa-4436-8b7d-e9dab85df586_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!33Tu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f45260e-dbfa-4436-8b7d-e9dab85df586_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!33Tu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f45260e-dbfa-4436-8b7d-e9dab85df586_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!33Tu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f45260e-dbfa-4436-8b7d-e9dab85df586_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-ovJUXzzYnvw" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;ovJUXzzYnvw&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/ovJUXzzYnvw?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1424</p><h4>Problem TLDR</h4><p>GCD of min &amp; max</p><h4>Intuition</h4><p>Iterate and find the min, max, then the gcd.</p><h4>Approach</h4><p>* remember gcd: ab bab means a/b b should not be 0, then the recursive call of b,a%b<br>* Rust: sort gives the shortest code; also there is a minmax from itertools<br>* Kotlin: make the entire solution recursive</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(n)</p></li><li><p><strong>Space complexity:</strong> O(1)</p></li></ul><h4>Code</h4><pre><code><code>    fun findGCD(n: IntArray, a:Int=n.max(), b:Int=n.min()):Int=
        if(b==0)a else findGCD(n, b,a%b)</code></code></pre><pre><code><code>    pub fn find_gcd(n: Vec&lt;i32&gt;) -&gt; i32 {
        let (mut b, mut a) = n.into_iter().minmax().into_option().unwrap();
        while b &gt; 0 { (a, b) = (b, a % b) } a
    }</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 17.07.2026 [3312. Sorted GCD Pair Queries]]]></title><description><![CDATA[Queries of position of gcd of every pair in a sorted order]]></description><link>https://dmitriisamoilenko.substack.com/p/17072026-3312-sorted-gcd-pair-queries</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/17072026-3312-sorted-gcd-pair-queries</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Fri, 17 Jul 2026 08:32:59 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!OMbj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27e1f493-d23e-468e-b914-35aacc48f873_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>17.07.2026</h1><p><a href="https://leetcode.com/problems/sorted-gcd-pair-queries/solutions/8402768/kotlin-rust-by-samoylenkodmitry-gib2/">3312. Sorted GCD Pair Queries</a> hard</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!OMbj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27e1f493-d23e-468e-b914-35aacc48f873_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!OMbj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27e1f493-d23e-468e-b914-35aacc48f873_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!OMbj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27e1f493-d23e-468e-b914-35aacc48f873_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!OMbj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27e1f493-d23e-468e-b914-35aacc48f873_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!OMbj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27e1f493-d23e-468e-b914-35aacc48f873_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!OMbj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27e1f493-d23e-468e-b914-35aacc48f873_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/27e1f493-d23e-468e-b914-35aacc48f873_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;17.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="17.07.2026" title="17.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!OMbj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27e1f493-d23e-468e-b914-35aacc48f873_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!OMbj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27e1f493-d23e-468e-b914-35aacc48f873_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!OMbj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27e1f493-d23e-468e-b914-35aacc48f873_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!OMbj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27e1f493-d23e-468e-b914-35aacc48f873_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-oB0oVptzR34" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;oB0oVptzR34&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/oB0oVptzR34?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1423</p><h4>Problem TLDR</h4><p>Queries of position of gcd of every pair in a sorted order</p><h4>Intuition</h4><p>Didn&#8217;t solved<br>```j<br>// 5:30 - have no idea<br>// dp[i] = how many gcds are up to i<br>// sort numbers, 1 2 4 4<br>// i ?<br>// 8: 00 look for hints: number of pair that have some gcd=g<br>// again, no idea how<br>// inclusion-exclusion: ?<br>// 11:38 gave up<br>```<br>Count number of gcds by computing for each gcd how many multipliers we have. Precompute the multipliers with frequency.<br>Subtract overcounted results of gcd[2*a],[3*a] and so on.<br>Binary search in a prefix sum of gcds count.</p><h4>Approach</h4><p>* computing multipliers in a forward way to find all gcds count is a clever idea</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(n)</p></li><li><p><strong>Space complexity:</strong> O(n)</p></li></ul><h4>Code</h4><pre><code><code>    fun gcdValues(n: IntArray, q: LongArray) = run {
        val m = n.max(); val c = IntArray(m+1); for (x in n) ++c[x]; val g = LongArray(m+1)
        for (a in m downTo 1) g[a] = (a..m step a)
            .sumOf{c[it]}.let { 1L*it*(it-1)/2}-(a*2..m step a).sumOf {g[it]}
        for (i in 1..m) g[i] += g[i - 1]
        q.map { v -&gt; g.asList().binarySearch { if (it &lt;= v) -1 else 1 }.inv() }
    }</code></code></pre><pre><code><code>    pub fn gcd_values(n: Vec&lt;i32&gt;, q: Vec&lt;i64&gt;) -&gt; Vec&lt;i32&gt; {
        let m = *n.iter().max().unwrap() as usize; let mut f = vec![0u64; m + 1]; 
        let mut g = f.clone(); for n in n { f[n as usize] += 1 };
        for i in (1..=m).rev() {
            let k: u64 = (i..=m).step_by(i).map(|j| f[j]).sum();
            g[i] = k * (k - 1) / 2 - (i * 2..=m).step_by(i).map(|j| g[j]).sum::&lt;u64&gt;();
        }
        for i in 1..=m { g[i] += g[i - 1] }
        q.iter().map(|&amp;x| g.partition_point(|&amp;v| v &lt;= x as u64) as i32).collect()
    }</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 16.07.2026 [3867. Sum of GCD of Formed Pairs]]]></title><description><![CDATA[Sum of gcd(min,max) of gcd(x, prefix max)]]></description><link>https://dmitriisamoilenko.substack.com/p/16072026-3867-sum-of-gcd-of-formed</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/16072026-3867-sum-of-gcd-of-formed</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Thu, 16 Jul 2026 05:05:55 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!yHNF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F813f5086-ad50-4b50-aa05-ad1a87e42501_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>16.07.2026</h1><p><a href="https://leetcode.com/problems/sum-of-gcd-of-formed-pairs/solutions/8400378/kotlin-rust-by-samoylenkodmitry-1v69/">3867. Sum of GCD of Formed Pairs</a> medium</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yHNF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F813f5086-ad50-4b50-aa05-ad1a87e42501_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yHNF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F813f5086-ad50-4b50-aa05-ad1a87e42501_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!yHNF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F813f5086-ad50-4b50-aa05-ad1a87e42501_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!yHNF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F813f5086-ad50-4b50-aa05-ad1a87e42501_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!yHNF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F813f5086-ad50-4b50-aa05-ad1a87e42501_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yHNF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F813f5086-ad50-4b50-aa05-ad1a87e42501_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/813f5086-ad50-4b50-aa05-ad1a87e42501_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;16.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="16.07.2026" title="16.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!yHNF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F813f5086-ad50-4b50-aa05-ad1a87e42501_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!yHNF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F813f5086-ad50-4b50-aa05-ad1a87e42501_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!yHNF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F813f5086-ad50-4b50-aa05-ad1a87e42501_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!yHNF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F813f5086-ad50-4b50-aa05-ad1a87e42501_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-6WixMY2VDxs" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;6WixMY2VDxs&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/6WixMY2VDxs?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1422</p><h4>Problem TLDR</h4><p>Sum of gcd(min,max) of gcd(x, prefix max)</p><h4>Intuition</h4><p>The entire algorithm is give, just implement.</p><h4>Approach</h4><p>* remember gcd as `a/b, bab`</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(n)</p></li><li><p><strong>Space complexity:</strong> O(n)</p></li></ul><h4>Code</h4><pre><code><code>    fun gcdSum(n: IntArray) = run {
        var max = 0; fun gcd(a: Int, b: Int): Int = if (b==0)a else gcd(b,a%b)
        val g = n.map { max=max(max,it);gcd(max, it) }.sorted()
        (0..&lt;g.size/2).sumOf { i -&gt; 1L*gcd(g[i],g[g.size-1-i]) }
    }</code></code></pre><pre><code><code>    pub fn gcd_sum(n: Vec&lt;i32&gt;) -&gt; i64 {
        let g = |mut a, mut b| { while b != 0 { (a, b) = (b, a % b); } a };
        let mut m = 0;
        let s: Vec&lt;_&gt; = n.iter().map(|&amp;x| { m = m.max(x); g(m, x) }).sorted().collect();
        (0..s.len() / 2).map(|i| g(s[i], s[s.len() - 1 - i]) as i64).sum()
    }</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 15.07.2026 [3658. GCD of Odd and Even Sums]]]></title><description><![CDATA[Gcd of odds and evens]]></description><link>https://dmitriisamoilenko.substack.com/p/15072026-3658-gcd-of-odd-and-even</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/15072026-3658-gcd-of-odd-and-even</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Wed, 15 Jul 2026 06:51:07 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!jfJ5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8dd4e47e-39fc-4ea6-ad8f-9277dcdc1386_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>15.07.2026</h1><p><a href="https://leetcode.com/problems/gcd-of-odd-and-even-sums/solutions/8398308/kotlin-rust-by-samoylenkodmitry-88sc/">3658. GCD of Odd and Even Sums</a> easy</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!jfJ5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8dd4e47e-39fc-4ea6-ad8f-9277dcdc1386_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!jfJ5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8dd4e47e-39fc-4ea6-ad8f-9277dcdc1386_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!jfJ5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8dd4e47e-39fc-4ea6-ad8f-9277dcdc1386_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!jfJ5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8dd4e47e-39fc-4ea6-ad8f-9277dcdc1386_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!jfJ5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8dd4e47e-39fc-4ea6-ad8f-9277dcdc1386_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!jfJ5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8dd4e47e-39fc-4ea6-ad8f-9277dcdc1386_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8dd4e47e-39fc-4ea6-ad8f-9277dcdc1386_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;15.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="15.07.2026" title="15.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!jfJ5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8dd4e47e-39fc-4ea6-ad8f-9277dcdc1386_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!jfJ5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8dd4e47e-39fc-4ea6-ad8f-9277dcdc1386_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!jfJ5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8dd4e47e-39fc-4ea6-ad8f-9277dcdc1386_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!jfJ5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8dd4e47e-39fc-4ea6-ad8f-9277dcdc1386_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-7ksMlk5woyU" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;7ksMlk5woyU&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/7ksMlk5woyU?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1421</p><h4>Problem TLDR</h4><p>Gcd of odds and evens</p><h4>Intuition</h4><p>Calculate sums, calculate gcd.<br>Or.. return `n`: sum of odds is n^2, sum of evens is n(n+1).</p><h4>Approach</h4><p>* remember gcd as `a/b, bab`</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(n)</p></li><li><p><strong>Space complexity:</strong> O(1)</p></li></ul><h4>Code</h4><pre><code><code>    fun gcdOfOddEvenSums(n: Int) = run{
        fun gcd(a: Int, b: Int): Int = if (b==0)a else gcd(b,a%b)
        gcd((1..2*n step 2).sum(), (2..2*n step 2).sum())
    }</code></code></pre><pre><code><code>    pub fn gcd_of_odd_even_sums(n: i32) -&gt; i32 { n }</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 14.07.2026 [3336. Find the Number of Subsequences With Equal GCD]]]></title><description><![CDATA[Equal gcd subsequencies]]></description><link>https://dmitriisamoilenko.substack.com/p/14072026-3336-find-the-number-of</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/14072026-3336-find-the-number-of</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Tue, 14 Jul 2026 08:19:23 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!TnMZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85936ee6-0934-4df8-a782-cff2ec6addd9_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>14.07.2026</h1><p><a href="https://leetcode.com/problems/find-the-number-of-subsequences-with-equal-gcd/solutions/8396354/kotlin-rust-by-samoylenkodmitry-sagu/">3336. Find the Number of Subsequences With Equal GCD</a> hard</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!TnMZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85936ee6-0934-4df8-a782-cff2ec6addd9_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TnMZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85936ee6-0934-4df8-a782-cff2ec6addd9_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!TnMZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85936ee6-0934-4df8-a782-cff2ec6addd9_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!TnMZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85936ee6-0934-4df8-a782-cff2ec6addd9_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!TnMZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85936ee6-0934-4df8-a782-cff2ec6addd9_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!TnMZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85936ee6-0934-4df8-a782-cff2ec6addd9_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/85936ee6-0934-4df8-a782-cff2ec6addd9_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;14.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="14.07.2026" title="14.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!TnMZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85936ee6-0934-4df8-a782-cff2ec6addd9_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!TnMZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85936ee6-0934-4df8-a782-cff2ec6addd9_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!TnMZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85936ee6-0934-4df8-a782-cff2ec6addd9_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!TnMZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85936ee6-0934-4df8-a782-cff2ec6addd9_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-LnMMjFhXwfg" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;LnMMjFhXwfg&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/LnMMjFhXwfg?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><h4>Problem TLDR</h4><p>Equal gcd subsequencies</p><h4>Intuition</h4><p>```j<br>// 200 length and 200 max<br>// the gcd can be fixed?<br>// 6 minute: i have no idea, go for hints: dp[i][gcd1][gcd2]<br>// 21 minute: wrong answer 615/622 test case<br>```<br>Used the hint.<br>* dp [i] [gcd1] [gcd2] take to the first or take to the second or skip</p><h4>Approach</h4><p>* the bottom up: flatten the gcd1xgcd2 table, use only the previous table, result is a diagonal sum</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(n^3)</p></li><li><p><strong>Space complexity:</strong> O(n^2)</p></li></ul><h4>Code</h4><pre><code><code>    fun subsequencePairCount(n: IntArray): Long {
        val dp = HashMap&lt;Int, Long&gt;(); val M = 1000000007L
        fun gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
        fun f(i: Int, x: Int, y: Int): Long = if (i == n.size)
            if (x == y) 1L else 0L else dp.getOrPut(i*40401+x*201+y) {
            (f(i+1, gcd(n[i],x), y) + f(i+1, x, gcd(n[i],y)) + f(i+1, x, y)) % M }
        return (f(0, 0, 0) - 1 + M) % M
    }</code></code></pre><pre><code><code>    pub fn subsequence_pair_count(n: Vec&lt;i32&gt;) -&gt; i32 {
        let (mut dp, M) = ([0i64; 201*201], 1_000_000_007); dp[0] = 1;
        let g = |mut a: usize, mut b: usize| { while b &gt; 0 {(a,b)=(b,a%b)} a };
        for v in n { let mut nxt = dp;
            for i in 0..201*201 { if dp[i] &gt; 0 {
                let g1 = g(i/201, v as usize) * 201 + i%201;
                let g2 = i/201 * 201 + g(i%201, v as usize);
                nxt[g1] = (nxt[g1] + dp[i]) % M;
                nxt[g2] = (nxt[g2] + dp[i]) % M
            }} dp = nxt }
        ((1..201).map(|i| dp[i * 201 + i]).sum::&lt;i64&gt;() % M) as i32
    }</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 13.07.2026 [1291. Sequential Digits]]]></title><description><![CDATA[Increasing digit numbers in a range]]></description><link>https://dmitriisamoilenko.substack.com/p/13072026-1291-sequential-digits</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/13072026-1291-sequential-digits</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Mon, 13 Jul 2026 07:21:05 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!Kke5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79d622c0-c8fb-47ed-9b32-aeb66761c3e7_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>13.07.2026</h1><p><a href="https://leetcode.com/problems/sequential-digits/solutions/8393935/kotlin-rust-by-samoylenkodmitry-2p1q/">1291. Sequential Digits</a> medium</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Kke5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79d622c0-c8fb-47ed-9b32-aeb66761c3e7_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Kke5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79d622c0-c8fb-47ed-9b32-aeb66761c3e7_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!Kke5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79d622c0-c8fb-47ed-9b32-aeb66761c3e7_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!Kke5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79d622c0-c8fb-47ed-9b32-aeb66761c3e7_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!Kke5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79d622c0-c8fb-47ed-9b32-aeb66761c3e7_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Kke5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79d622c0-c8fb-47ed-9b32-aeb66761c3e7_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/79d622c0-c8fb-47ed-9b32-aeb66761c3e7_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;13.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="13.07.2026" title="13.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!Kke5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79d622c0-c8fb-47ed-9b32-aeb66761c3e7_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!Kke5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79d622c0-c8fb-47ed-9b32-aeb66761c3e7_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!Kke5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79d622c0-c8fb-47ed-9b32-aeb66761c3e7_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!Kke5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79d622c0-c8fb-47ed-9b32-aeb66761c3e7_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-E7T4fvPS9Z8" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;E7T4fvPS9Z8&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/E7T4fvPS9Z8?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1419</p><h4>Problem TLDR</h4><p>Increasing digit numbers in a range</p><h4>Intuition</h4><p>The possible set of numbers is very small. Generate all then filter and sort.</p><h4>Approach</h4><p>* iterate over lengths, sliding window over string in an inner loop</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(log^2(n))</p></li><li><p><strong>Space complexity:</strong> O(log^2(n))</p></li></ul><h4>Code</h4><pre><code><code>    fun sequentialDigits(l: Int, h: Int) = 
    (2..9).flatMap{"123456789".windowed(it)}.map{it.toInt()}.filter{it in l..h}</code></code></pre><pre><code><code>    pub fn sequential_digits(l: i32, h: i32) -&gt; Vec&lt;i32&gt; {
        (2..10).flat_map(|w|(1..11-w).map(move |x| (x..x+w).fold(0,|r,t|r*10+t)))
               .filter(|&amp;x|l&lt;=x&amp;&amp;x&lt;=h).collect()
    }</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 12.07.2026 [1331. Rank Transform of an Array]]]></title><description><![CDATA[Rank of items in an array]]></description><link>https://dmitriisamoilenko.substack.com/p/12072026-1331-rank-transform-of-an</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/12072026-1331-rank-transform-of-an</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Sun, 12 Jul 2026 09:02:02 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!YIif!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23c2cee0-e889-4dbf-a5e1-9dbda754ae39_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>12.07.2026</h1><p><a href="https://leetcode.com/problems/rank-transform-of-an-array/solutions/8391969/kotlin-rust-by-samoylenkodmitry-zbz7/">1331. Rank Transform of an Array</a> easy</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!YIif!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23c2cee0-e889-4dbf-a5e1-9dbda754ae39_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!YIif!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23c2cee0-e889-4dbf-a5e1-9dbda754ae39_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!YIif!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23c2cee0-e889-4dbf-a5e1-9dbda754ae39_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!YIif!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23c2cee0-e889-4dbf-a5e1-9dbda754ae39_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!YIif!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23c2cee0-e889-4dbf-a5e1-9dbda754ae39_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!YIif!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23c2cee0-e889-4dbf-a5e1-9dbda754ae39_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/23c2cee0-e889-4dbf-a5e1-9dbda754ae39_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;12.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="12.07.2026" title="12.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!YIif!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23c2cee0-e889-4dbf-a5e1-9dbda754ae39_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!YIif!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23c2cee0-e889-4dbf-a5e1-9dbda754ae39_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!YIif!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23c2cee0-e889-4dbf-a5e1-9dbda754ae39_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!YIif!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23c2cee0-e889-4dbf-a5e1-9dbda754ae39_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-8yd87OStcow" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;8yd87OStcow&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/8yd87OStcow?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1418</p><h4>Problem TLDR</h4><p>Rank of items in an array</p><h4>Intuition</h4><p>Sort. Dedup. Binary search each number.</p><h4>Approach</h4><p>* we can use toSortedMap().run { ... headSet(it).size } but leetcode give TLE, still O(nlogn)<br>* Rust: use itertools and collect tuples to the hashmap</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(nlogn)</p></li><li><p><strong>Space complexity:</strong> O(n)</p></li></ul><h4>Code</h4><pre><code><code>    fun arrayRankTransform(a: IntArray) =
    a.toSet().sorted().run { a.map { binarySearch(it) + 1 } }</code></code></pre><pre><code><code>6</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 11.07.2026 [2685. Count the Number of Complete Components]]]></title><description><![CDATA[Fully connected components]]></description><link>https://dmitriisamoilenko.substack.com/p/11072026-2685-count-the-number-of</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/11072026-2685-count-the-number-of</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Sat, 11 Jul 2026 12:20:23 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!Gcwc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1528b9f-8276-45fd-84b8-6837d67c63e4_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>11.07.2026</h1><p><a href="https://leetcode.com/problems/count-the-number-of-complete-components/solutions/8390139/kotlin-rust-by-samoylenkodmitry-lwl3/">2685. Count the Number of Complete Components</a> medium</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Gcwc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1528b9f-8276-45fd-84b8-6837d67c63e4_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Gcwc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1528b9f-8276-45fd-84b8-6837d67c63e4_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!Gcwc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1528b9f-8276-45fd-84b8-6837d67c63e4_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!Gcwc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1528b9f-8276-45fd-84b8-6837d67c63e4_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!Gcwc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1528b9f-8276-45fd-84b8-6837d67c63e4_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Gcwc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1528b9f-8276-45fd-84b8-6837d67c63e4_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b1528b9f-8276-45fd-84b8-6837d67c63e4_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;11.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="11.07.2026" title="11.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!Gcwc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1528b9f-8276-45fd-84b8-6837d67c63e4_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!Gcwc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1528b9f-8276-45fd-84b8-6837d67c63e4_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!Gcwc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1528b9f-8276-45fd-84b8-6837d67c63e4_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!Gcwc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1528b9f-8276-45fd-84b8-6837d67c63e4_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-pmytBsVvW-A" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;pmytBsVvW-A&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/pmytBsVvW-A?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1417</p><h4>Problem TLDR</h4><p>Fully connected components</p><h4>Intuition</h4><p>1. Union-Find to find connected components<br>2. Count incoming edges for each node<br>3. Fully connected group size is the number of edges for each node +1</p><h4>Approach</h4><p>* 50 elements can fit into long variable<br>* adjacency matrix would look the same for each group</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(n)</p></li><li><p><strong>Space complexity:</strong> O(n)</p></li></ul><h4>Code</h4><pre><code><code>    fun countCompleteComponents(n: Int, e: Array&lt;IntArray&gt;) = e
        .fold(LongArray(n){1L shl it}){ m, (a, b) -&gt; m[a] += 1L shl b; m[b] += 1L shl a; m }
        .groupBy { it }.count { (k, v) -&gt; v.size == k.countOneBits() }</code></code></pre><pre><code><code>6</code></code></pre>]]></content:encoded></item><item><title><![CDATA[# 10.07.2026 [3534. Path Existence Queries in a Graph II]]]></title><description><![CDATA[Queries of connected nodes shortest distances]]></description><link>https://dmitriisamoilenko.substack.com/p/10072026-3534-path-existence-queries</link><guid isPermaLink="false">https://dmitriisamoilenko.substack.com/p/10072026-3534-path-existence-queries</guid><dc:creator><![CDATA[DMITRII SAMOILENKO]]></dc:creator><pubDate>Fri, 10 Jul 2026 09:59:19 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!ipCx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe501e512-58ac-4128-bb0e-42fdd260b85d_1600x900.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>10.07.2026</h1><p><a href="https://leetcode.com/problems/path-existence-queries-in-a-graph-ii/solutions/8388003/kotlin-by-samoylenkodmitry-7zxo/">3534. Path Existence Queries in a Graph II</a> hard</p><p><a href="https://dmitrysamoylenko.com/leetcode/">https://dmitrysamoylenko.com/leetcode/</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ipCx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe501e512-58ac-4128-bb0e-42fdd260b85d_1600x900.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ipCx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe501e512-58ac-4128-bb0e-42fdd260b85d_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!ipCx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe501e512-58ac-4128-bb0e-42fdd260b85d_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!ipCx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe501e512-58ac-4128-bb0e-42fdd260b85d_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!ipCx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe501e512-58ac-4128-bb0e-42fdd260b85d_1600x900.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ipCx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe501e512-58ac-4128-bb0e-42fdd260b85d_1600x900.webp" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e501e512-58ac-4128-bb0e-42fdd260b85d_1600x900.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;10.07.2026&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="10.07.2026" title="10.07.2026" srcset="https://substackcdn.com/image/fetch/$s_!ipCx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe501e512-58ac-4128-bb0e-42fdd260b85d_1600x900.webp 424w, https://substackcdn.com/image/fetch/$s_!ipCx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe501e512-58ac-4128-bb0e-42fdd260b85d_1600x900.webp 848w, https://substackcdn.com/image/fetch/$s_!ipCx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe501e512-58ac-4128-bb0e-42fdd260b85d_1600x900.webp 1272w, https://substackcdn.com/image/fetch/$s_!ipCx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe501e512-58ac-4128-bb0e-42fdd260b85d_1600x900.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div id="youtube2-tJYqTupC-3s" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;tJYqTupC-3s&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/tJYqTupC-3s?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>Join me on Telegram</h4><p>https://t.me/leetcode_daily_unstoppable/1416</p><h4>Problem TLDR</h4><p>Queries of connected nodes shortest distances</p><h4>Intuition</h4><p>Didn&#8217;t solved.<br>```j<br>// 1 1 1 1 1 1 1 1 1 1 1 from every node to every node dist = 1<br>// 1 2 . 4 5 6 7 8 9 md=2<br>// * * . <br>// 1 1 .<br>// * . *<br>// 2 1 . 0<br>// . * *<br>// 2 2 . 1 0<br>// . * * *<br>// 2 2 . 1 1 0<br>// 3 2 . 2 1 1 0<br>// 3 3 . 2 2 1 1 0<br>// 3 3 . 2 2 2 1 1 0<br>// 4 3 . 3 2 2 2 1 1 0<br>// ^<br>// can be removed, distances stay the same<br>// so the distance is (a-b)/md <br><br>// 1 hr mark: wrong answer: 91, 92, 127, 173, 179, 182 md=51, 91-182 my 2, correct 3<br>// so this simple formula doesnt work<br>// 182-91=91; 91/51 = 2<br>// but 91+51 = 142, so we go to 127<br>// 127+51 = 178, we have to go to 173<br>// 173+51=200+ we arrive at 182 at 3 steps<br>// that means for each number we should track next reachable<br>// or do dp[current_position][steps_required]=reachable_position<br>// but this is O(n^2), so let&#8217;s give up<br>// hints: binary jumping (?)<br><br>```<br>* sort the numbers<br>* use sliding window to find the rightmost jump for every cell: left goest +1, right goes until diff is bigger than max<br>* prepare binary lifting jump table: u[k][x] = u[k-1][Y] where Y = u[k-1][x]; for each x we prepare all 2^k (0..31) jumps by reusing previous; <br>* in query: find the left &#8216;c&#8217; pointer and the right &#8216;t&#8217; pointer positions<br>* by moving the left &#8216;c&#8217; pointer with jump table count the jumps st += 2^k if it is not overshoot the right &#8216;t&#8217; pointer u[k][c]&lt;t</p><h4>Approach</h4><p>* learn the binary lifting</p><h4>Complexity</h4><ul><li><p><strong>Time complexity:</strong> O(nlogn)</p></li><li><p><strong>Space complexity:</strong> O(n)</p></li></ul><h4>Code</h4><pre><code><code>    fun pathExistenceQueries(n: Int, ns: IntArray, md: Int, qs: Array&lt;IntArray&gt;)=run{
        val s = ns.sorted(); val u = Array(18){IntArray(n)}; var r = 0
        for (l in 0..&lt;n) { while (r+1&lt;n &amp;&amp; s[r+1]-s[l]&lt;=md)++r; u[0][l]=r}
        for (k in 1..17) for (x in 0..&lt;n) u[k][x] = u[k-1][u[k-1][x]]
        qs.map {(a,b) -&gt; if (a==b)return@map 0
            var c = s.binarySearch(min(ns[a],ns[b])); val t = s.binarySearch(max(ns[a],ns[b]))
            if (u[17][c]&lt;t) return@map -1; var st = 1
            for (k in 17 downTo 0) if (u[k][c]&lt;t) {c = u[k][c]; st += 1 shl k }; st
        }
    }</code></code></pre><pre><code></code></pre>]]></content:encoded></item></channel></rss>