ALGORITHM RIDDLE
you get a number x and you have to set the all the from 1 to x twic in an array of size 2x.
the rule is the numbers have to be placed in a cell that is the difference from their value.
for example if you set the number 2 like this:
|2| | | | |
the bex one has to be like this:
|2| |2| | |
let’s say x=4, then we have an array of 8 cells and we can set the numbers like this:
1 2 3 4 5 6 7 8
|4|2|3|2|4|3|1|1|
you can place the number in various ways.
can you do the same for the number 10 (array size 20), 12(array size 24)?
if yes, show how, if not explain why for each number.
This problem is NP-Complete you must try all possibilities to determine feasibility and to get a result set of possible solutions.
If you want a yes/no answer to whether it can be done that is defined by the function
1/2(4n + (-1)^n - 1), plugging values of n starting at 1…infinity will give you the sequence of numbers that are yes answers. Proof not included 🙂To clarify, the solution set finding is NP-Complete and the yes/no answer for a given N can be done using the above formula. (I hope I haven’t fudged the math on that one, its a generating function)