How to print out X number of permutations in java?











up vote
0
down vote

favorite












The code I have written takes elements of an array and goes through the array to give all permutations. But I need it to only display a certain number of permutations:



The final code is to give only 6 permutations of 9 elements (in other words, print the first 60480 arrangements of the total 362880 outputs). For simplicity, I'm working with 4 elements in the array and I get all 24 arrangements to print out. But I need the code to work for any number of permutations. For example, if I need it to print out 1-permutation, the code should print the first 4 arrangements - ABCD, ABDC, ACBD, and ACDB. I'm unsure how to solve this.



public static void main(String args) {
// TODO Auto-generated method stub


String myArray = {"A","B","C", "D"};
int size = myArray.length;
permutation(myArray, 0, size-1);

// Calculate Permutations
int n=size;
int r=6; // subject to change
int p = n - r;
int total=1;
int total2=1;
int total3=0;

for (int top=n; top>0; top--)
{
total *= top;
}

if ((n-r<0))
{
System.out.println("r value cannot be greater than array size");
total3=0;
}
else
{
for (int bot=1; bot<=p; bot++)
{
if (p==0) // should be -- else if (p==0) -- after correction
{
total2=1;
}
else
{
total2 *= bot;
}
}
total3 = total/total2;
}

System.out.printf("%d permutations of %d elements = %dn",r,n,total3);
// end calculation

}
// end main

// print array
public static void prtArray(String myArray, int size)
{
for(int i=0; i<size; i++)
{
System.out.printf("%s", myArray[i]);
}
System.out.println();
}

// swap elements
public static void swap(String myArray, int i, int j) {
String temp;
temp = myArray[i];
myArray[i]=myArray[j];
myArray[j]=temp;
}

// permutation
private static void permutation(String myArray, int b, int e)
{
if (b == e)
prtArray(myArray, e+1); // accounts for array of size 1
else
{
for(int i = b; i <= e; i++)
{

swap(myArray, i, b);
permutation(myArray, b+1, e);
swap(myArray, i, b);

}
}
}
}









share|improve this question
























  • I don't really see an issue. The code runs and if I add 9 elements in the array I get all the permutations. What is really the question?
    – Sven
    Nov 5 at 5:55










  • Please provide the code that doesn't work, instead of code that works with a vague description of what to change to make it not work. Also please add the full exception message and stacktrace, so we know where exactly what goes wrong. Thanks!
    – Max Vollmer
    Nov 5 at 7:43










  • I know the code runs fine, the problem is that it prints out ALL permutations. I'm working with 4 elements in my array so when I run the code I get 24 different arrangements printing out. But, for example, say I need to print out for 2-permutations then I should only get the first 12 arrangements to print out. The code needs to account for r-permutations where the formula for permutations is [(# of elements)! / (# of elements - r)! ]. This is where I'm stuck.
    – Fab J
    Nov 5 at 21:47















up vote
0
down vote

favorite












The code I have written takes elements of an array and goes through the array to give all permutations. But I need it to only display a certain number of permutations:



The final code is to give only 6 permutations of 9 elements (in other words, print the first 60480 arrangements of the total 362880 outputs). For simplicity, I'm working with 4 elements in the array and I get all 24 arrangements to print out. But I need the code to work for any number of permutations. For example, if I need it to print out 1-permutation, the code should print the first 4 arrangements - ABCD, ABDC, ACBD, and ACDB. I'm unsure how to solve this.



public static void main(String args) {
// TODO Auto-generated method stub


String myArray = {"A","B","C", "D"};
int size = myArray.length;
permutation(myArray, 0, size-1);

// Calculate Permutations
int n=size;
int r=6; // subject to change
int p = n - r;
int total=1;
int total2=1;
int total3=0;

for (int top=n; top>0; top--)
{
total *= top;
}

if ((n-r<0))
{
System.out.println("r value cannot be greater than array size");
total3=0;
}
else
{
for (int bot=1; bot<=p; bot++)
{
if (p==0) // should be -- else if (p==0) -- after correction
{
total2=1;
}
else
{
total2 *= bot;
}
}
total3 = total/total2;
}

System.out.printf("%d permutations of %d elements = %dn",r,n,total3);
// end calculation

}
// end main

