From be11bffdc5c85bac29d3a092562ae77a6f4d0869 Mon Sep 17 00:00:00 2001 From: Riddhi Date: Mon, 27 Oct 2025 16:18:43 +0530 Subject: [PATCH] Implement function to find k-permutations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: You are given an array nums consisting of distinct integers, and an integer k. Your task is to generate all possible permutations of length k using the elements of nums. Each element can be used at most once in each permutation. Return all such permutations in any order. Output: Return all such permutations in any order. Constraints: 1≤n,k≤8 −50≤nums[i]≤50 --- Python/partial_k_permutation.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Python/partial_k_permutation.py diff --git a/Python/partial_k_permutation.py b/Python/partial_k_permutation.py new file mode 100644 index 00000000..4eb6ed8c --- /dev/null +++ b/Python/partial_k_permutation.py @@ -0,0 +1,13 @@ +def findKPerm(nums ,k): + result = [] + def helper(start): + if start == k: + result.append(nums[:k]) + return + for i in range(start, len(nums)): + nums[i], nums[start] = nums[start], nums[i] + helper(start + 1) + nums[i], nums[start] = nums[start], nums[i] + + helper(0) + return result