`{"id":"fd377cb5-8a09-42e4-993f-c97246e5d31b","name":"Lexicographically Smallest Subsequence","description":"1. Given an integer array nums and a positive integer k;\r\n2. return the lexicographically smallest subsequence of size k;","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. 1 &lt;= nums.length &lt;= 10^5\r\n2. 1 &lt;= nums[i] &lt;= 10^9\r\n3. 1 &lt;= k &lt;= nums.length","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\n\nusing namespace std;\n\nvector<int> smallest(vector<int>& nums, int k)\n{\n //write your code here\n}\n\nint main()\n{\n int n;\n cin>>n;\n vector<int> arr(n,0);\n \n for(int i=0; i<n;i++)\n {\n cin>>arr[i];\n }\n int k;\n cin>>k;\n \n vector<int> ans = smallest(arr,k);\n \n cout<<ans.size()<<endl;\n \n for(int i=0;i<ans.size();i++)\n {\n cout<<ans[i]<<endl;\n }\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static int[] smallest(int[] nums, int k) {\r\n // write your code here\r\n return null;\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(read.readLine());\r\n int arr[] = new int[n];\r\n for (int i = 0; i < n; i++)arr[i] = Integer.parseInt(read.readLine());\r\n int k = Integer.parseInt(read.readLine());\r\n int ans[] = smallest(arr, k);\r\n System.out.println(ans.length);\r\n\r\n for (int e : ans) {\r\n System.out.println(e);\r\n }\r\n\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"8\r\n2\r\n4\r\n3\r\n3\r\n5\r\n4\r\n9\r\n6\r\n4","sampleOutput":"4\r\n2\r\n3\r\n3\r\n4\r\n","questionVideo":"https://www.youtube.com/embed/wuwE7g8VvgQ","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"dcbebc3a-3eea-4956-96fe-141960dd9535","name":"Lexicographically Smallest Subsequence","slug":"lexicographically-smallest-subsequence","type":1}],"next":{"id":"b23f1312-f6f6-4645-8f78-4d5a494e3d19","name":"Lexicographically Smallest Subsequence MCQ","type":0,"slug":"lexicographically-smallest-subsequence-mcq"},"prev":{"id":"97b9f229-94d1-4219-b9f2-8fe368591df4","name":"Basic Calculator Iii","type":3,"slug":"basic-calculator-iii"}}}`

# Lexicographically Smallest Subsequence

1. Given an integer array nums and a positive integer k; 2. return the lexicographically smallest subsequence of size k;

`{"id":"fd377cb5-8a09-42e4-993f-c97246e5d31b","name":"Lexicographically Smallest Subsequence","description":"1. Given an integer array nums and a positive integer k;\r\n2. return the lexicographically smallest subsequence of size k;","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. 1 &lt;= nums.length &lt;= 10^5\r\n2. 1 &lt;= nums[i] &lt;= 10^9\r\n3. 1 &lt;= k &lt;= nums.length","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\n\nusing namespace std;\n\nvector<int> smallest(vector<int>& nums, int k)\n{\n //write your code here\n}\n\nint main()\n{\n int n;\n cin>>n;\n vector<int> arr(n,0);\n \n for(int i=0; i<n;i++)\n {\n cin>>arr[i];\n }\n int k;\n cin>>k;\n \n vector<int> ans = smallest(arr,k);\n \n cout<<ans.size()<<endl;\n \n for(int i=0;i<ans.size();i++)\n {\n cout<<ans[i]<<endl;\n }\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static int[] smallest(int[] nums, int k) {\r\n // write your code here\r\n return null;\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(read.readLine());\r\n int arr[] = new int[n];\r\n for (int i = 0; i < n; i++)arr[i] = Integer.parseInt(read.readLine());\r\n int k = Integer.parseInt(read.readLine());\r\n int ans[] = smallest(arr, k);\r\n System.out.println(ans.length);\r\n\r\n for (int e : ans) {\r\n System.out.println(e);\r\n }\r\n\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"8\r\n2\r\n4\r\n3\r\n3\r\n5\r\n4\r\n9\r\n6\r\n4","sampleOutput":"4\r\n2\r\n3\r\n3\r\n4\r\n","questionVideo":"https://www.youtube.com/embed/wuwE7g8VvgQ","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"dcbebc3a-3eea-4956-96fe-141960dd9535","name":"Lexicographically Smallest Subsequence","slug":"lexicographically-smallest-subsequence","type":1}],"next":{"id":"b23f1312-f6f6-4645-8f78-4d5a494e3d19","name":"Lexicographically Smallest Subsequence MCQ","type":0,"slug":"lexicographically-smallest-subsequence-mcq"},"prev":{"id":"97b9f229-94d1-4219-b9f2-8fe368591df4","name":"Basic Calculator Iii","type":3,"slug":"basic-calculator-iii"}}}` Editor

# Lexicographically Smallest Subsequence

medium

1. Given an integer array nums and a positive integer k; 2. return the lexicographically smallest subsequence of size k;

## Constraints

1. 1 <= nums.length <= 10^5 2. 1 <= nums[i] <= 10^9 3. 1 <= k <= nums.length

## Format

### Input

Input is managed for you

### Output

Output is managed for you

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}8 2 4 3 3 5 4 9 6 4```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}4 2 3 3 4 ```

Question Video

Discussions

Show Discussion

Related Resources 