// print array
public static void prtArray(String myArray, int size)
{
for(int i=0; i<size; i++)
{
System.out.printf("%s", myArray[i]);
}
System.out.println();
}

// swap elements
public static void swap(String myArray, int i, int j) {
String temp;
temp = myArray[i];
myArray[i]=myArray[j];
myArray[j]=temp;
}

// permutation
private static void permutation(String myArray, int b, int e)
{
if (b == e)
prtArray(myArray, e+1); // accounts for array of size 1
else
{
for(int i = b; i <= e; i++)
{

swap(myArray, i, b);
permutation(myArray, b+1, e);
swap(myArray, i, b);

}
}
}
}









share|improve this question
























  • I don't really see an issue. The code runs and if I add 9 elements in the array I get all the permutations. What is really the question?
    – Sven
    Nov 5 at 5:55










  • Please provide the code that doesn't work, instead of code that works with a vague description of what to change to make it not work. Also please add the full exception message and stacktrace, so we know where exactly what goes wrong. Thanks!
    – Max Vollmer
    Nov 5 at 7:43










  • I know the code runs fine, the problem is that it prints out ALL permutations. I'm working with 4 elements in my array so when I run the code I get 24 different arrangements printing out. But, for example, say I need to print out for 2-permutations then I should only get the first 12 arrangements to print out. The code needs to account for r-permutations where the formula for permutations is [(# of elements)! / (# of elements - r)! ]. This is where I'm stuck.
    – Fab J
    Nov 5 at 21:47













up vote
0
down vote

favorite









up vote
0
down vote

favorite











The code I have written takes elements of an array and goes through the array to give all permutations. But I need it to only display a certain number of permutations:



The final code is to give only 6 permutations of 9 elements (in other words, print the first 60480 arrangements of the total 362880 outputs). For simplicity, I'm working with 4 elements in the array and I get all 24 arrangements to print out. But I need the code to work for any number of permutations. For example, if I need it to print out 1-permutation, the code should print the first 4 arrangements - ABCD, ABDC, ACBD, and ACDB. I'm unsure how to solve this.



public static void main(String args) {
// TODO Auto-generated method stub


String myArray = {"A","B","C", "D"};
int size = myArray.length;
permutation(myArray, 0, size-1);

// Calculate Permutations
int n=size;
int r=6; // subject to change
int p = n - r;
int total=1;
int total2=1;
int total3=0;

for (int top=n; top>0; top--)
{
total *= top;
}

if ((n-r<0))
{
System.out.println("r value cannot be greater than array size");
total3=0;
}
else
{
for (int bot=1; bot<=p; bot++)
{
if (p==0) // should be -- else if (p==0) -- after correction
{
total2=1;
}
else
{
total2 *= bot;
}
}
total3 = total/total2;
}

System.out.printf("%d permutations of %d elements = %dn",r,n,total3);
// end calculation

}
// end main

// print array
public static void prtArray(String myArray, int size)
{
for(int i=0; i<size; i++)
{
System.out.printf("%s", myArray[i]);
}
System.out.println();
}

// swap elements
public static void swap(String myArray, int i, int j) {
String temp;
temp = myArray[i];
myArray[i]=myArray[j];
myArray[j]=temp;
}

// permutation
private static void permutation(String myArray, int b, int e)
{
if (b == e)
prtArray(myArray, e+1); // accounts for array of size 1
else
{
for(int i = b; i <= e; i++)
{

swap(myArray, i, b);
permutation(myArray, b+1, e);
swap(myArray, i, b);

}
}
}
}









share|improve this question















The code I have written takes elements of an array and goes through the array to give all permutations. But I need it to only display a certain number of permutations:



