[ create a new paste ] login | about

Link: http://codepad.org/QR9ptUwR    [ raw code | output | fork | 1 comment ]

C++, pasted on Apr 12:
#include <map>

void print_pairs(int array[], int length, int sum)
{
   std::map<int, int> element;
   for (int i=0; i< length; ++i)
   {
      element.insert( std::pair<int, int>(array[i], 1));
      if(element.count(sum - array[i]) )
         printf("\n{%d, %d}", array[i], sum - array[i]);
     
   }
}

int main()
{
   int a[] = {3, 4, 5, 1, 4, 2};
  
   print_pairs(a, 6, 6);   

   return 0;
}


Output:
1
2
3
4

{3, 3}
{1, 5}
{2, 4}


Create a new paste based on this one


Comments:
posted by chiffa on Sep 25
Why {3,3} if you only have one 3 in your array? Am I missing something?
reply