
Problem
Solution
Submissions
Permutations
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 10
Write a C program to generate all possible permutations of a given array of distinct integers. A permutation is a rearrangement of the elements of the array where each element appears exactly once. The order of the permutations in the output doesn't matter.
Example 1
- Input: nums = [1,2,3]
- Output: [[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]
- Explanation:
- For the array [1,2,3], we need to generate all possible arrangements.
- There are 3! = 6 possible permutations.
- We use backtracking to systematically generate each permutation.
- For the array [1,2,3], we need to generate all possible arrangements.
Example 2
- Input: nums = [0,1]
- Output: [[0,1], [1,0]]
- Explanation:
- For the array [0,1], we need to generate all possible arrangements.
- There are 2! = 2 possible permutations.
- The permutations are [0,1] and [1,0].
- For the array [0,1], we need to generate all possible arrangements.
Constraints
- 1 <= nums.length <= 6
- -10 <= nums[i] <= 10
- All the integers of nums are unique
- Time Complexity: O(n * n!), where n is the length of the array
- Space Complexity: O(n * n!) for storing all permutations
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use backtracking to systematically generate all permutations
- For each position in the permutation, try placing each unused number
- Keep track of which numbers have been used in the current permutation
- When you've placed all numbers, you've found a complete permutation
- Use recursion to build permutations element by element
- You can use a boolean array or set to track which elements have been used
- You can also swap elements in the array to generate permutations without extra space