The final code is to give only 6 permutations of 9 elements (in other words, print the first 60480 arrangements of the total 362880 outputs). For simplicity, I'm working with 4 elements in the array and I get all 24 arrangements to print out. But I need the code to work for any number of permutations. For example, if I need it to print out 1-permutation, the code should print the first 4 arrangements - ABCD, ABDC, ACBD, and ACDB. I'm unsure how to solve this.



public static void main(String args) {
// TODO Auto-generated method stub


String myArray = {"A","B","C", "D"};
int size = myArray.length;
permutation(myArray, 0, size-1);

// Calculate Permutations
int n=size;
int r=6; // subject to change
int p = n - r;
int total=1;
int total2=1;
int total3=0;

for (int top=n; top>0; top--)
{
total *= top;
}

if ((n-r<0))
{
System.out.println("r value cannot be greater than array size");
total3=0;
}
else
{
for (int bot=1; bot<=p; bot++)
{
if (p==0) // should be -- else if (p==0) -- after correction
{
total2=1;
}
else
{
total2 *= bot;
}
}
total3 = total/total2;
}

System.out.printf("%d permutations of %d elements = %dn",r,n,total3);
// end calculation

}
// end main

// print array
public static void prtArray(String myArray, int size)
{
for(int i=0; i<size; i++)
{
System.out.printf("%s", myArray[i]);
}
System.out.println();
}

// swap elements
public static void swap(String myArray, int i, int j) {
String temp;
temp = myArray[i];
myArray[i]=myArray[j];
myArray[j]=temp;
}

// permutation
private static void permutation(String myArray, int b, int e)
{
if (b == e)
prtArray(myArray, e+1); // accounts for array of size 1
else
{
for(int i = b; i <= e; i++)
{

swap(myArray, i, b);
permutation(myArray, b+1, e);
swap(myArray, i, b);

}
}
}
}






java arrays recursion permutation swap






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 5 at 23:12

























asked Nov 5 at 5:32









Fab J

82




82












  • I don't really see an issue. The code runs and if I add 9 elements in the array I get all the permutations. What is really the question?
    – Sven
    Nov 5 at 5:55










  • Please provide the code that doesn't work, instead of code that works with a vague description of what to change to make it not work. Also please add the full exception message and stacktrace, so we know where exactly what goes wrong. Thanks!
    – Max Vollmer
    Nov 5 at 7:43










  • I know the code runs fine, the problem is that it prints out ALL permutations. I'm working with 4 elements in my array so when I run the code I get 24 different arrangements printing out. But, for example, say I need to print out for 2-permutations then I should only get the first 12 arrangements to print out. The code needs to account for r-permutations where the formula for permutations is [(# of elements)! / (# of elements - r)! ]. This is where I'm stuck.
    – Fab J
    Nov 5 at 21:47


















  • I don't really see an issue. The code runs and if I add 9 elements in the array I get all the permutations. What is really the question?
    – Sven
    Nov 5 at 5:55










  • Please provide the code that doesn't work, instead of code that works with a vague description of what to change to make it not work. Also please add the full exception message and stacktrace, so we know where exactly what goes wrong. Thanks!
    – Max Vollmer
    Nov 5 at 7:43










  • I know the code runs fine, the problem is that it prints out ALL permutations. I'm working with 4 elements in my array so when I run the code I get 24 different arrangements printing out. But, for example, say I need to print out for 2-permutations then I should only get the first 12 arrangements to print out. The code needs to account for r-permutations where the formula for permutations is [(# of elements)! / (# of elements - r)! ]. This is where I'm stuck.
    – Fab J
    Nov 5 at 21:47
















I don't really see an issue. The code runs and if I add 9 elements in the array I get all the permutations. What is really the question?
– Sven
Nov 5 at 5:55




I don't really see an issue. The code runs and if I add 9 elements in the array I get all the permutations. What is really the question?
– Sven
Nov 5 at 5:55












Please provide the code that doesn't work, instead of code that works with a vague description of what to change to make it not work. Also please add the full exception message and stacktrace, so we know where exactly what goes wrong. Thanks!
– Max Vollmer
Nov 5 at 7:43




