Start learning free

LESSON 219 OF 410 · SORTING

Smallest K Integers in O(n log n) Time using JavaScript

Practice O(n log n) Read the full explanation

Given an array of positive integers nums, return the smallest k values, in any order you want.

Example:

Input: nums = [5, 9, 3, 6, 2, 1, 3, 2, 7, 5], k = 4
Output: [1, 2, 2, 3]
Explanation: Smallest number is 1, 2nd smallest is 2, 
            3rd smallest is 2, 4th smallest is 3
The result can be in any order, [2, 1, 3, 2] is also a correct answer.

Note:

For this lesson, your algorithm should run in O(n log n) time and use O(1) extra space besides the returned values, when sorting in place (sorting a copy of nums takes O(n) extra space).
(There are faster solutions which we will discuss in future lessons)


STEP 1 OF 2
    Ctrl + Enter to check

    JAVASCRIPT · SORTING

    Smallest K in JavaScript, explained

    Understanding the Problem

    The core challenge of this problem is to find the smallest k integers from an array of positive integers. This is a common problem in computer science, often referred to as the "k smallest elements" problem. It has applications in various fields such as data analysis, statistics, and machine learning where selecting a subset of data based on certain criteria is required.

    Potential pitfalls include misunderstanding the requirement to return the smallest k values in any order and not optimizing the solution to meet the O(n log n) time complexity constraint.

    Approach

    To solve this problem, we can consider the following approaches:

    Naive Solution

    A naive solution would be to sort the entire array and then return the first k elements. This approach is simple but not optimal in terms of time complexity.

    Optimized Solution

    Given the constraint of O(n log n) time complexity, the most straightforward optimized solution is to sort the array and then select the first k elements. Sorting the array takes O(n log n) time, and selecting the first k elements takes O(k) time, which is negligible compared to the sorting time.

    Algorithm

    Here is a step-by-step breakdown of the optimized algorithm:

    1. Sort the array in ascending order.
    2. Select the first k elements from the sorted array.
    3. Return the selected elements.

    Code Implementation

    // Function to find the smallest k integers
    function smallestKIntegers(nums, k) {
      // Step 1: Sort the array in ascending order
      nums.sort((a, b) => a - b);
      
      // Step 2: Select the first k elements
      return nums.slice(0, k);
    }
    
    // Example usage
    const nums = [5, 9, 3, 6, 2, 1, 3, 2, 7, 5];
    const k = 4;
    console.log(smallestKIntegers(nums, k)); // Output: [1, 2, 2, 3]
    

    Complexity Analysis

    The time complexity of the optimized solution is O(n log n) due to the sorting step. The space complexity is O(1) as we are not using any additional space that scales with the input size.

    Edge Cases

    Potential edge cases include:

    • k is greater than the length of the array: In this case, the function should return the entire sorted array.
    • k is 0: The function should return an empty array.
    • Array contains duplicate values: The function should handle duplicates correctly and include them in the result if they are among the smallest k values.

    Testing

    To test the solution comprehensively, consider the following test cases:

    // Test case 1: General case
    console.log(smallestKIntegers([5, 9, 3, 6, 2, 1, 3, 2, 7, 5], 4)); // Output: [1, 2, 2, 3]
    
    // Test case 2: k is greater than the length of the array
    console.log(smallestKIntegers([5, 9, 3], 5)); // Output: [3, 5, 9]
    
    // Test case 3: k is 0
    console.log(smallestKIntegers([5, 9, 3], 0)); // Output: []
    
    // Test case 4: Array contains duplicate values
    console.log(smallestKIntegers([1, 2, 2, 3, 3, 4], 3)); // Output: [1, 2, 2]
    

    Thinking and Problem-Solving Tips

    When approaching such problems, consider the following tips:

    • Understand the problem requirements and constraints thoroughly.
    • Start with a simple, naive solution to get a basic understanding.
    • Think about how to optimize the solution to meet the given constraints.
    • Break down the problem into smaller steps and solve each step methodically.
    • Practice solving similar problems to improve your problem-solving skills.

    Conclusion

    In this blog post, we discussed how to solve the problem of finding the smallest k integers from an array of positive integers. We explored a naive solution and an optimized solution that meets the O(n log n) time complexity constraint. We also covered edge cases, testing, and provided tips for approaching such problems. Understanding and solving such problems is crucial for developing strong problem-solving skills in computer science.

    Additional Resources

    For further reading and practice, consider the following resources:

    Keep going with JavaScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free