Find Kth lexicographically smallest subarray - GeeksforGeeks

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 = 2Output: 5 15Explanation: 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 = 5Output: 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++
Python3
Javascript
// C++ Program of the above approach#include <bits/stdc++.h>using namespace std; // Function to find the power set of the// given arrayvector<vector<int> > powerSet(int* arr, int N){    int pow1 = pow(2, N);     // Stores the power set    vector<vector<int> > 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 arrayvector<int> kthSmallestSubset(    int* arr, int N, int K){    // Stores the power set    vector<vector<int> > powSet        = powerSet(arr, N);     // Sort the power set    // in lexicographic order    sort(powSet.begin(), powSet.end());     // Return Answer    return powSet[K - 1];} // Driver Codeint 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 << " ";    }}
# Python Program of the above approach # Function to find the power set of the# given arraydef 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 arraydef 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 Codearr = [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.
<script>// Javascript Program of the above approach // Function to find the power set of the// given arrayfunction powerSet(arr, N) {  let pow1 = Math.pow(2, N);   // Stores the power set  let v = [];   // Loop to iterate over all elements of  // the power set  for (let count = 0; count < pow1; count++) {     // Stores the current subset    let temp = [];    for (let j = 0; j < N; j++) {      if (count & (1 << j)) {        temp.push(arr[j]);      }    }     // Sorting the current subset    temp.sort();    if (count != 0) {      v.push(temp);    }  }   // Return Power Ser  return v;} // Function to find the// Kth lexicographic smallest// subset of the given arrayfunction kthSmallestSubset(arr, N, K) {  // Stores the power set  let powSet = powerSet(arr, N);   // Sort the power set  // in lexicographic order  powSet.sort();   // Return Answer  return powSet[K - 1];} // Driver Code let arr = [1, 2, 3, 4];let N = arr.length;let K = 5; let ans = kthSmallestSubset(arr, N, K);for (x of ans) {  document.write(x + " ");} // This code is contributed by gfgking.</script>
1 2 4 
Time Complexity: O(N * 2N)Auxiliary Space: O(2N)
rakeshsahni
gfgking
varshagumber28
simmytarika5
lexicographic-ordering
subarray
Arrays
Bit Magic
Mathematical
Arrays
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Window Sliding Technique
Trapping Rain Water
Reversal algorithm for array rotation
Move all negative numbers to beginning and positive to end with constant extra space
Program to find sum of elements in a given array
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
Cyclic Redundancy Check and Modulo-2 Division