Please provide the code that doesn't work, instead of code that works with a vague description of what to change to make it not work. Also please add the full exception message and stacktrace, so we know where exactly what goes wrong. Thanks!
– Max Vollmer
Nov 5 at 7:43












I know the code runs fine, the problem is that it prints out ALL permutations. I'm working with 4 elements in my array so when I run the code I get 24 different arrangements printing out. But, for example, say I need to print out for 2-permutations then I should only get the first 12 arrangements to print out. The code needs to account for r-permutations where the formula for permutations is [(# of elements)! / (# of elements - r)! ]. This is where I'm stuck.
– Fab J
Nov 5 at 21:47




I know the code runs fine, the problem is that it prints out ALL permutations. I'm working with 4 elements in my array so when I run the code I get 24 different arrangements printing out. But, for example, say I need to print out for 2-permutations then I should only get the first 12 arrangements to print out. The code needs to account for r-permutations where the formula for permutations is [(# of elements)! / (# of elements - r)! ]. This is where I'm stuck.
– Fab J
Nov 5 at 21:47












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










I am assuming you don't want an element to be repeated in a permutation.
eg.



If input array is {1, 2, 3, 4}, then for length 3: 123, 124, etc are valid permutations but 122 or 111 is not.



To avoid picking elements which are already picked, we need to pass a visited array in the recursion.



public class Main {
// Maintain a global counter. After finding a permutation, increment this.
private static int count = 0;

// pos is the current index, and K is the length of permutation you want to print, and N is the number of permutation you want to print.
private static void printPermutations(int arr, int visited, int pos, int K, int N, String str) {

// We have already found N number of permutations. We don't need anymore. So just return.
if (count == N) {
return;
}

if (pos == K) {
System.out.println(str);
count++; // we have found a valid permutation, increment counter.
return;
}

for (int i = 0; i < arr.length; i++) {
// Only recur if the ith element is not visited.
if (visited[i] == 0) {
// mark ith element as visited.
visited[i] = 1;
printPermutations(arr, visited, pos + 1, K, N, str + arr[i]);
// unmark ith element as visited.
visited[i] = 0;
}
}

}


public static void main(String args) {
int arr = {1, 2, 3, 4};
int visited = {0, 0, 0, 0}; // same as size of input array.
count = 0; // make sure to reset this counter everytime you call printPermutations.
// let's print first 4 permutations of length 3.
printPermutations(arr, visited, 0, 3, 4, "");
}
}


Outputs:



for N = 4 and K = 3 (i.e First 4 permutations of length 3):



printPermutations(arr, visited, 0, 3, 4, "");

123
124
132
134


for N = 4 and K = 4 (i.e First 4 permutations of length 4):



printPermutations(arr, visited, 0, 4, 4, "");

1234
1243
1324
1342





share|improve this answer























  • The code I have accounts for repeated elements and runs fine. The problem is that it prints out ALL permutations. I'm working with 4 elements in my array so when I run the code I get 24 different arrangements printing out. But, for example, say I need to print out for 2-permutations of this 4-element array, then I should only get the first 12 arrangements to print out. The code needs to account for r-permutations where the formula for permutations is [(# of elements)! / (# of elements - r)! ]. This is where I'm stuck.
    – Fab J
    Nov 5 at 21:48










  • If you want your recursion to stop after a "certain" number of permutation is computed, you can maintain a global counter of how many permutations are computed so far. And if you reach that number (say N), return the recursion thereafter. Please check edits.
    – Sumit Jha
    Nov 6 at 2:10










  • Please accept the answer if it worked for you.
    – Sumit Jha
    Nov 6 at 10:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148835%2fhow-to-print-out-x-number-of-permutations-in-java%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










I am assuming you don't want an element to be repeated in a permutation.
eg.



If input array is {1, 2, 3, 4}, then for length 3: 123, 124, etc are valid permutations but 122 or 111 is not.



