Problem Statement
Consider the set of integers between 1 and n, inclusive, and two positive integers c and k. You want to build an ordered list of k pairs (x1, y1), (x2, y2), ... (xk, yk) such that the following conditions are met.
- 1 ≤ xi < yi ≤ n for all i between 1 and k, inclusive.
- yi < xi+1 for all i between 1 and k-1, inclusive.
- (xi+1 + yi+1) - (xi + yi) = c for all i between 1 and k-1, inclusive.
For instance, consider n=5, c=4, and k=2. There are two stable lists of pairs: one is [(1, 2), (3, 4)] and the other is [(2, 3), (4, 5)]. The latter is the only maximal stable list of pairs in this example as its sum is (2+3+4+5) = 14.
Given n, c, and k, find one maximal stable list of pairs, and return a int[] that describes the list. Specifically, if (x1, y1), (x2, y2), ..., (xk, yk) is your maximal stable list of pairs, you must return { x1, y1, x2, y2, ..., xk, yk }.
If there are many maximal stable lists of pairs, you may return any one of them. If no stable list of pairs with the desired properties exists, you must return an empty int[].
Definition
- Class:
- StablePairsDiv1
- Method:
- findMaxStablePairs
- Parameters:
- int, int, int
- Returns:
- int[]
- Method signature:
- int[] findMaxStablePairs(int n, int c, int k)
- (be sure your method is public)
Constraints
- n will be between 1 and 100, inclusive.
- c will be between 1 and 100, inclusive.
- k will be between 1 and 100, inclusive.
Examples
5
4
2
Returns: {2, 3, 4, 5 }This example was described in the problem statement.4
4
2
Returns: {1, 2, 3, 4 }1
100
1
Returns: { }When n=1, regardless of c and k, there is no way to build a pair.3
100
1
Returns: {2, 3 }With k=1 we are looking for stable lists that only contain a single pair of numbers. There are three stable lists: [(1, 2)], [(1, 3)], and [(2, 3)]. Obviously, the last one is the only maximal one in this case.10
6
3
Returns: {2, 5, 6, 7, 9, 10 }12
7
3
Returns: {4, 5, 6, 10, 11, 12 }