public class PermutationSeating { public static void main (String[] argv) { int numSeats = 3; // M int numPeople = 2; // K int n = countPermutations (numSeats, numPeople); System.out.println (numPeople + " can sit on " + numSeats + " seats in " + n + " different arrangements"); } static int countPermutations (int numSpaces, int numRemaining) { // Given numRemaining to assign among numSpaces seats, // count all possible arrangements. // Bottom out case: if none are remaining, there's only one way to do that. if (numRemaining == 0) { return 1; } // Otherwise, obtain the count for a smaller version of the problem. int n = countPermutations (numSpaces-1, numRemaining-1); // Since one person can chose from among numSpaces, there // numSpaces ways of doing that, which we multiply with n. return (numSpaces * n); } }