To avoid picking elements which are already picked, we need to pass a visited array in the recursion.



public class Main {
// Maintain a global counter. After finding a permutation, increment this.
private static int count = 0;

// pos is the current index, and K is the length of permutation you want to print, and N is the number of permutation you want to print.
private static void printPermutations(int arr, int visited, int pos, int K, int N, String str) {

// We have already found N number of permutations. We don't need anymore. So just return.
if (count == N) {
return;
}

if (pos == K) {
System.out.println(str);
count++; // we have found a valid permutation, increment counter.
return;
}

for (int i = 0; i < arr.length; i++) {
// Only recur if the ith element is not visited.
if (visited[i] == 0) {
// mark ith element as visited.
visited[i] = 1;
printPermutations(arr, visited, pos + 1, K, N, str + arr[i]);
// unmark ith element as visited.
visited[i] = 0;
}
}

}


public static void main(String args) {
int arr = {1, 2, 3, 4};
int visited = {0, 0, 0, 0}; // same as size of input array.
count = 0; // make sure to reset this counter everytime you call printPermutations.
// let's print first 4 permutations of length 3.
printPermutations(arr, visited, 0, 3, 4, "");
}
}


Outputs:



for N = 4 and K = 3 (i.e First 4 permutations of length 3):



printPermutations(arr, visited, 0, 3, 4, "");

123
124
132
134


for N = 4 and K = 4 (i.e First 4 permutations of length 4):



printPermutations(arr, visited, 0, 4, 4, "");

1234
1243
1324
1342





share|improve this answer























  • The code I have accounts for repeated elements and runs fine. The problem is that it prints out ALL permutations. I'm working with 4 elements in my array so when I run the code I get 24 different arrangements printing out. But, for example, say I need to print out for 2-permutations of this 4-element array, then I should only get the first 12 arrangements to print out. The code needs to account for r-permutations where the formula for permutations is [(# of elements)! / (# of elements - r)! ]. This is where I'm stuck.
    – Fab J
    Nov 5 at 21:48










  • If you want your recursion to stop after a "certain" number of permutation is computed, you can maintain a global counter of how many permutations are computed so far. And if you reach that number (say N), return the recursion thereafter. Please check edits.
    – Sumit Jha
    Nov 6 at 2:10










  • Please accept the answer if it worked for you.
    – Sumit Jha
    Nov 6 at 10:27















up vote
0
down vote



accepted










I am assuming you don't want an element to be repeated in a permutation.
eg.



If input array is {1, 2, 3, 4}, then for length 3: 123, 124, etc are valid permutations but 122 or 111 is not.



To avoid picking elements which are already picked, we need to pass a visited array in the recursion.



public class Main {
// Maintain a global counter. After finding a permutation, increment this.
private static int count = 0;

// pos is the current index, and K is the length of permutation you want to print, and N is the number of permutation you want to print.
private static void printPermutations(int arr, int visited, int pos, int K, int N, String str) {

// We have already found N number of permutations. We don't need anymore. So just return.
if (count == N) {
return;
}

if (pos == K) {
System.out.println(str);
count++; // we have found a valid permutation, increment counter.
return;
}

for (int i = 0; i < arr.length; i++) {
// Only recur if the ith element is not visited.
if (visited[i] == 0) {
// mark ith element as visited.
visited[i] = 1;
printPermutations(arr, visited, pos + 1, K, N, str + arr[i]);
// unmark ith element as visited.
visited[i] = 0;
}
}

}


public static void main(String args) {
int arr = {1, 2, 3, 4};
int visited = {0, 0, 0, 0}; // same as size of input array.
count = 0; // make sure to reset this counter everytime you call printPermutations.
// let's print first 4 permutations of length 3.
printPermutations(arr, visited, 0, 3, 4, "");
}
}


Outputs:



for N = 4 and K = 3 (i.e First 4 permutations of length 3):



printPermutations(arr, visited, 0, 3, 4, "");

123
124
132
134


for N = 4 and K = 4 (i.e First 4 permutations of length 4):



