Specification
For this challenge you will:
- Take an array of positive integers.
- For each overlapping pair in the array, calculate the difference of it's integers. If the difference is a common divisor of the integers (they are both divisible by their difference), swap their positions in the array. The values of the next pair are affected by this swap. No integer is divisible by
0
. - If an integer was not divisible by the difference of any of it's pairs, remove it from the array.
- Repeat the previous two steps until the length of the array will not decrease any further.
- Output the length of the resulting array.
Example
- Input =
[ 4, 5, 2, 4 ]
. - Difference of first pair
[ ( 4, 5, ) 2, 4 ]
=1
, (common divisor of4
and5
)- Swap the pair and the array becomes
[ 5, 4, 2, 4 ]
.
- Swap the pair and the array becomes
- Difference of second pair
[ 5, ( 4, 2, ) 4 ]
=2
(common divisor of4
and2
)- Swap the pair so the array becomes
[ 5, 2, 4, 4 ]
.
- Swap the pair so the array becomes
- Difference of third pair
[ 5, 2, ( 4, 4 ) ]
=0
(not a divisor of any integer)- Do not swap the pair so the array remains the same.
- The final
4
was never swapped, remove it so the array becomes[ 5, 2, 4 ]
. - Repeat.
- Difference of first pair
[ ( 5, 2, ) 4 ]
=3
- Do not swap the pair so the array remains the same.
- Difference of second pair
[ 5, ( 2, 4 ) ]
=2
- Swap the pair so the array becomes
[ 5, 4, 2 ]
.
- Swap the pair so the array becomes
- The
5
was never swapped, remove it so the array becomes[ 4, 2 ]
- Repeat.
- From here the array will endlessly switch between
[ 4, 2 ]
and[ 2, 4 ]
, so output the resulting length of2
.
Rules
- Input array will always have a length between 2 and 9 inclusive.
- Integers will always be between 1 and 999 inclusive.
- Input can optionally be in the form of a string delimited by a specific character.
- This is code-golf so shortest code in bytes wins.
Test Cases
Format: [ input array ] = result (steps to get result, do not output this)
[ 1, 1 ] = 0 ([])
[ 1, 2 ] = 2 ([ 2, 1 ] -> [ 1, 2 ] -> ...)
[ 1, 2, 3 ] = 2 ([ 2, 1 ] -> [ 1, 2 ] -> ...)
[ 4, 6, 8 ] = 3 ([ 6, 8, 4 ] -> [ 8, 4, 6 ] -> [ 4, 6, 8 ] -> ...)
[ 99, 1, 3 ] = 0 ([])
[ 4, 5, 2, 4 ] = 2 ([ 5, 2, 4 ] -> [ 4, 2 ] -> [ 2, 4 ] -> ...)
[ 12, 4, 6, 3 ] = 3 ([ 6, 4, 3 ] -> [ 4, 3, 6 ] -> [ 3, 6, 4 ] -> ...)
[ 9, 6, 10, 18 ] = 2 ([ 6, 10, 18, 9 ] -> [ 9, 18 ] -> [ 18, 9 ] -> ...)
[ 55, 218, 654, 703, 948, 960 ] = 2
[ 954, 973, 925, 913, 924, 996, 927, 981, 905 ] = 2
[924, 996, 913] -> [913, 996]
\$\endgroup\$[12,4,6,3] -> [6,3,4] -> [3,4,6] -> [4,6,3]
\$\endgroup\$