title: LeetCode: TwoSum Solution
body: <p>I'm trying to use a map in order to make the algorithm for this answer <a href="https://leetcode.com/problems/two-sum/#/description" rel="nofollow noreferrer">https://leetcode.com/problems/two-sum/#/description</a> O(n), but for some reason i'm getting the improper indices for a lot of test cases i'm trying</p>
  
  <p>From what I can tell, I'm doing the checks properly for the iterators, however these are kind of new to me, and i'm not entirely sure if i'm returning the correct indices that I want.</p>
  
  <pre><code>//My Code:
  
  
  
  #include &lt;iostream&gt;
  #include &lt;cstdio&gt;
  #include &lt;array&gt;
  #include &lt;vector&gt;
  #include &lt;map&gt;
  
  std::vector&lt;int&gt; twoSum(int nums[], int size, int target)
  {
      std::vector&lt;int&gt; answer;
      std::map&lt;int, int&gt; myMap;
      std::map&lt;int, int&gt;::iterator it;
      std::cout &lt;&lt; size &lt;&lt; std::endl;
      for (int i = 0; i &lt; size; i++)
      {
          myMap.insert(std::pair&lt;int, int&gt;(nums[i], i));
          int indexItOne = distance(myMap.begin(), myMap.find(nums[i]));
          int indexItTwo = distance(myMap.begin(), myMap.find(target-nums[i]));
          it = myMap.find(target - nums[i]);
  
          if (it != myMap.begin() || indexItOne != indexItTwo)
          {
              answer.push_back(i);
              answer.push_back(distance(myMap.begin(), myMap.find(target - nums[i])));
              return answer;
          }
  
      }//for
  
  }//twoSum
  </code></pre>