Count Students Outside Their Sorted Positions
Updated
Problem statement
You are given an array heights, where heights[i] is the height of the student currently standing at index i.
The students should stand in nondecreasing height order. Return the number of indices whose current height differs from the height that would appear at that index after sorting the entire array.
Function
countStudentsOutOfPosition(heights: int[]) → intExamples
Example 1
heights = [1,1,4,2,1,3]return = 3The sorted array is [1,1,1,2,3,4]. Indices 2, 4, and 5 differ.
Example 2
heights = [5,1,2,3,4]return = 5After sorting, every index contains a different height.
Example 3
heights = [1,2,2,4]return = 0The students are already in nondecreasing order.
Constraints
1 <= heights.length <= 1000001 <= heights[i] <= 1000000000
More Concentric AI interview questions
- Kth Greater Element Index to the RightOA · Seen Sep 2026
- Find the Intersection Node of Two Linked ListsOA · Seen Sep 2026
- Maximum Profit Under a Bitwise OR LimitOA · Seen Aug 2026
- Minimum Weighted Manhattan Travel CostOA · Seen Aug 2026
- Rearrange a Linked List from Alternating EndsONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Macaron Order with Set DiscountsShopify · OAShopify interview questions
- Build the Task HierarchyPendo · OA
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Competition Winner and Runner-Up (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
- Rank Football TeamsCapital One · OACapital One interview questions
- Maximum Apples That Fit in a BoxVirtu Financial · OAVirtu Financial interview questions
- Rank Football TeamsPersona · OAPersona interview questions
- Meandering ArrayVerisk · OA