1. Problem Statement (Simple Explanation): You’re given an array nums that may contain duplicates . You must return all unique permutations of nums. The order of permutations in the output does not matter. 2. Examples: Example 1: Input: nums = [1,1,2] All permutations (with duplicates): [1,1,2] [1,2,1] [1,1,2] (duplicate) [1,2,1] (duplicate) [2,1,1] [2,1,1] (duplicate) Unique permutations: [ [1,1,2], [1,2,1], [2,1,1] ] Example 2: Input: nums = [1,2,3] All permutations are unique: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] 3. Approach – Backtracking with Duplicate Handling: This is similar to Problem 46 (Permutations) but nums may contain duplicates, so naive permutation will produce duplicate lists. We need to avoid generating duplicates instead of removing them afterwards. Standard trick: sor...
1. Problem Statement (Simple Explanation): You’re given an array nums of distinct integers. You must return all possible permutations of nums (all possible orderings). The answer can be returned in any order. 2. Examples: 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] ] Example 2: Input: nums = [0,1] Output: [ [0,1], [1,0] ] Example 3: Input: nums = [1] Output: [ [1] ] 3. Approach – Backtracking (DFS): This is a classic permutations problem. Intuition: We build permutations one position at a time: At each step, we choose one of the remaining unused numbers to place next. We continue until we have used all numbers → one complete permutation. Then we backtrack and try another choice. Since all integers in nums are distinct, we don’t need special duplicate handling. Two common im...