Function to output all combinations that give a certain value

I have two vectors _A and _B and an equation (x-y)/2, where the x is vector _A and y is _B. So for example _A={8,1,16} and B={4,1,8} my output should be all the combinations that give a postive integer greater than 0 so {(8,4),(16,8)}. How can i make it output only the numbers that work for this equation.


My Code for the function:

for(int i =0; i<_A.size(); i++){
for(int j=0; j<_B.size();j++){
int INT =( _A.at(i) - _B.at(j))/2;
if (INT % 1 == 0 && INT > 0) {
cout << "{";
for(int i =0; i<_A.size();i++){

cout << "(" << _A.at(i) << "," << _B.at(i) << "),";
}
cout << "}";
cout << endl;

}else{
cout << "The number is not an intger" << endl;
}
}
}
you want %2 instead of %1 I think.
%1 checks if a number is zero (its either 1 or undefined). %2 checks if it is even/odd (divisible by 2). ...
Last edited on
Thanks joninn I think I finally got it working
Not sure that I agree with your model output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>
using namespace std;

vector<pair<int,int>> evenPositiveDifference( const vector<int> &A, const vector<int> &B )
{
   vector<pair<int,int>> result;
   for ( int x : A )
   {
      for ( int y : B ) if ( x > y && ( x - y ) % 2 == 0 ) result.push_back( { x, y } );
   }
   return result;
}

int main()
{
   vector<int> A = { 8, 1, 16 }, B = { 4, 1, 8 };
   for ( auto p : evenPositiveDifference( A, B ) ) cout << p.first << ", " << p.second << '\n';
}


8, 4
16, 4
16, 8
Topic archived. No new replies allowed.