{"id":"8db88f54-64f0-4623-8d43-f7c3abd79ec8","name":"Find K Closest Elements","description":"1. Given a sorted integer array arr, two integers k and x, return the k closest integers to x in the array. The result should also be sorted in ascending order.\r\n2. An integer a is closer to x than an integer b if:\r\n |a - x| < |b - x|, or\r\n |a - x| == |b - x| and a < b","inputFormat":"Input is managed for you\r\n","outputFormat":"Output is managed for you\r\n","constraints":"1 &lt;= k &lt;= arr.length\r\n1 &lt;= arr.length &lt;= 10^4\r\narr is sorted in ascending order.\r\n-10^4 &lt;= arr[i], x &lt;= 10^4","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\n#include<iostream>\n\nusing namespace std;\n\nvector<int> findClosest(vector<int> &arr, int k, int x){\n //write your code here\n \n}\n\nint main(){\n int n;\n cin>>n;\n vector<int> arr;\n \n for(int i=0; i<n; ++i){\n int a;\n cin>>a;\n arr.push_back(a);\n }\n \n int k, x;\n cin>>k>>x;\n \n vector<int> ans=findClosest(arr, k, x);\n \n for(int ele: ans){\n cout<<ele<<\" \";\n }\n \n return 0;\n}"},"java":{"code":"import java.util.*;\r\nimport java.io.*;\r\n\r\npublic class Main {\r\n\r\n /*find ''k'' closest element to ''x'' and return answer list*/\r\n /*elements in answer list should be in ascending order*/\r\n public static ArrayList<Integer> findClosest(int[]arr,int k,int x) {\r\n //write your code here\r\n return null;\r\n }\r\n\r\n public static void main(String[]args) {\r\n\r\n //input work\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n\r\n int[]arr = new int[n];\r\n\r\n for(int i=0; i < n;i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n int k = scn.nextInt();\r\n int x = scn.nextInt();\r\n\r\n ArrayList<Integer>ans = findClosest(arr,k,x);\r\n\r\n for(int val : ans) {\r\n System.out.print(val + \" \");\r\n }\r\n\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"6\r\n10 20 30 40 50 60\r\n3 \r\n45","sampleOutput":"30 40 50","questionVideo":"https://www.youtube.com/embed/6AackEaa0Qs","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":"cb36811c-9cd7-4d80-aa52-ae9b8409862a","name":"Searching And Sorting For Intermediate","slug":"searching-and-sorting-for-intermediate-10001","type":0},{"id":"84e90ca1-f33f-4104-abed-aafa2baf0492","name":"Find K Closest Elements","slug":"find-k-closest-elements","type":1}],"next":{"id":"3c28f9c1-4d99-4186-a2db-1fa20fe2af45","name":"Find k closest elements","type":3,"slug":"find-k-closest-elements"},"prev":{"id":"4e577543-f59c-4ea9-af88-521e555691bd","name":"Max Sum In The Configuration MCQ","type":0,"slug":"max-sum-in-the-configuration-mcq"}}}

Find K Closest Elements

1. Given a sorted integer array arr, two integers k and x, return the k closest integers to x in the array. The result should also be sorted in ascending order. 2. An integer a is closer to x than an integer b if: |a - x| < |b - x|, or |a - x| == |b - x| and a < b

{"id":"8db88f54-64f0-4623-8d43-f7c3abd79ec8","name":"Find K Closest Elements","description":"1. Given a sorted integer array arr, two integers k and x, return the k closest integers to x in the array. The result should also be sorted in ascending order.\r\n2. An integer a is closer to x than an integer b if:\r\n |a - x| < |b - x|, or\r\n |a - x| == |b - x| and a < b","inputFormat":"Input is managed for you\r\n","outputFormat":"Output is managed for you\r\n","constraints":"1 &lt;= k &lt;= arr.length\r\n1 &lt;= arr.length &lt;= 10^4\r\narr is sorted in ascending order.\r\n-10^4 &lt;= arr[i], x &lt;= 10^4","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\n#include<iostream>\n\nusing namespace std;\n\nvector<int> findClosest(vector<int> &arr, int k, int x){\n //write your code here\n \n}\n\nint main(){\n int n;\n cin>>n;\n vector<int> arr;\n \n for(int i=0; i<n; ++i){\n int a;\n cin>>a;\n arr.push_back(a);\n }\n \n int k, x;\n cin>>k>>x;\n \n vector<int> ans=findClosest(arr, k, x);\n \n for(int ele: ans){\n cout<<ele<<\" \";\n }\n \n return 0;\n}"},"java":{"code":"import java.util.*;\r\nimport java.io.*;\r\n\r\npublic class Main {\r\n\r\n /*find ''k'' closest element to ''x'' and return answer list*/\r\n /*elements in answer list should be in ascending order*/\r\n public static ArrayList<Integer> findClosest(int[]arr,int k,int x) {\r\n //write your code here\r\n return null;\r\n }\r\n\r\n public static void main(String[]args) {\r\n\r\n //input work\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n\r\n int[]arr = new int[n];\r\n\r\n for(int i=0; i < n;i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n int k = scn.nextInt();\r\n int x = scn.nextInt();\r\n\r\n ArrayList<Integer>ans = findClosest(arr,k,x);\r\n\r\n for(int val : ans) {\r\n System.out.print(val + \" \");\r\n }\r\n\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"6\r\n10 20 30 40 50 60\r\n3 \r\n45","sampleOutput":"30 40 50","questionVideo":"https://www.youtube.com/embed/6AackEaa0Qs","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":"cb36811c-9cd7-4d80-aa52-ae9b8409862a","name":"Searching And Sorting For Intermediate","slug":"searching-and-sorting-for-intermediate-10001","type":0},{"id":"84e90ca1-f33f-4104-abed-aafa2baf0492","name":"Find K Closest Elements","slug":"find-k-closest-elements","type":1}],"next":{"id":"3c28f9c1-4d99-4186-a2db-1fa20fe2af45","name":"Find k closest elements","type":3,"slug":"find-k-closest-elements"},"prev":{"id":"4e577543-f59c-4ea9-af88-521e555691bd","name":"Max Sum In The Configuration MCQ","type":0,"slug":"max-sum-in-the-configuration-mcq"}}}
plane

Editor


Loading...

Find K Closest Elements

medium

1. Given a sorted integer array arr, two integers k and x, return the k closest integers to x in the array. The result should also be sorted in ascending order. 2. An integer a is closer to x than an integer b if: |a - x| < |b - x|, or |a - x| == |b - x| and a < b

Constraints

1 <= k <= arr.length 1 <= arr.length <= 10^4 arr is sorted in ascending order. -10^4 <= arr[i], x <= 10^4

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

6 10 20 30 40 50 60 3 45

Sample Output

30 40 50

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode