For each part to fall "within a given range (inclusive)", the solutions for n = 5 and Range = 2 to 4
would be (2,3), (3,2)
.
The most trivial solution is like this:
for i = range_min to range_max:
if range_min <= n - i <= range_max:
print (i, n-i)
Update. A deeper analysis:
a = range_min
b = range_max
a <= i <= b
a <= n - i <= b
a - n <= -i <= b - n
-a + n >= i >= -b + n
n - b <= i <= n - a
max(a, n-b) <= i <= min(b, n-a)
for i = max(a, n-b) to min(b, n-a):
print (i, n-i)
There is nothing more efficient possible, as anyway you need to loop through all the matching pairs at least to print them, and here there is only printing.
Update 2. Only one pair (if exists) and in C++:
#include <iostream>
#include <algorithm> // min, max
using namespace std;
int main()
{
int n, a, b;
cin >> n >> a >> b;
int lo = max(a, n-b);
int hi = min(b, n-a);
if (lo <= hi) {
cout << "(" << lo << ", " << n - lo << ")" << endl;
} else {
cout << "nope" << endl;
}
return 0;
}