codekofi
← All problems

Problem 85

Medium

1 · Worked examples

0 / 3

Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.

solution()
returns

2 · Which problem is it?

Locked until you have predicted 3 outputs correctly.

The accepted solution

1int solution(vector<int> nums, int k) {
2
3 int n = nums.size();
4 int target = n - k;
5
6 int lo = 0;
7 int hi = n - 1;
8
9 while (true) {
10
11 int pivot = nums[hi];
12 int store = lo;
13
14 for (int i = lo; i < hi; i++) {
15
16 if (nums[i] < pivot) {
17 swap(nums[i], nums[store]);
18 store++;
19 }
20 }
21
22 swap(nums[store], nums[hi]);
23
24 if (store == target) {
25 return nums[store];
26 }
27
28 if (store < target) {
29 lo = store + 1;
30 } else {
31 hi = store - 1;
32 }
33 }
34}

Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.