Find items in an array that sum to 15…

Messy and hacky, should use set rather than map. In reality should use a hash…

#include <vector>
#include <map>

using namespace std;

int main() {

  vector<int> array;

  map<int,int> exists;
  for(size_t n=0;n<array.size();n++) {
    exists[array[n]] = 1;
  for(size_t n=0;n<array.size();n++) {
    int value_needed = 15-array[n];
    if(exists[value_needed] == 1) cout << "pair: " << array[n] << " " << value_needed << endl;


Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>