<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Muhamad Taufik Satya</title>
    <description>The latest articles on DEV Community by Muhamad Taufik Satya (@taufiksty).</description>
    <link>https://dev.to/taufiksty</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F963030%2Fbdb36b31-1d9b-4573-a78b-326ca93b637e.png</url>
      <title>DEV Community: Muhamad Taufik Satya</title>
      <link>https://dev.to/taufiksty</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/taufiksty"/>
    <language>en</language>
    <item>
      <title>Apply Operations to an Array</title>
      <dc:creator>Muhamad Taufik Satya</dc:creator>
      <pubDate>Sat, 01 Mar 2025 10:33:00 +0000</pubDate>
      <link>https://dev.to/taufiksty/apply-operations-to-an-array-4g55</link>
      <guid>https://dev.to/taufiksty/apply-operations-to-an-array-4g55</guid>
      <description>&lt;h2&gt;
  
  
  Optimizing Array Operations in TypeScript
&lt;/h2&gt;

&lt;p&gt;Today, I explore a Daily Leetcode problem number &lt;code&gt;2640. Apply Operations to an Array&lt;/code&gt; using TypeScript that modifies an array based on specific rules:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If two consecutive elements are equal, &lt;strong&gt;double&lt;/strong&gt; the first and &lt;strong&gt;set the second to 0&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;After processing the array, move all &lt;strong&gt;zeroes to the end&lt;/strong&gt; while keeping the relative order of other elements.
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/apply-operations-to-an-array/description/" rel="noopener noreferrer"&gt;link to the question&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s break it down step by step.  &lt;/p&gt;




&lt;h2&gt;
  
  
  Understanding the Code
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Function Definition
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;applyOperations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[]):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function takes an array of numbers (&lt;code&gt;nums&lt;/code&gt;) as input.  &lt;/p&gt;




&lt;h3&gt;
  
  
  Step 1: Iterating Through the Array
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use a &lt;code&gt;for&lt;/code&gt; loop to iterate &lt;strong&gt;from index &lt;code&gt;0&lt;/code&gt; to second-last element&lt;/strong&gt; (&lt;code&gt;nums.length - 1&lt;/code&gt;). This ensures that we check &lt;strong&gt;each element and its next neighbor&lt;/strong&gt;.  &lt;/p&gt;




&lt;h3&gt;
  
  
  Step 2: Doubling Consecutive Equal Elements
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If two consecutive elements are &lt;strong&gt;equal&lt;/strong&gt;, we:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Double&lt;/strong&gt; the first element (&lt;code&gt;nums[i] *= 2&lt;/code&gt;)
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Set the second element to &lt;code&gt;0&lt;/code&gt;&lt;/strong&gt; (&lt;code&gt;nums[i + 1] = 0&lt;/code&gt;)
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Example Transformation&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;Step&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;// (2+2 → 4, set next to 0)&lt;/span&gt;
&lt;span class="nx"&gt;Step&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;// (3+3 → 6, set next to 0)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 3: Moving Zeroes to the End
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We use &lt;strong&gt;&lt;code&gt;.filter(Boolean)&lt;/code&gt;&lt;/strong&gt; to remove &lt;code&gt;0&lt;/code&gt;s and keep non-zero values.
&lt;/li&gt;
&lt;li&gt;We use &lt;strong&gt;&lt;code&gt;.filter(num =&amp;gt; !num)&lt;/code&gt;&lt;/strong&gt; to extract all zeroes.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, we &lt;strong&gt;concatenate&lt;/strong&gt; the two arrays to place zeroes at the end.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example Output&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;applyOperations&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="c1"&gt;// Step 1: [4, 0, 6, 0, 4]&lt;/span&gt;
&lt;span class="c1"&gt;// Step 2: Remove zeros -&amp;gt; [4, 6, 4]&lt;/span&gt;
&lt;span class="c1"&gt;// Step 3: Add zeros at the end -&amp;gt; [4, 6, 4, 0, 0]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Final Optimized Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;applyOperations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[]):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Time &amp;amp; Space Complexity Analysis
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Loop through the array → &lt;code&gt;O(n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Filtering &amp;amp; concatenation → &lt;code&gt;O(n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Overall Complexity: &lt;code&gt;O(n)&lt;/code&gt; (linear time complexity)
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This function efficiently:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Modifies the array &lt;strong&gt;in place&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;Moves &lt;strong&gt;all zeroes to the end&lt;/strong&gt; after processing.
&lt;/li&gt;
&lt;li&gt;Runs in &lt;strong&gt;O(n) time complexity&lt;/strong&gt;, making it optimal for large inputs.
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>leetcode</category>
      <category>array</category>
      <category>typescript</category>
      <category>daily</category>
    </item>
    <item>
      <title>Special Array With X Elements Greater Than or Equal X (Go)</title>
      <dc:creator>Muhamad Taufik Satya</dc:creator>
      <pubDate>Mon, 27 May 2024 10:30:39 +0000</pubDate>
      <link>https://dev.to/taufiksty/special-array-with-x-elements-greater-than-or-equal-x-go-3efg</link>
      <guid>https://dev.to/taufiksty/special-array-with-x-elements-greater-than-or-equal-x-go-3efg</guid>
      <description>&lt;p&gt;On May 27 2024, I encountered a daily Leetcode challenge entitled &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Special Array With X Elements Greater Than or Equal X &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This challenge is relatively easy and quite friendly for beginners to practice. Because I'm learning Golang, I implemented it using that language.&lt;/p&gt;

&lt;p&gt;For the question link, you can go to the following &lt;a href="https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x"&gt;page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff7jxkdgjocuweogumpvi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff7jxkdgjocuweogumpvi.png" alt="Question description" width="800" height="171"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjjsv7zhh44j62kj66ek4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjjsv7zhh44j62kj66ek4.png" alt="Example questions" width="800" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;First of all, let's define the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func specialArray(nums []int) int {

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, initiate the loop and associated dependent variables such as x and count into the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func specialArray(nums []int) int {
    for i := 1; i &amp;lt;= len(nums); i++ {
        x := i
        count := 0
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, loop back over the nums array to compare whether each element is greater than x, if yes, add it to the count variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for _, v := range nums {
    if v &amp;gt;= x {
        count++
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, check whether count is the same as x, if it is, immediately return x. However, if not, at the end of the function add return -1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if count == x {
    return x
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Complete code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func specialArray(nums []int) int {
    for i := 1; i &amp;lt;= len(nums); i++ {
        x := i
        count := 0

        for _, v := range nums {
            if v &amp;gt;= x {
                count++
            }
        }

        if count == x {
            return x
        }
    }

    return -1
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>go</category>
      <category>leetcode</category>
    </item>
    <item>
      <title>Calculate Determinant Matrix with Javascript</title>
      <dc:creator>Muhamad Taufik Satya</dc:creator>
      <pubDate>Wed, 16 Aug 2023 06:22:26 +0000</pubDate>
      <link>https://dev.to/taufiksty/calculate-determinant-matrix-using-javascript-2cnc</link>
      <guid>https://dev.to/taufiksty/calculate-determinant-matrix-using-javascript-2cnc</guid>
      <description>&lt;p&gt;Yesterday, I had difficulty doing a kata (challenge in codewars). I started getting used to doing 1 kata 1 day as part of my learning algorithm and data structures.&lt;/p&gt;

&lt;p&gt;I also have a hard time understanding recursion. When I encountered a recursion problem for calculating the determinant matrix, I started trying to analyze it to find a solution.&lt;/p&gt;

&lt;p&gt;Here is the question :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Write a function that accepts a square matrix (&lt;code&gt;N x N&lt;/code&gt; 2D array) and returns the determinant of the matrix.&lt;/p&gt;

&lt;p&gt;How to take the determinant of a matrix -- it is simplest to start with the smallest cases:&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;1x1&lt;/code&gt; matrix &lt;code&gt;|a|&lt;/code&gt; has determinant &lt;code&gt;a&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;2x2&lt;/code&gt; matrix &lt;code&gt;[ [a, b], [c, d] ]&lt;/code&gt; or&lt;/p&gt;


&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|a  b|
|c  d|
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;has determinant: &lt;code&gt;a*d - b*c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The determinant of an n x n sized matrix is calculated by reducing the problem to the calculation of the determinants of n matrices of &lt;code&gt;n-1 x n-1&lt;/code&gt; size.&lt;/p&gt;

&lt;p&gt;For the 3x3 case, [ [a, b, c], [d, e, f], [g, h, i] ] or&lt;/p&gt;


&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|a b c|  
|d e f|  
|g h i|  
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;the determinant is: &lt;code&gt;a * det(a_minor) - b * det(b_minor) + c * det(c_minor)&lt;/code&gt; where &lt;code&gt;det(a_minor)&lt;/code&gt; refers to taking the determinant of the &lt;code&gt;2x2&lt;/code&gt; matrix created by crossing out the row and column in which the element a occurs:&lt;/p&gt;


&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|- - -|
|- e f|
|- h i|  
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Note the alternation of signs.&lt;/p&gt;

&lt;p&gt;The determinant of larger matrices are calculated analogously, e.g. if M is a &lt;code&gt;4x4&lt;/code&gt; matrix with first row &lt;code&gt;[a, b, c, d]&lt;/code&gt;, then:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;det(M) = a * det(a_minor) - b * det(b_minor) + c * det(c_minor) - d * det(d_minor)&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Define the function&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * Calculate determinant on matrix
 * @param m: matrix [[]]
 * @returns determinant: number
 */
function determinant(m) {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this definition we have matrix parameters for determinant function and the desired result is a number&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Know the dimensions of the matrix&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const n = m.length;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because it has been ascertained that the input matrix is always square or has the same N, what we need to know is NxN or what order the input matrix is.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Solve the easy-to-know possibilities first&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (n == 1) return m[0][0];

if (n == 2) {
    return m[0][0] * m[1][1] - m[0][1] * m[1][0];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Based on what has been told in the problem, if the matrix is of the order 1x1, then the result will be the matrix elements themselves. For matrices of order 2x2, we can directly calculate it with &lt;code&gt;a*d - b*c&lt;/code&gt; for &lt;code&gt;[[a, b], [c, d]]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Create a minor function inside determinant function&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function minor(matrix, row, col) {
    return matrix
      .map((r, i) =&amp;gt; r.filter((_, j) =&amp;gt; i !== row &amp;amp;&amp;amp; j !== col))
      .filter((r, i) =&amp;gt; i !== row);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the case of matrices of order greater than 2. In the problem, we are told to use the minor method, which is to make a 2x2 matrix outside of the rows and columns of elements being calculated. To do this we need to map the original matrix and filter out the rows and columns other than the rows and columns of elements calculated in row one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Calculate and return determinant&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let det = 0;

for (let col = 0; col &amp;lt; n; col++) {
    det += m[0][col] * determinant(minor(m, 0, col)) * (col % 2 === 0 ? 1 : -1);
}

return det;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, calculate the determinant using the minor method by calling the minor function for each of the first row elements based on the hint given by the question. &lt;/p&gt;

&lt;p&gt;The full code can be seen as follows :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * Calculate determinant on matrix
 * @param m: matrix [[]]
 * @returns determinant: number
 */
function determinant(m) {
    const n = m.length;

    if (n == 1) return m[0][0];

    if (n == 2) {
        return m[0][0] * m[1][1] - m[0][1] * m[1][0];
    }

    function minor(matrix, row, col) {
        return matrix
            .map((r, i) =&amp;gt; r.filter((_, j) =&amp;gt; i !== row &amp;amp;&amp;amp; j !== col))
            .filter((r, i) =&amp;gt; i !== row);
    }

    let det = 0;

    for (let col = 0; col &amp;lt; n; col++) {
        det += m[0][col] * determinant(minor(m, 0, col)) * (col % 2 === 0 ? 1 : -1);
    }

    return det;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For tests :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Tests */
console.log(determinant([[1]])); // 1
console.log(
    determinant([
        [1, 2],
        [3, 4],
    ]),
); // -2
console.log(
    determinant([
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
    ]),
); // 0
console.log(
    determinant([
        [1, 2, 3, 4],
        [5, 6, 7, 8],
        [9, 10, 11, 12],
        [13, 14, 15, 16],
    ]),
); // 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>recursion</category>
      <category>linearalgebra</category>
      <category>codewars</category>
    </item>
  </channel>
</rss>
