In an alternating permutation of numbers 1 to n, the numbers are arranged so that they alternately increase and decrease. For example, there are five alternating permutations of the numbers 1, 2, 3, and 4:
1 3 2 4
1 4 2 3
2 3 1 4
2 4 1 3
3 4 1 2
Write a function to count the alternating permutations for a set of n numbers.
Solution Stats
Problem Comments
3 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers13
Suggested Problems
-
7001 Solvers
-
Return a list sorted by number of occurrences
2901 Solvers
-
Calculate the area of a triangle between three points
3573 Solvers
-
Remove from a 2-D matrix all the rows that contain at least one element less than or equal to 4
140 Solvers
-
Given a matrix, swap the 2nd & 3rd columns
1284 Solvers
More from this Author329
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
The test case result for n=5 seems a tad high.
Tim, that last case is altperm(altperm(5)). I did that to discourage lookup solutions.
Oops--my mistake.