Similar Problems

Similar Problems not available

Query Kth Smallest Trimmed Number - Leetcode Solution

Companies:

LeetCode:  Query Kth Smallest Trimmed Number Leetcode Solution

Difficulty: Medium

Topics: string sorting heap-priority-queue array  

Problem Statement:

Given an integer array nums representing the trimmed mean of a set of integers, return the number of elements in the original set after trimming. For example, if the original set has n elements, and after trimming the set has m elements where m <= n, then the trimmed mean is the sum of the remaining n-m elements divided by n-m.

The trimmed mean is defined as the mean of the elements that remain after removing the smallest k values and the largest k values from the set. The number k is an integer greater than or equal to 0.

Example:

Input: nums = [9,4,5,8,3], k = 1 Output: 4 Explanation: The trimmed mean of [4,5,8] is (4 + 5 + 8) / 3 = 17 / 3 = 5.66667. The smallest value is 3 and the largest value is 9. After removing k = 1 elements, the remaining elements are [4,5,8]. The trimmed mean is (4 + 5 + 8) / 3 = 17 / 3 = 5.66667. Therefore, the number of remaining elements is 4.

Solution:

One way to solve this problem is to sort the given array in ascending order and then remove the k smallest and k largest values from it. We can then calculate the sum of the remaining elements and divide it by the number of remaining elements to obtain the trimmed mean.

To find the kth smallest value, we can use the quickselect algorithm which has an average time complexity of O(n). The idea behind quickselect is to partition the array based on a pivot element such that all elements to the left of the pivot are less than or equal to the pivot and all elements to the right are greater than or equal to the pivot. We can then select the kth smallest value by recursively applying the partitioning algorithm to the left or right subarray depending on the value of k.

Once we have found the kth smallest and kth largest values, we can remove them from the array and compute the trimmed mean as described above.

Below is the Python code to solve the problem:

def quickselect(nums, k): left = 0 right = len(nums) - 1

while True:
    pivot_index = partition(nums, left, right)
    
    if pivot_index == k:
        return nums[k]
    elif pivot_index < k:
        left = pivot_index + 1
    else:
        right = pivot_index - 1

def partition(nums, left, right): pivot = nums[right] i = left - 1

for j in range(left, right):
    if nums[j] <= pivot:
        i += 1
        nums[i], nums[j] = nums[j], nums[i]
        
nums[i+1], nums[right] = nums[right], nums[i+1]

return i+1

def trimmed_mean(nums, k): n = len(nums) nums.sort()

kth_smallest = quickselect(nums, k)
kth_largest = quickselect(nums, n-k-1)

trimmed_nums = [x for x in nums if x > kth_smallest and x < kth_largest]

return len(trimmed_nums)

if name == 'main': nums = [9, 4, 5, 8, 3] k = 1 print(trimmed_mean(nums, k))

The time complexity of the above algorithm is O(nlogn) due to the sorting operation, however, the quickselect algorithm used to find the kth smallest and kth largest values has an average time complexity of O(n) making this solution optimal for most inputs.

Query Kth Smallest Trimmed Number Solution Code

1