Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Segment & Fenwick trees

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Segment & Fenwick trees

Count of Smaller Numbers After Self

advancedlayer A6 · Segment & Fenwick trees5 tests

Given an integer array nums, return an array counts of the same length where counts[i] is how many numbers to the right of nums[i] are strictly smaller than it.

  • [5, 2, 6, 1] gives [2, 1, 1, 0]
  • Equal values do not count — only strictly smaller ones
  • An empty array gives an empty array
  • Values can be negative and can repeat, so index them by rank (their position in the sorted set of distinct values) rather than by value
  • Aim for O(n log n); the nested-loop count is the thing to beat

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen