<?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>Forem: Abdur Rahman Robin</title>
    <description>The latest articles on Forem by Abdur Rahman Robin (@robin3317).</description>
    <link>https://forem.com/robin3317</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%2F270778%2F989f1bb6-88ef-435c-abaa-262b558c68cc.png</url>
      <title>Forem: Abdur Rahman Robin</title>
      <link>https://forem.com/robin3317</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/robin3317"/>
    <language>en</language>
    <item>
      <title>Swap two variables without using third variable (Bitwise XOR)</title>
      <dc:creator>Abdur Rahman Robin</dc:creator>
      <pubDate>Sat, 05 Aug 2023 00:34:21 +0000</pubDate>
      <link>https://forem.com/robin3317/swap-two-variables-without-using-third-variable-52ji</link>
      <guid>https://forem.com/robin3317/swap-two-variables-without-using-third-variable-52ji</guid>
      <description>&lt;p&gt;There are multiple ways of swapping two variables without using third variable in programming. Using Bitwise XOR(^) is one of them. Suppose you have two variables a and b which values are respectively 50 and 100. And if you perform Bitwise XOR operation three times and assign the value respectively to a, b and a - you will find the swapped values which are a = 100 &amp;amp; b = 50.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1BWTsvy9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7ojn1f4gd3hiu5q83cu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1BWTsvy9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7ojn1f4gd3hiu5q83cu.png" alt="Code Image" width="605" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Things will be more clear if you notice the truth table of Bitwise XOR(^):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;X&lt;/th&gt;
      &lt;th&gt;Y&lt;/th&gt;
      &lt;th&gt;X ^ Y&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Let's do the operations in binary since machine understand only machine code/binary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = 50  (base 10) -&amp;gt; 0110010 (base 2)
b = 100 (base 10) -&amp;gt; 1100100 (base 2)

a =  a ^ b
     0110010 
     1100100
-------------
(XOR)1010110

b =  a ^ b
     1010110
     1100100
-------------
(XOR)0110010 [Equivalent to 50(base 10)]

a =  a ^ b
     1010110
     0110010
-------------
(XOR)1100100 [Equivalent to 100(base 10)]

Finally a = 100 and b = 50
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To know more about Bitwise XOR, you can visit &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_XOR"&gt;the official documentation of MDN&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Median of Two Sorted Arrays</title>
      <dc:creator>Abdur Rahman Robin</dc:creator>
      <pubDate>Sat, 04 Feb 2023 01:35:32 +0000</pubDate>
      <link>https://forem.com/robin3317/median-of-two-sorted-arrays-11lf</link>
      <guid>https://forem.com/robin3317/median-of-two-sorted-arrays-11lf</guid>
      <description>&lt;p&gt;It's a very easy problem if we ignore the time complexity condition. Merge the two arrays, sort it and return the middle element if the merged array length is odd. And if the merged array length is even, add two middle elements and divide it by 2 and return the result.&lt;/p&gt;

&lt;p&gt;But we should maintain the logarithmic time complexity, e.g: O(log(m+n))&lt;/p&gt;

