Swift Coding Test Course, Finding the K-th Number

Problem Description

This is a problem to find the K-th smallest number in a specific range when an array is given.
Given an integer array and two integers L and R, as well as K,
write an algorithm that returns the K-th smallest number among the numbers from L to R.

Input Format

    - The first line contains N (1 ≤ N ≤ 100,000) and Q (1 ≤ Q ≤ 100,000).
    - The second line contains N integers A₁, A₂, ..., Aₙ (-1,000,000,000 ≤ Aᵢ ≤ 1,000,000,000).
    - Following this, Q queries are given, each in the form of L, R, K.
    

Output Format

For each query, output the K-th smallest number.
Each output should be printed on a new line.

Example

    Input
    5 3
    1 5 2 6 3
    2 4 3
    1 5 2
    2 5 1

    Output
    5
    2
    3
    

Problem Solving Process

This problem fundamentally deals with interval queries.
While processing multiple queries, one must consider an optimal algorithm to find the K-th number each time in the interval.

Step 1: Problem Analysis

Given an array with N elements and Q queries,
each query must find the K-th number from the subarray of the array.
If the interval of the array is sorted every time to find the K-th number, it would take O(N * log(N)) time, which is
inefficient. The chances decrease as the number of queries increases.

Step 2: Designing a Solution Method

To solve this problem, the following methods can be used:
– **Peek Algorithm**: Sort the numbers in the interval to efficiently find the K-th number.
– **Counting Sort or Subarray Sorting**: Sort the subarray first and then find the K-th number.

Step 3: Algorithm Implementation

        func kthSmallest(arr: [Int], l: Int, r: Int, k: Int) -> Int {
            var subArray = Array(arr[l...r]) // Create subarray
            subArray.sort() // Sort
            return subArray[k - 1] // Return K-th smallest number
        }

        func solveProblem(arr: [Int], queries: [(Int, Int, Int)]) {
            for query in queries {
                let (l, r, k) = query
                let answer = kthSmallest(arr: arr, l: l, r: r, k: k)
                print(answer)
            }
        }
        

Step 4: Time Complexity Analysis

The above algorithm takes O(N log N) time for each query, so
when processing Q queries, the worst-case time complexity is O(Q * N log N).
This is very large, so we need to solve this problem more efficiently.

Efficient Solution Method: Segment Tree or Fenwick Tree

An efficient method could utilize a segment tree or a Fenwick tree to find the K-th number
in each interval. However, this part will not be covered here.

Step 5: Conclusion

We have examined the process of solving the problem of finding the K-th number.
Although approached by sorting the interval for each query,
using a more efficient method can lead to faster query processing.
This will be covered in the next lecture.

Conclusion

Understanding the problem accurately and designing an efficient algorithm is very important
in solving coding test problems.
Try various methods and establish your own approach.
The next lecture will cover more advanced topics such as the application of data structures.
Thank you!