printPermutations(arr, visited, 0, 4, 4, "");

1234
1243
1324
1342





share|improve this answer























  • The code I have accounts for repeated elements and runs fine. The problem is that it prints out ALL permutations. I'm working with 4 elements in my array so when I run the code I get 24 different arrangements printing out. But, for example, say I need to print out for 2-permutations of this 4-element array, then I should only get the first 12 arrangements to print out. The code needs to account for r-permutations where the formula for permutations is [(# of elements)! / (# of elements - r)! ]. This is where I'm stuck.
    – Fab J
    Nov 5 at 21:48










  • If you want your recursion to stop after a "certain" number of permutation is computed, you can maintain a global counter of how many permutations are computed so far. And if you reach that number (say N), return the recursion thereafter. Please check edits.
    – Sumit Jha
    Nov 6 at 2:10










  • Please accept the answer if it worked for you.
    – Sumit Jha
    Nov 6 at 10:27













up vote
0
down vote



accepted







up vote
0
down vote



accepted






I am assuming you don't want an element to be repeated in a permutation.
eg.



If input array is {1, 2, 3, 4}, then for length 3: 123, 124, etc are valid permutations but 122 or 111 is not.



To avoid picking elements which are already picked, we need to pass a visited array in the recursion.



public class Main {
// Maintain a global counter. After finding a permutation, increment this.
private static int count = 0;

// pos is the current index, and K is the length of permutation you want to print, and N is the number of permutation you want to print.
private static void printPermutations(int arr, int visited, int pos, int K, int N, String str) {

// We have already found N number of permutations. We don't need anymore. So just return.
if (count == N) {
return;
}

if (pos == K) {
System.out.println(str);
count++; // we have found a valid permutation, increment counter.
return;
}

for (int i = 0; i < arr.length; i++) {
// Only recur if the ith element is not visited.
if (visited[i] == 0) {
// mark ith element as visited.
visited[i] = 1;
printPermutations(arr, visited, pos + 1, K, N, str + arr[i]);
// unmark ith element as visited.
visited[i] = 0;
}
}

}


public static void main(String args) {
int arr = {1, 2, 3, 4};
int visited = {0, 0, 0, 0}; // same as size of input array.
count = 0; // make sure to reset this counter everytime you call printPermutations.
// let's print first 4 permutations of length 3.
printPermutations(arr, visited, 0, 3, 4, "");
}
}


Outputs:



for N = 4 and K = 3 (i.e First 4 permutations of length 3):



printPermutations(arr, visited, 0, 3, 4, "");

123
124
132
134


for N = 4 and K = 4 (i.e First 4 permutations of length 4):



printPermutations(arr, visited, 0, 4, 4, "");

1234
1243
1324
1342





share|improve this answer














I am assuming you don't want an element to be repeated in a permutation.
eg.



If input array is {1, 2, 3, 4}, then for length 3: 123, 124, etc are valid permutations but 122 or 111 is not.



To avoid picking elements which are already picked, we need to pass a visited array in the recursion.



public class Main {
// Maintain a global counter. After finding a permutation, increment this.
private static int count = 0;

// pos is the current index, and K is the length of permutation you want to print, and N is the number of permutation you want to print.
private static void printPermutations(int arr, int visited, int pos, int K, int N, String str) {

// We have already found N number of permutations. We don't need anymore. So just return.
if (count == N) {
return;
}

if (pos == K) {
System.out.println(str);
count++; // we have found a valid permutation, increment counter.
return;
}

for (int i = 0; i < arr.length; i++) {
// Only recur if the ith element is not visited.
if (visited[i] == 0) {
// mark ith element as visited.
visited[i] = 1;
printPermutations(arr, visited, pos + 1, K, N, str + arr[i]);
// unmark ith element as visited.
visited[i] = 0;
}
}

}


public static void main(String args) {
int arr = {1, 2, 3, 4};
int visited = {0, 0, 0, 0}; // same as size of input array.
count = 0; // make sure to reset this counter everytime you call printPermutations.
// let's print first 4 permutations of length 3.
printPermutations(arr, visited, 0, 3, 4, "");
}
}


