-3

I am having a question about permutation in Java.

Suppose I have five different elements in an array [a, b, c, d, e] and I want to select three elements out of them, the order does matter. I know in math we can use 5P3 to get the answer, but can we use Java to get the count and the complete list of elements of the permutation set of 5P3?

Ryan L
  • 119
  • 13
  • 2
    not entirely sure I understand what you're asking for, it looks like you just want P53 in java? This link has it all: https://github.com/atapazvant/Project-Euler/blob/master/Project-Euler/src/project/euler/solutions/p53.java – Ronak Shah Jul 04 '17 at 21:33
  • Do you want to return all the permutations, or do you just want to return the number of permutations? – ajb Jul 04 '17 at 21:34
  • I want all the permutations and the number of all permutations. Thx – Minghua Zheng Jul 04 '17 at 22:41

1 Answers1

0

Since "1, 2, 3" is different from "2, 1, 3", I will suggest the following code snippet. Note that i can't be identical to j or k to avoid "1, 1, 1" or "1, 3, 3" scenarios.

   // array with elements
   char[] items = {'a', 'b', 'c', 'd', 'e'};
   int count = 0;

    // first of the "three"
    for (int i = 0; i < upperBound; i++) {
       // second of the "three"
       for (int j = 0; j < upperBound; j++) {
          // can't be identical to i
          if (j == i) 
             continue;

          // third of the "three"
          for (int k = 0; k < upperBound; k++) {
             // can't be identical to i or j
             if (k == i || k ==j) 
                continue;

             // print some display
             System.out.println(items[i] + ", " + items[j] + ", " + items[k]);

             // increment the total count
             count++;
          }
       }
    }



    System.out.println("Total count is " + count);
Ryan L
  • 119
  • 13
  • Welcome to Stack Overflow! While you may have solved this user's problem, code-only answers are not very helpful to users who come to this question in the future. Please edit your answer to explain why your code solves the original problem. – Joe C Jul 04 '17 at 21:59
  • Thx @Joe. Revised per advice. – Ryan L Jul 04 '17 at 22:32
  • 1
    Thx @RyanL. Suppose I have i items in an array like [1, 2, 3, 4, 5, 6, ….i-2, i-1, i] I wanna select j elements out of them, the order does matter, how can we use Java to get the value of jPi and all results? – Minghua Zheng Jul 05 '17 at 10:43
  • Hi Minghua, did you mean iPj instead? – Ryan L Jul 05 '17 at 11:26
  • sorry for my typo. select j items from i, the order does matter. it's similar to the code you just provided (like select 3 items from a five items array [a, b,c,d,e]), but the number of the for loop is a variable. Thx@RyanL – Minghua Zheng Jul 05 '17 at 12:23
  • @Minghua, thx for acknowledging this answer. However, I am still working on your n element solution. Hopefully I can get something and let you know. – Ryan L Jul 05 '17 at 21:49
  • @RyanL check out this link. https://stackoverflow.com/questions/44949030/print-all-possible-permutations-of-r-elements-in-a-given-integer-array-of-size-n – Minghua Zheng Jul 06 '17 at 16:49
  • Thanks @MinghuaZheng for pointed out. There seems to have some similarities in there :) – Ryan L Jul 06 '17 at 18:07
  • @MinghuaZheng, here's what I am thinking about your IPJ scenario. (note that J is different from j. J is a pre-determined number of items to take out from the collection of I. Here we go: for(j = 0; j < J; j++) {pickedItemList.add(j); GetItem(pickedItemList); } void GetItem(ArrayList pickedItemList) { for(i = 0; i < I; i++) {// if i is not in a arraylist, arraylist add (i), otherwise continue; GetItem(pickedItemList); }} As you see above from the "incomplete pseudo code snippet", recursion is applied. I said it's incomplete because defensive coding to guard against infinite looping is needed. – Ryan L Jul 06 '17 at 19:05
  • 1
    @RyanL, thx. You are right. I will do more resarch about recursion. – Minghua Zheng Jul 06 '17 at 23:24