Count Smaller Prior Elements
Updated
Problem statement
Given an integer array nums, return an array answer where answer[i] is the number of indices j < i for which nums[j] < nums[i].
Equal earlier values are not smaller and must not be counted.
Function
countSmallerBefore(nums: int[]) → int[]Examples
Example 1
nums = [5,2,6,1,3]return = [0,0,2,0,2]The counts are based only on earlier positions: 0, 0, 2, 0, and 2.
Example 2
nums = [2,2,2]return = [0,0,0]Equal earlier values never count as smaller.
Constraints
1 <= nums.length <= 200000-10^9 <= nums[i] <= 10^9
More Tata Consultancy Services Online Assessment questions
Similar questions at other companies
- Photo AlbumHonda R&D · OA
- Rolling MetricsAkuna Capital · OAAkuna Capital interview questions
- Minimum Bridge TurnbacksBNSF Railway · OABNSF Railway interview questions
- Horizontal Pod AutoscalerSnowflake · OASnowflake interview questions
- Horizontal Pod AutoscalerRubrik · OARubrik interview questions
- Adjust Prices 🍱DoorDash · OADoorDash interview questions
- Photo AlbumFactSet · OAFactSet interview questions
- Maximum Profit from an Increasing Price TripletOdoo · OAOdoo interview questions
- Count Decreasing TripletsAirbnb · OAAirbnb interview questions
- Maximize Shipment Order by Sorting One WindowAmazon · OA