Outputs:



for N = 4 and K = 3 (i.e First 4 permutations of length 3):



printPermutations(arr, visited, 0, 3, 4, "");

123
124
132
134


for N = 4 and K = 4 (i.e First 4 permutations of length 4):



printPermutations(arr, visited, 0, 4, 4, "");

1234
1243
1324
1342






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 6 at 2:23

























answered Nov 5 at 7:45









Sumit Jha

1,1881924




1,1881924












  • The code I have accounts for repeated elements and runs fine. The problem is that it prints out ALL permutations. I'm working with 4 elements in my array so when I run the code I get 24 different arrangements printing out. But, for example, say I need to print out for 2-permutations of this 4-element array, then I should only get the first 12 arrangements to print out. The code needs to account for r-permutations where the formula for permutations is [(# of elements)! / (# of elements - r)! ]. This is where I'm stuck.
    – Fab J
    Nov 5 at 21:48










  • If you want your recursion to stop after a "certain" number of permutation is computed, you can maintain a global counter of how many permutations are computed so far. And if you reach that number (say N), return the recursion thereafter. Please check edits.
    – Sumit Jha
    Nov 6 at 2:10










  • Please accept the answer if it worked for you.
    – Sumit Jha
    Nov 6 at 10:27


















  • The code I have accounts for repeated elements and runs fine. The problem is that it prints out ALL permutations. I'm working with 4 elements in my array so when I run the code I get 24 different arrangements printing out. But, for example, say I need to print out for 2-permutations of this 4-element array, then I should only get the first 12 arrangements to print out. The code needs to account for r-permutations where the formula for permutations is [(# of elements)! / (# of elements - r)! ]. This is where I'm stuck.
    – Fab J
    Nov 5 at 21:48










  • If you want your recursion to stop after a "certain" number of permutation is computed, you can maintain a global counter of how many permutations are computed so far. And if you reach that number (say N), return the recursion thereafter. Please check edits.
    – Sumit Jha
    Nov 6 at 2:10










  • Please accept the answer if it worked for you.
    – Sumit Jha
    Nov 6 at 10:27
















The code I have accounts for repeated elements and runs fine. The problem is that it prints out ALL permutations. I'm working with 4 elements in my array so when I run the code I get 24 different arrangements printing out. But, for example, say I need to print out for 2-permutations of this 4-element array, then I should only get the first 12 arrangements to print out. The code needs to account for r-permutations where the formula for permutations is [(# of elements)! / (# of elements - r)! ]. This is where I'm stuck.
– Fab J
Nov 5 at 21:48




The code I have accounts for repeated elements and runs fine. The problem is that it prints out ALL permutations. I'm working with 4 elements in my array so when I run the code I get 24 different arrangements printing out. But, for example, say I need to print out for 2-permutations of this 4-element array, then I should only get the first 12 arrangements to print out. The code needs to account for r-permutations where the formula for permutations is [(# of elements)! / (# of elements - r)! ]. This is where I'm stuck.
– Fab J
Nov 5 at 21:48












If you want your recursion to stop after a "certain" number of permutation is computed, you can maintain a global counter of how many permutations are computed so far. And if you reach that number (say N), return the recursion thereafter. Please check edits.
– Sumit Jha
Nov 6 at 2:10




If you want your recursion to stop after a "certain" number of permutation is computed, you can maintain a global counter of how many permutations are computed so far. And if you reach that number (say N), return the recursion thereafter. Please check edits.
– Sumit Jha
Nov 6 at 2:10












Please accept the answer if it worked for you.
– Sumit Jha
Nov 6 at 10:27




Please accept the answer if it worked for you.
– Sumit Jha
Nov 6 at 10:27


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148835%2fhow-to-print-out-x-number-of-permutations-in-java%23new-answer', 'question_page');
}
);

Post as a guest