packageorg.arpit.java2blog;importjava.util.ArrayList;importjava.util.List;publicclassPermutateArray{publicstaticvoidmain(String[] args){PermutateArray pa=newPermutateArray();int[] arr={10,20,30};List<List<Integer>> permute = pa.permute(arr);System.out.println("Permuations of array : [10, 20, 30] are:");System.out.println("=========================================");for(List<Integer> perm:permute){System.out.println(perm);}}publicList<List<Integer>>permute(int[] arr){List<List<Integer>> list =newArrayList<>();permuteHelper(list,newArrayList<>(), arr);return list;}privatevoidpermuteHelper(List<List<Integer>> list,List<Integer> resultList,int[] arr){// Base caseif(resultList.size()== arr.length){
list.add(newArrayList<>(resultList));}else{for(int i =0; i < arr.length; i++){if(resultList.contains(arr[i])){// If element already exists in the list then skipcontinue;}// Choose element
resultList.add(arr[i]);// ExplorepermuteHelper(list, resultList, arr);// Unchoose element
resultList.remove(resultList.size()-1);}}}}
packageorg.arpit.java2blog;importjava.util.ArrayList;importjava.util.List;publicclassPermutateArray{publicstaticvoidmain(String[] args){PermutateArray pa=newPermutateArray();int[] arr={10,20,30};List<List<Integer>> permute = pa.permute(arr);System.out.println("Permuations of array : [10, 20, 30] are:");System.out.println("=========================================");for(List<Integer> perm:permute){System.out.println(perm);}}publicList<List<Integer>>permute(int[] arr){List<List<Integer>> list =newArrayList<>();permuteHelper(list,newArrayList<>(), arr);return list;}privatevoidpermuteHelper(List<List<Integer>> list,List<Integer> resultList,int[] arr){// Base caseif(resultList.size()== arr.length){
list.add(newArrayList<>(resultList));}else{for(int i =0; i < arr.length; i++){if(resultList.contains(arr[i])){// If element already exists in the list then skipcontinue;}// Choose element
resultList.add(arr[i]);// ExplorepermuteHelper(list, resultList, arr);// Unchoose element
resultList.remove(resultList.size()-1);}}}}