SRM 704, D2, 250-Pointer (SwapAndArithmetic)

James S. Plank

Fri Feb 3 14:42:38 EST 2017

In case Topcoder's servers are down

Here is a summary of the problem:

The examples

Example x Answer
0
{ 3, 1, 2 }
"Possible" (swap it to become { 3, 2, 1 } or { 1, 2, 3 }
1
{ 1, 2, 4 }
"Impossible"
2
{ 1, 1, 1, 1, 1, 1 }
"Possible"
3
{ 1000, 704, 1 }
"Impossible"
4
{ 7, 3, 11, 5, 1, 9 }
"Possible"


Testing yourself

Like the Cryptography Problem, I have a shell script tests.sh, that you can use to test your program. When you run tests.sh, your answer should be identical to answers.txt

Hints

Try this on your own. If you get stuck, return here and read what I've written below.



























































If you're here, think about how much easier the problem is if you sort the input. Now, go back and try to solve it again, and if you get stuck again, come back.



























































What you want to do is sort the vector. You can do that most easily with the sort() method from the STL algorithms library. You need to include algorithms, and then you can sort the vector with sort(x.begin(), x.end()). Then, the easiest thing to do is try to discover if the vector is not an arithmetic sequence.

The way that I did that was to: