Skip to content

Instantly share code, notes, and snippets.

@asa55
Created April 3, 2020 22:26
Show Gist options
  • Save asa55/f1b3fc0855c2109cf8d1cb69b0a42752 to your computer and use it in GitHub Desktop.
Save asa55/f1b3fc0855c2109cf8d1cb69b0a42752 to your computer and use it in GitHub Desktop.
// How do you find all pairs of an integer array whose sum is equal to a given number?
#include <iostream>
#include <array>
int desired_sum = 10;
std::array<int, 10> arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int main ()
{
int i, j;
for ( i = 0; i < arr.size(); ++i)
for ( j = i; j < arr.size(); ++j )
if ( arr[i] + arr[j] == desired_sum )
std::cout << i << " " << j << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment