Find Kth lexicographically smallest subarray

• Difficulty Level : Expert
• Last Updated : 07 Dec, 2021

Given an array arr[] of N integers, the task is to find the Kth lexicographically smallest subset of the given array.

Example:

Input: arr[] = {5, 15}, K = 2
Output: 5 15
Explanation: The subsets of the given set in lexicographic order are {5}, {5, 15}, and {15}. Hence the 2nd smallest subset is {5, 15}.

Input: arr[] = {1, 2, 3, 4}, K = 5
Output: 1 2 4

Approach: The given problem can be solved by generating all the power set of the given array and thereafter sorting the subsets of the power set in lexicographic order. Hence, the subset at the Kth index of the sorted power set is the required answer.

Below is the implementation of the above approach:

C++

 `// C++ Program of the above approach``#include ``using` `namespace` `std;` `// Function to find the power set of the``// given array``vector > powerSet(``int``* arr, ``int` `N)``{``    ``int` `pow1 = ``pow``(2, N);` `    ``// Stores the power set``    ``vector > v;` `    ``// Loop to iterate over all elements of``    ``// the power set``    ``for` `(``int` `count = 0; count < pow1; count++) {` `        ``// Stores the current subset``        ``vector<``int``> temp;``        ``for` `(``int` `j = 0; j < N; j++) {``            ``if` `(count & (1 << j)) {``                ``temp.push_back(arr[j]);``            ``}``        ``}` `        ``// Sorting the current subset``        ``sort(temp.begin(), temp.end());``        ``if` `(count != 0) {``            ``v.push_back(temp);``        ``}``    ``}` `    ``// Return Power Ser``    ``return` `v;``}` `// Function to find the``// Kth lexicographic smallest``// subset of the given array``vector<``int``> kthSmallestSubset(``    ``int``* arr, ``int` `N, ``int` `K)``{``    ``// Stores the power set``    ``vector > powSet``        ``= powerSet(arr, N);` `    ``// Sort the power set``    ``// in lexicographic order``    ``sort(powSet.begin(), powSet.end());` `    ``// Return Answer``    ``return` `powSet[K - 1];``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 2, 3, 4 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``int` `K = 5;` `    ``vector<``int``> ans``        ``= kthSmallestSubset(arr, N, K);``    ``for` `(``auto` `x : ans) {``        ``cout << x << ``" "``;``    ``}``}`

Python3

 `# Python Program of the above approach` `# Function to find the power set of the``# given array``def` `powerSet(arr, N):``  ``pow1 ``=` `2` `*``*` `N` `  ``# Stores the power set``  ``v ``=` `[];` `  ``# Loop to iterate over all elements of``  ``# the power set``  ``for` `count ``in` `range``(pow1):` `    ``# Stores the current subset``    ``temp ``=` `[]``    ``for` `j ``in` `range``(N):``      ``if` `(count & (``1` `<< j)):``        ``temp.append(arr[j]);` `    ``# Sorting the current subset``    ``temp.sort();``    ``if` `(count !``=` `0``):``      ``v.append(temp);` `  ``# Return Power Ser``  ``return` `v;` `# Function to find the``# Kth lexicographic smallest``# subset of the given array``def` `kthSmallestSubset(arr, N, K):``  ` `  ``# Stores the power set``  ``powSet ``=` `powerSet(arr, N);` `  ``# Sort the power set``  ``# in lexicographic order``  ``powSet.sort();` `  ``# Return Answer``  ``return` `powSet[K ``-` `1``];` `# Driver Code``arr ``=` `[``1``, ``2``, ``3``, ``4``];``N ``=` `len``(arr)``K ``=` `5``;` `ans ``=` `kthSmallestSubset(arr, N, K);``for` `x ``in` `ans:``  ``print``(x, end``=``" "``);` `  ``# This code is contributed by gfgking.`

Javascript

 ``
Output
`1 2 4 `

Time Complexity: O(N * 2N)
Auxiliary Space: O(2N)

My Personal Notes arrow_drop_up