How Many Numbers Are Smaller Than the Current Number - Complete Solution Guide
How Many Numbers Are Smaller Than the Current Number is LeetCode problem 1365, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given the array nums , for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i] . Return the answer in an array. Example 1: Input: nums = [8,1,2,2,3] Output: [4,0,1,1,3] Explanation: For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3). For nums[1]=1 does not exist any smaller number than it. For nums[2]=2 there exist one smaller number than it (1). For
Detailed Explanation
This problem asks us to analyze an array of numbers, `nums`, and for each individual number within it, determine how many *other* numbers in that same array are strictly smaller. The crucial part is 'for each `nums[i]`' – meaning we need to produce an output array where each element `result[i]` corresponds to the count for `nums[i]`. The constraint `j != i` is important, ensuring we don't count a number against itself. Consider `nums = [8,1,2,2,3]` from the example. For `nums[0]=8`, we scan the rest of the array: `1` is smaller, `2` is smaller, the other `2` is smaller, and `3` is smaller. That's 4 smaller numbers. For `nums[1]=1`, there are no numbers in `[8,2,2,3]` that are smaller. So the count is 0. This process needs to be repeated for every element, maintaining their original indices in the result. The interesting aspect here isn't necessarily a hidden trick, but rather the emphasis on individual counts for each element while ensuring we only consider *other* distinct elements. The presence of duplicate numbers (like `2,2` in the example) means we can't simply count unique smaller values; we must count every instance of a smaller number.
Solution Approach
The provided solution takes a very direct, literal approach to the problem statement. It uses nested loops to achieve the required counting. The outer loop iterates through each element `nums[i]` in the input array, marking it as the 'current number' we're evaluating. For each `nums[i]`, an inner loop then iterates through *all* elements `nums[j]` in the array again. Inside this inner loop, the solution checks two conditions: first, `i != j` ensures that we are comparing `nums[i]` with a *different* number in the array, not itself. Second, `nums[j] < nums[i]` checks if the other number is strictly smaller than our current number. If both conditions are met, a counter (`count`) for `nums[i]` is incremented. Once the inner loop completes, `count` holds the total number of smaller elements for the current `nums[i]`, and this `count` is appended to our `result` list. This exhaustive pairwise comparison guarantees correctness by systematically checking every relevant pairing.
Step-by-Step Algorithm
- Initialize an empty array `result` of the same size as `nums` to store the counts.
- Iterate through the `nums` array using a `for` loop (outer loop).
- For each number `nums[i]`, initialize a counter `count` to 0.
- Iterate through the `nums` array again using another `for` loop (inner loop).
- Inside the inner loop, check if `j != i` and `nums[j] < nums[i]`. If both conditions are true, increment `count`.
- After the inner loop completes, append `count` to the `result` array.
- After the outer loop completes, return the `result` array.
Key Insights
- **Direct Iteration for Each Element**: The problem explicitly asks for a count 'for each `nums[i]`'. The solution directly translates this by using an outer loop to fix `nums[i]` and an inner loop to perform the required comparison across the entire array for that specific `nums[i]` element.
- **Strictly Smaller and Distinct Indices**: The conditions `nums[j] < nums[i]` and `i != j` are fundamental. The solution correctly implements both, ensuring that only numbers truly smaller than the current one, and not the current number itself, contribute to the count.
- **Brute-Force Simplicity**: This approach foregoes any complex sorting or hash map optimizations. Its 'elegance' lies in its straightforwardness: it directly simulates the problem's request without any abstraction, making it easy to understand and verify for correctness, particularly for smaller input sizes.
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, Sorting, Counting Sort.
Companies
Asked at: Accenture, tcs.