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?



Java: Tips of the Day

Different between parseInt() and valueOf() in java?

Well, the API for Integer.valueOf(String) does indeed say that the String is interpreted exactly as if it were given to Integer.parseInt(String). However, valueOf(String) returns a new Integer() object whereas parseInt(String) returns a primitive int.

If you want to enjoy the potential caching benefits of Integer.valueOf(int), you could also use this eyesore:

Integer k = Integer.valueOf(Integer.parseInt("123"))

Now, if what you want is the object and not the primitive, then using valueOf(String) may be more attractive than making a new object out of parseInt(String) because the former is consistently present across Integer, Long, Double, etc.

Ref: https://bit.ly/3vRuIPY