Under construction

Still being built. Things may change

Function to Find the K-th Number

Time limit0.2sMemory limit512 MB

Summary
Given an array of up to 5,000,000 integers and 1-based K, return the element at position K after sorting the array ascending.
Level

Medium4 of 10

Topics
Sorting, Brute force
Solved
No attempts yet

Problem

You are given a sequence A1,A2,…,ANA_1, A_2, \dots, A_N of size NN. Write a function that returns the KK-th number from the front when AA is sorted in ascending order.

The function you must write is as follows. NN is given as n, AA as a, and KK as k.

  • C, C11, C (Clang), C11 (Clang): int kth(int *a, int n, int k);
  • C++, C++11, C++14, C++17, C++ (Clang), C++11 (Clang), C++14 (Clang), C++17 (Clang): int kth(std::vector<int> &a, int k);
  • Java, Java (OpenJDK): int kth(int[] a, int k); (class name: Test)
  • PyPy2, PyPy3: def kth(a: list, k : int) -> int
  • Go: kth(a []int, k int) int

Constraints

  • 1≤N≤5 000 0001 \le N \le 5\,000\,000
  • 1≤K≤N1 \le K \le N
  • −109≤Ai≤109-10^9 \le A_i \le 10^9

Examples1

  1. Example 1

    Input
    1
    0
    1
    
    Expected output
    0