Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Java Array Exercises: Create all possible permutations of a given array of distinct integers

Java Array: Exercise-68 with Solution

Write a Java program to create all possible permutations of a given array of distinct integers.

Example:
Input :
nums1 = {1, 2, 3, 4}
nums2 = {1, 2, 3}
Output:
Possible permutations of the said array:
[1, 2, 3, 4]
[1, 2, 4, 3]
[1, 3, 2, 4]
[1, 3, 4, 2]
....
[4, 3, 2, 1]
[4, 3, 1, 2]
[4, 1, 3, 2]
[4, 1, 2, 3]
Possible permutations of the said array:
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 2, 1]
[3, 1, 2]

Sample Solution:

Java Code:

import java.util.*;
import java.util.List;

 public class solution {
 public static void main(String[] args) throws Exception {
    int[] nums1 = {1, 2, 3, 4};
	System.out.println("\nOriginal array: "+Arrays.toString(nums1));
    List<List<Integer>> result1 = new solution().permute(nums1);
	System.out.println("\nPossible permutations of the said array:");
	result1.forEach(System.out::println);
    int[] nums2 = {1, 2, 3};
	System.out.println("\nOriginal array: "+Arrays.toString(nums2));
    List<List<Integer>> result2 = new solution().permute(nums2);
	System.out.println("\nPossible permutations of the said array:");
	result2.forEach(System.out::println);	
	  }

  public List<List<Integer>> permute(int[] nums) {
    List<List<Integer>> result = new ArrayList<>();
    Permutation(0, nums, result);
    return result;
  }

  private void Permutation(int i, int[] nums, List<List<Integer>> result) {
    if (i == nums.length - 1) {
      List<Integer> list = new ArrayList<>();
      for (int n : nums) list.add(n);
      result.add(list);
    } else {
      for (int j = i, l = nums.length; j < l; j++) {
        int temp = nums[j];
        nums[j] = nums[i];
        nums[i] = temp;
        Permutation(i + 1, nums, result);
        temp = nums[j];
        nums[j] = nums[i];
        nums[i] = temp;
      }
    }
  }
}

Sample Output:

Original array: [1, 2, 3, 4]

Possible permutations of the said array:
[1, 2, 3, 4]
[1, 2, 4, 3]
[1, 3, 2, 4]
[1, 3, 4, 2]
[1, 4, 3, 2]
[1, 4, 2, 3]
[2, 1, 3, 4]
[2, 1, 4, 3]
[2, 3, 1, 4]
[2, 3, 4, 1]
[2, 4, 3, 1]
[2, 4, 1, 3]
[3, 2, 1, 4]
[3, 2, 4, 1]
[3, 1, 2, 4]
[3, 1, 4, 2]
[3, 4, 1, 2]
[3, 4, 2, 1]
[4, 2, 3, 1]
[4, 2, 1, 3]
[4, 3, 2, 1]
[4, 3, 1, 2]
[4, 1, 3, 2]
[4, 1, 2, 3]

Original array: [1, 2, 3]

Possible permutations of the said array:
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 2, 1]
[3, 1, 2]

Flowchart:

Flowchart: Create all possible permutations of a given array of distinct integers.

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to find subarray which has the largest sum in a given circular array of integers.
Next: Write a Java program to find minimum subarray sum of specified size in a given array of integers

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe