
Problem
Solution
Submissions
Smaller Numbers After Self
Certification: Advanced Level
Accuracy: 0%
Submissions: 0
Points: 15
Write a C program that takes an array of integers and returns an array where each element represents the count of smaller numbers to the right of the current element. For each element in the input array, count how many elements to its right are smaller than it.
Example 1
- Input: nums = [5, 2, 6, 1]
- Output: [2, 1, 1, 0]
- Explanation:
Step 1: For 5, elements to right are [2, 6, 1]. Smaller than 5: [2, 1] → count = 2
Step 2: For 2, elements to right are [6, 1]. Smaller than 2: [1] → count = 1
Step 3: For 6, elements to right are [1]. Smaller than 6: [1] → count = 1
Step 4: For 1, no elements to right → count = 0
Example 2
- Input: nums = [1, 2, 3, 4]
- Output: [0, 0, 0, 0]
- Explanation:
Step 1: For 1, elements to right are [2, 3, 4]. None smaller than 1 → count = 0
Step 2: For 2, elements to right are [3, 4]. None smaller than 2 → count = 0
Step 3: For 3, elements to right are [4]. None smaller than 3 → count = 0
Step 4: For 4, no elements to right → count = 0
Constraints
- 1 ≤ nums.length ≤ 10^5
- -10^4 ≤ nums[i] ≤ 10^4
- Return an array of the same length as input
- Time Complexity: O(n log n)
- Space Complexity: O(n)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use merge sort with index tracking to count inversions efficiently
- Create an array of indices and sort based on values while tracking original positions
- During merge process, count how many elements from right array are smaller
- Maintain original indices to place counts in correct positions
- Use the divide and conquer approach of merge sort for efficient counting
- Build result array using the inversion counts calculated during merge