&lt;p&gt;So we should use any algorithm or technique that gives us logarithmic time complexity. And that is none other than Binary Search!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RPgDc0S3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mcv2tbbymd52xhsj26na.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RPgDc0S3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mcv2tbbymd52xhsj26na.png" alt="Image description" width="800" height="882"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZsTCDIuq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wirgwn1w6dyclztsdfd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZsTCDIuq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wirgwn1w6dyclztsdfd.png" alt="Image description" width="800" height="726"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--poDwVGCN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/djpsmtsm00k577j4nwis.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--poDwVGCN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/djpsmtsm00k577j4nwis.png" alt="Image description" width="800" height="830"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fh8Fa3Re--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/px9iztl4qofdf5hyzivl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fh8Fa3Re--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/px9iztl4qofdf5hyzivl.png" alt="Image description" width="800" height="997"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ggqe9BxM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u49t3dmrl6rskmenu6np.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ggqe9BxM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u49t3dmrl6rskmenu6np.png" alt="Image description" width="800" height="637"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var findMedianSortedArrays = function(nums1, nums2) {
    let [a, b] = [nums1, nums2]
    const total = nums1.length + nums2.length
    const half = Math.floor(total/2) 

    if (a.length &amp;gt; b.length) {
        [a, b] = [nums2, nums1] 
        // we always do binary search in the smaller array which gives us time complexity O(log(min(m, n)))
    }

    let [left, right] = [0, a.length - 1]

    while(true) {
        let middleOfA = Math.floor((left + right) / 2)
        let middleOfB = half - middleOfA - 2 

        /*
        In left partition, if middle value is not found - that means middle index is less than 0(zero).
        That's why we are using -Infinity in this case.
        And vice-versa in the right partition.
        */
        let aLeft = a[middleOfA] !== undefined ? a[middleOfA] : -Infinity
        let aRight = a[middleOfA + 1] !== undefined ? a[middleOfA + 1] : Infinity
        let bLeft = b[middleOfB] !== undefined ? b[middleOfB] : -Infinity
        let bRight = b[middleOfB + 1] !== undefined ? b[middleOfB + 1] : Infinity

        if (aLeft &amp;lt;= bRight &amp;amp;&amp;amp; bLeft &amp;lt;= aRight) {
            // that means our partition is correct
            if (total % 2 !== 0) return Math.min(aRight, bRight)
            else return (Math.max(aLeft, bLeft) + Math.min(aRight, bRight)) / 2 || 0.00
        } else if (aLeft &amp;gt; bRight) {
            // so we need to reduce the size of aLeft
            right = middleOfA - 1
        }else { 
            // bLeft &amp;gt; aRight, increase the size of aLeft
            left = middleOfA + 1
        }
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>What does the Array method `reduce` do?</title>
      <dc:creator>Abdur Rahman Robin</dc:creator>
      <pubDate>Mon, 07 Mar 2022 04:26:50 +0000</pubDate>
      <link>https://forem.com/robin3317/what-does-the-array-method-reduce-do-15ej</link>
      <guid>https://forem.com/robin3317/what-does-the-array-method-reduce-do-15ej</guid>
      <description>&lt;p&gt;First of all, the name "reduce" doesn't actually reduce anything. It's a confusional/tricky naming convention you often find in programming. Although for better understanding you can assume - it takes lots of values and reduce them into a single value and return.&lt;/p&gt;

&lt;p&gt;reduce is a higher order function which takes two arguments:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Callback function and&lt;/li&gt;
&lt;li&gt;Initial Value.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And the callback function takes four arguments:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;previousValue,&lt;/li&gt;
&lt;li&gt;currentValue,&lt;/li&gt;
&lt;li&gt;currentIndex,&lt;/li&gt;
&lt;li&gt;array&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;More often you will find the callback function takes only two arguments according to the problem we need to solve, which is Okay.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1, 2, 3].reduce((previousValue, currentValue, currentIndex, array) =&amp;gt; {
  // here the return statement goes...
}, initialValue);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's look at a practical example. Write a program to return the sum of all the elements in an array. Please think in a as-usual way/procedure first, then we will solve the same thing with reduce. Here is the as-usual way/procedure of writing this program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sum(arr) {
  let sum = 0;
  for(let i = 0; i &amp;lt; array.length; i++) {
    sum = sum + arr[i];
  }
  return sum;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, if we call sum with an array it will return the sum of it's all elements. Right?&lt;/p&gt;

&lt;p&gt;Yeah, And we can do the same thing with reduce also. Here is the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1, 2, 3, 4].reduce(function(previousValue, currentValue) {
  let result = previousValue + currentValue;
  return result;
}, 0);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It does the same thing. The reducer walks through the array element, at each step adding the current array value to the result from the previous step (this result is the running sum of all the previous steps) - until there are no more elements to add.(reference: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce"&gt;here&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Here the &lt;code&gt;previousValue&lt;/code&gt; is the as-usual &lt;code&gt;sum&lt;/code&gt; and &lt;code&gt;currentValue&lt;/code&gt; is the as-usual &lt;code&gt;arr[i]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the first iteration, there is no &lt;code&gt;previousValue (return value of the previous calculation)&lt;/code&gt; - right? In this case, &lt;code&gt;initialValue&lt;/code&gt; will be used as &lt;code&gt;previousValue&lt;/code&gt;. If there is no &lt;code&gt;initialValue&lt;/code&gt;, the array element at index 0 is used as the initial value and iteration starts from the next element (index 1 instead of index 0).&lt;/p&gt;

&lt;p&gt;Instead of using extra variable result, you can write the program like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1, 2, 3, 4].reduce(function(previousValue, currentValue) {
  previousValue += currentValue;
  return previousValue;
}, 0);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And more shortly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1, 2, 3, 4].reduce((previousValue, currentValue) =&amp;gt; previousValue += currentValue, 0);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope you understand. Now it's your task to write a program which will find the minimum number from a non-empty array using reduce (consider all the elements in the array are positive).&lt;/p&gt;

&lt;p&gt;Here it is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [4, 2, 3, 1];
let result = arr.reduce((minValue, currentValue) =&amp;gt; {
  if (currentValue &amp;lt; minValue) {
    minValue = currentValue;
  }
  return minValue;
}); // no initial value 😎
console.log(result);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;❤️ Happy Coding ❤️&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
