Imagine you arrange the thirteen integers 1 to 1 3 in a row and label them a 1 , a 2 , … , a 1 3 from left to right under the following conditions:
How many such arrangements are possible?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Ah, just what I was thinking, William... Nice explanation. You beat me to it! :-)
Great solution!
Yay, thanks for acknowledging me :D
Nice solution!
I got the correct answer 377. I did some unnecessary thing.
Consider the second constraint only. We won't need the first, because it is implied by the second:
The maximum value a n can take, for any n = 1 , 2 , . . . , 1 1 , is n + 1 = n + 2 − 1 , which, once taken, limits the possible values for a n + 2 to n + 2 and n + 2 + 1 , implying a n + 2 > a n .
Note that:
There are two possible positions for the number 1. Also, if a 2 = 1 , then a 1 = 2 necessarily.
There are two possible positions for 2 if a 1 = 1 , and one if a 2 = 1 .
In general, after the positions for the first
k
numbers are picked, we have
k
numbers distributed among the first
k
+
1
spaces of the list, and two things may
happen:
How, then, can we find a closed expression for the number of possibilities with such a strange tree of ramifications? I don't know for certain, so I wrote a bit of code to count for me, which gave me 377. The code itself is not the prettiest, but hopefully it is understandable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
Try altering your code to find the number of possibilities when there is only 1 integer in the list (instead of 13), then 2, then 3, then 4, and so on, and hopefully a familiar sequence will present itself.
Log in to reply
You're right. Sorry, I was lazy af.
Log in to reply
Lazy? That program looks like more work than I did.
Log in to reply
@Jeremy Galvagni – I was mentally lazy, so I adopted a mechanical solution. But yeah, it depends on how laziness is defined.
Log in to reply
@Fabricio Kolberg – I guess that shows how programmers see the world.
Log in to reply
@Jeremy Galvagni – Not sure if I should be ashamed or proud xD
Yup. Brilliant problem. Brilliant Allbritain solution/explanation (above). The problem should have immediately triggered a thought something like, "Fibonacci something something...but wtf, how exactly?" But of course it didn't until someone pointed it out.
As a CS grad student, I start every problem with the naive solution and optimize it to get efficient ones.
As a habit, I first thought about the solution in the following way :
Generate all possible permutations (13!) and check for the constraints whether they are satisfied or not and increment a counter every time it does.
As you can see,It is horrifyingly inefficient.
So, then I solved the Inspiration problem first, without taking help of any Intel chips. and reached the answer : 1716
Now all I had to do is generate all these (1716) possibilities and check for the constraints which could be done in O(1).
The following is the code which shall do (Output : all the valid arrangements followed by the count of it).
import itertools as it
A = list(it.combinations(range(1,14),7))
count=0
for a in A:
i=1
for aa in a:
if(aa == (i-1) or aa == (i) or aa == (i+1)):
i=i+2
else:
break
if(i==15):
count=count+1
print (a)
print (count)
Consider the general case for m integers 1..m, with boundary conditions n=1...m-2 in the first and n=1...m in the second condition. Let f(m) be the number of such arrangements. if we fix a m = m , we observe that there are f(m-1) possibilities for the arrangement before it. The only other option is a m = m − 1 , implying that a m − 1 = m , so that there are f(m-2) possibilities for the arrangement before that. So f(m)=f(m-2)+f(m-1). Together with the starting condition f(1)=1, f(2)=2 it is the fibonacci sequence, so f(13)=377.
Problem Loading...
Note Loading...
Set Loading...
As Fabricio notes, only the second constraint need be considered, as the first follows naturally: a n ≤ n + 1 ≤ a n + 2 , and equality cannot hold on both sides. Therefore, such an arrangement is a permutation of the normal numerical order in which each number is either in its original position or has been swapped with its neighbor on either side. For example,
( 1 , 2 , 4 , 3 , 5 , 7 , 6 , 9 , 8 , 1 1 , 1 3 , 1 2 ) .
More generally, the question then becomes: how many disjoint sets of pairs of consecutive numbers can be chosen from the set { 1 , 2 , … , n } ? These will be the pairs that are swapped to create an arrangement. (In our situation n = 1 3 , of course.) Denote this number by P n . By direct computation we have P 1 = 1 , P 2 = 2 . For n > 2 , such a permutation either fixes 1 or swaps it with 2 . If 1 is fixed, there are P n − 1 possible arrangements for the remaining n − 1 numbers. If 1 and 2 are swapped, there are P n − 2 possible arrangements for the remaining n − 2 numbers. This means
P n = P n − 1 + P n − 2 ,
i.e. this sequence is nothing more than our old friend the Fibonacci sequence! Specifically P n = F n + 1 . Therefore the solution for the given problem is P 1 3 = F 1 4 = 3 7 7 . □