<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xml:lang="en-us" xml:base="https://yosunnyvim.me/">
    <title>CPs - Yosunnyvim</title>
    <subtitle type="html"><![CDATA[Personal website for Mohamed Younis]]></subtitle>
    <link href="/cp/" rel="alternate" type="text/html" title="html" />
    <link href="/cp/index.xml" rel="alternate" type="application/rss+xml" title="rss" />
    <icon>https://yosunnyvim.me/logo_hu_2f64bab10613c3d8.png</icon>
    <logo>https://yosunnyvim.me/logo_hu_2f64bab10613c3d8.png</logo>
    <author>
        <name>Mohamed Younis</name>
        
    </author>
    <generator uri="https://gohugo.io" version="0.167.0">Hugo</generator>
    <updated>2026-09-06T00:00:00Z</updated>
    <id>urn:uuid:7b0a365b-fac6-5d2c-89e1-0fc0b2911802</id>
    <entry>
        
        <category term="cp" />
        
        <link rel="alternate" type="type/html" href="https://yosunnyvim.me/cp/bits/?utm_source=atom_feed" />
        <title type="html"><![CDATA[Bits]]></title>
        
        
        
        <published>2026-07-29T00:00:00Z</published>
        <updated>2026-07-29T00:00:00Z</updated>
        <id>urn:uuid:e73c4936-1d50-5ee2-39f9-1f7e90a199f0</id>
        
        <summary type="html"><![CDATA[<p>How to convert from decimal to any number system</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span>vector<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">long</span> <span style="color:#66d9ef">long</span><span style="color:#f92672">&gt;</span>getRepresentation(<span style="color:#66d9ef">long</span> <span style="color:#66d9ef">long</span>  n, <span style="color:#66d9ef">int</span> base){
</span></span><span style="display:flex;"><span>	vector<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">long</span> <span style="color:#66d9ef">long</span><span style="color:#f92672">&gt;</span>ret;	
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">while</span>(n){
</span></span><span style="display:flex;"><span>		ret.push_back(n<span style="color:#f92672">%</span>base);
</span></span><span style="display:flex;"><span>		n<span style="color:#f92672">/=</span>base;
</span></span><span style="display:flex;"><span>	}	
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">return</span> ret;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h2 id="bitwise-operations">Bitwise operations</h2>
<table>
	<thead>
			<tr>
					<th>Operator</th>
					<th>Description</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>&amp;</td>
					<td>Bitwise AND</td>
			</tr>
			<tr>
					<td>|</td>
					<td>Bitwise OR</td>
			</tr>
			<tr>
					<td>^</td>
					<td>Bitwise XOR</td>
			</tr>
			<tr>
					<td>&raquo;</td>
					<td>Bitwise right shifting</td>
			</tr>
			<tr>
					<td>&laquo;</td>
					<td>Bitwise left shifting</td>
			</tr>
			<tr>
					<td>~</td>
					<td>one&rsquo;s complement</td>
			</tr>
	</tbody>
</table>
<table>
	<thead>
			<tr>
					<th>AND</th>
					<th>gives <code>1</code> if all are <code>1</code></th>
					<th></th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><strong>OR</strong></td>
					<td><strong>gives <code>1</code> if there are atleast <code>1</code></strong></td>
					<td></td>
			</tr>
			<tr>
					<td><strong>XOR</strong></td>
					<td><strong>give one if number of 1s is odd</strong></td>
					<td></td>
			</tr>
			<tr>
					<td><strong>Complement</strong></td>
					<td><strong>flips all bits</strong></td>
					<td></td>
			</tr>
	</tbody>
</table>
<blockquote>
<blockquote>
<p><strong>Left-shifting <code>n</code> by <code>k</code> (<code>n &lt;&lt; k</code>) multiplies <code>n</code> by <code>2^k</code>. Right-shifting <code>n</code> by <code>k</code> (<code>n &gt;&gt; k</code>) divides <code>n</code> by <code>2^k</code> (integer division, remainder dropped).</strong></p>
</blockquote>
</blockquote>]]></summary>
    </entry>
    <entry>
        
        <category term="cp" />
        
        <link rel="alternate" type="type/html" href="https://yosunnyvim.me/cp/binary-search/?utm_source=atom_feed" />
        <title type="html"><![CDATA[Binary Search]]></title>
        
        
        
        <published>2026-07-23T00:00:00Z</published>
        <updated>2026-07-23T00:00:00Z</updated>
        <id>urn:uuid:d061234e-36e6-5bca-496a-b61a283adb09</id>
        
        <summary type="html"><![CDATA[<p>Classic Binary Search</p>
<p>Search for value <code>x</code> in a sorted array.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span><span style="color:#66d9ef">int</span> l <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>, r <span style="color:#f92672">=</span> n <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">bool</span> found <span style="color:#f92672">=</span> false;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">while</span> (l <span style="color:#f92672">&lt;=</span> r) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> m <span style="color:#f92672">=</span> (l <span style="color:#f92672">+</span> r) <span style="color:#f92672">/</span> <span style="color:#ae81ff">2</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> (x <span style="color:#f92672">==</span> v[m]) { found <span style="color:#f92672">=</span> true; <span style="color:#66d9ef">break</span>; }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">else</span> <span style="color:#a6e22e">if</span> (x <span style="color:#f92672">&gt;</span> v[m]) l <span style="color:#f92672">=</span> m <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">else</span> r <span style="color:#f92672">=</span> m <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p><strong>O(log n)</strong> per query.</p>
<h3 id="problems-using-this">Problems using this</h3>
<ul>
<li><code>A. Binary Search</code></li>
</ul>
<hr>
<h2 id="lower-bound-closest-to-the-left">Lower Bound (Closest to the Left)</h2>
<p>Find the <strong>first</strong> index where <code>a[i] &gt;= x</code> (or equivalently, the index where <code>x</code> would be inserted to keep sorted order).</p>]]></summary>
    </entry>
    <entry>
        
        <category term="cp" />
        
        <link rel="alternate" type="type/html" href="https://yosunnyvim.me/cp/two-pointers-and-sliding-window/?utm_source=atom_feed" />
        <title type="html"><![CDATA[Two Pointers & Sliding Window]]></title>
        
        
        
        <published>2026-07-16T00:00:00Z</published>
        <updated>2026-07-16T00:00:00Z</updated>
        <id>urn:uuid:763f9dd7-64af-5900-9997-8e5c1c85ff0d</id>
        
        <summary type="html"><![CDATA[<h2 id="merge-two-sorted-arrays">Merge Two Sorted Arrays</h2>
<p>Given two sorted arrays <code>a</code> and <code>b</code>, merge them into one sorted array.</p>
<p>Two pointers <code>i</code> and <code>j</code> — always pick the smaller front element:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span><span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>, j <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>, k <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">while</span> (i <span style="color:#f92672">&lt;</span> a.size() <span style="color:#f92672">||</span> j <span style="color:#f92672">&lt;</span> b.size()) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> (j <span style="color:#f92672">==</span> b.size() <span style="color:#f92672">||</span> (i <span style="color:#f92672">&lt;</span> a.size() <span style="color:#f92672">&amp;&amp;</span> a[i] <span style="color:#f92672">&lt;</span> b[j])) {
</span></span><span style="display:flex;"><span>        c[k] <span style="color:#f92672">=</span> a[i<span style="color:#f92672">++</span>];
</span></span><span style="display:flex;"><span>    } <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>        c[k] <span style="color:#f92672">=</span> b[j<span style="color:#f92672">++</span>];
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    k<span style="color:#f92672">++</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>Runs in <strong>O(n + m)</strong> — optimal since we must look at every element.</p>]]></summary>
    </entry>
    <entry>
        
        <category term="cp" />
        
        <link rel="alternate" type="type/html" href="https://yosunnyvim.me/cp/number-theory/?utm_source=atom_feed" />
        <title type="html"><![CDATA[number theory]]></title>
        
        
        
        <published>2026-07-08T00:00:00Z</published>
        <updated>2026-07-08T00:00:00Z</updated>
        <id>urn:uuid:77522f20-6e10-5100-a925-5820cd134496</id>
        
        <summary type="html"><![CDATA[<h1 id="number-theory">Number Theory</h1>
<h2 id="divisors">Divisors</h2>
<p>For example the divisors of a number like <code>12</code> (call it <code>n</code>) will be <code>[1, 2, 3, 4, 6, 12]</code>.</p>
<p>If we look at them in pairs:</p>
<pre tabindex="0"><code>[1, 12]
[2, 6]
[3, 4]
</code></pre><p>We could just get the first three <code>[1, 2, 3]</code> — call them <code>x</code> — and <code>[4, 6, 12]</code> is <code>y</code>, so <code>x * y = n</code>. By getting <code>x</code> we could get <code>y</code> because we know <code>n</code>.</p>]]></summary>
    </entry>
    <entry>
        
        <category term="cp" />
        
        <link rel="alternate" type="type/html" href="https://yosunnyvim.me/cp/prefix-sum-diffrence-array/?utm_source=atom_feed" />
        <title type="html"><![CDATA[Prefix Sum & Difference Array]]></title>
        
        
        
        <published>2026-07-01T00:00:00Z</published>
        <updated>2026-07-01T00:00:00Z</updated>
        <id>urn:uuid:5e620021-5cb7-59a0-19cb-3baf7d9f2063</id>
        
        <summary type="html"><![CDATA[<h1 id="prefix-sum--difference-array">Prefix Sum &amp; Difference Array</h1>
<h2 id="1d-prefix-sum">1D Prefix Sum</h2>
<p>Given array <code>a[1..n]</code>, build a prefix array where <code>pref[i] = a[1] + a[2] + ... + a[i]</code>.</p>
<p>Sum of any range <code>[l, r]</code> in <strong>O(1)</strong>:</p>
<pre tabindex="0"><code>sum(l, r) = pref[r] - pref[l - 1]
</code></pre><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span>arr[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>; i <span style="color:#f92672">&lt;=</span> n; i<span style="color:#f92672">++</span>) {
</span></span><span style="display:flex;"><span>    arr[i] <span style="color:#f92672">+=</span> arr[i <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>];
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#75715e">// query [l, r]:
</span></span></span><span style="display:flex;"><span>cout <span style="color:#f92672">&lt;&lt;</span> arr[r] <span style="color:#f92672">-</span> arr[l <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>];
</span></span></code></pre></div><p>This works for answering many range-sum queries after <strong>O(n)</strong> preprocessing.</p>]]></summary>
    </entry>
    <entry>
        
        <category term="cp" />
        
        <link rel="alternate" type="type/html" href="https://yosunnyvim.me/cp/frequancy-array/?utm_source=atom_feed" />
        <title type="html"><![CDATA[Frequency Array]]></title>
        
        
        
        <published>2026-06-20T00:00:00Z</published>
        <updated>2026-06-20T00:00:00Z</updated>
        <id>urn:uuid:593eeb75-2ae9-5b18-1991-0a8b83d32090</id>
        
        <summary type="html"><![CDATA[<h2 id="basic-frequency-count">Basic Frequency Count</h2>
<p>Count how many times each value appears in an array:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span><span style="color:#66d9ef">int</span> freq[n <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>] <span style="color:#f92672">=</span> {};
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>; i <span style="color:#f92672">&lt;</span> n; i<span style="color:#f92672">++</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> x; cin <span style="color:#f92672">&gt;&gt;</span> x;
</span></span><span style="display:flex;"><span>    freq[x]<span style="color:#f92672">++</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p><code>freq[v]</code> = how many times <code>v</code> appears.</p>
<p>This is <strong>O(n)</strong> and works when values are bounded (e.g. <code>1 ≤ x ≤ n</code>).</p>
<h3 id="problems-using-this">Problems using this</h3>
<ul>
<li><code>D. Spy Detected!</code> — find the element that appears once (others appear twice)</li>
</ul>
<hr>
<h2 id="first-element-appearing-k-times">First Element Appearing K Times</h2>
<p>Scan the array while incrementing frequency. Return the first value that reaches frequency <code>k</code>:</p>]]></summary>
    </entry>
</feed>
