<?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: Mrinal Haque</title>
    <description>The latest articles on DEV Community by Mrinal Haque (@mrinal013).</description>
    <link>https://dev.to/mrinal013</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%2F1039439%2F3db9430e-e9f5-415e-87d5-b2916ca488c1.jpeg</url>
      <title>DEV Community: Mrinal Haque</title>
      <link>https://dev.to/mrinal013</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mrinal013"/>
    <language>en</language>
    <item>
      <title>Problem solve with PHP: Two sum</title>
      <dc:creator>Mrinal Haque</dc:creator>
      <pubDate>Thu, 23 Mar 2023 08:58:58 +0000</pubDate>
      <link>https://dev.to/mrinal013/problem-solve-with-php-two-sum-3flc</link>
      <guid>https://dev.to/mrinal013/problem-solve-with-php-two-sum-3flc</guid>
      <description>&lt;p&gt;**Problem: **Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.&lt;/p&gt;

&lt;p&gt;You may assume that each input would have exactly one solution, and you may not use the same element twice.&lt;/p&gt;

&lt;p&gt;You can return the answer in any order.&lt;/p&gt;

&lt;p&gt;Here is &lt;a href="https://leetcode.com/problems/two-sum/" rel="noopener noreferrer"&gt;leetcode problem&lt;/a&gt;, where all details are explain.&lt;/p&gt;

&lt;p&gt;As far, I can solve the problem in three ways.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution 1&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;class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $target
     * @return Integer[]
     */
    function twoSum($nums, $target) {
        $n = count($nums);

        for ( $i=0; $i&amp;lt;$n-1; $i++ ) {
            for ( $j=$i+1; $j&amp;lt;$n; $j++ ) {
                if ( $nums[$i] + $nums[$j] === $target ) {
                    return [$i, $j];
                }
            }
        }

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Solution 2&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;class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $target
     * @return Integer[]
     */
    function twoSum($nums, $target) {
        $n = count($nums);

        for ( $i = 0; $i &amp;lt; $n -1; $i++ ) {
            $remain = $target - $nums[$i];
            $arr_remain = array_slice( $nums, $i + 1 );

            if ( in_array( $remain, $arr_remain ) ) {
                $pos = array_search( $remain, $nums );
                if ( $pos === $i ) {
                    $arr_keys = array_keys( $nums, $remain );
                    $pos = $arr_keys[1];
                }
                return [ $i, $pos ];
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Solution 3&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;class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $target
     * @return Integer[]
     */
    function twoSum($nums, $target) {
        $n = count($nums);

        $hash_table = [];
        for ( $i = 0; $i &amp;lt; $n - 1; $i++ ) {
            $remain = $target - $nums[$i];
            if ( in_array( $remain, $hash_table ) ) {
                $arr_key = array_keys( $hash_table, $remain );
                return [ $arr_key[0], $i ];
            }
            $hash_table[$i] = $nums[$i];
        }

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

&lt;/div&gt;



&lt;p&gt;However, the 3rd solution should better optimized in algorithmic context, but I get the 1st solution is more fast. Any idea is most welcome for the reason of speed.&lt;/p&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
