Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Count Smaller elements

Hard Acceptance 38.16% Points 40.00

Given an array arr containing non-negative integers. Count and return an array res where res[i] denotes the number of smaller elements on right side of arr[i].

Examples
Example 1

Example 1

Input: arr[] = [12, 1, 2, 3, 0, 11, 4]

Output: [6, 1, 1, 1, 0, 1, 0]

Explanation: There are 6 smaller elements right after 12. There is 1 smaller element right after 1. And so on.

Example 2

Example 2

Input: arr[] = [1, 2, 3, 4, 5]

Output: [0, 0, 0, 0, 0]

Explanation: There are 0 smaller elements right after 1. There are 0 smaller elements right after 2. And so on.

Hints
Hint 1
Expected Time Complexity: O(n log n)
Hint 2
Expected Auxiliary Space: O(n)
Constraints
  • 1 = arr.size() = 10^6
  • 0 = arr[i] = 10^8
Companies
Google
Topics
Array
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions