We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
We could also see this directly as we simply want to count the permutations for which A or B appears before any of the K intermediate nodes.
So consider the function FA, resp. FB, that transforms any permutation into a such a one by swapping the first of the K+2 nodes of the path with A, resp. B. Each permutation in the image of FA/FB has exactly K+2 pre-images so that the image has N!/(K+2) elements.
The set of permutations we want to count is the disjoint union of the images of FA and FB, so that their number is 2*N!/(K+2).
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Magic Number Tree
You are viewing a single comment's thread. Return to all comments →
We could also see this directly as we simply want to count the permutations for which A or B appears before any of the K intermediate nodes.
So consider the function FA, resp. FB, that transforms any permutation into a such a one by swapping the first of the K+2 nodes of the path with A, resp. B. Each permutation in the image of FA/FB has exactly K+2 pre-images so that the image has N!/(K+2) elements.
The set of permutations we want to count is the disjoint union of the images of FA and FB, so that their number is 2*N!/(K+2).