Practice › Arrays
Count of Smaller Numbers After Self
Hard · Arrays · Asked at Google, Amazon, Microsoft, Apple
For every index i, count how many elements to the right of i are strictly smaller than a[i]. Print the counts.
Input
First line: n. Second line: n space-separated integers.
Output
n counts, space-separated.
Constraints
- 1 ≤ n ≤ 10^5
- −10^4 ≤ a[i] ≤ 10^4
Scan from the right with a Fenwick tree (binary indexed tree) over the values: the answer for i is the number of values already inserted that are smaller than a[i]. Merge sort that counts how many elements jump over each element also works. Both are O(n log n).
Example 1
Input
4 5 2 6 1
Output
2 1 1 0
Example 2
Input
2 -1 -1
Output
0 0
Hints
- Look at the array from right to left: you need "how many already-seen values are smaller than a[i]".
- Values are small (±10^4), so shift them to 1 … 20001 and use them as indices.
- A Fenwick tree answers prefix counts and point updates in O(log V).
Approach
Process i from n − 1 down to 0. Before inserting a[i], query the Fenwick tree for the number of inserted values ≤ a[i] − 1 — exactly the smaller elements to its right. Then add 1 at a[i]. With values shifted by +10001, every index is positive. (Merge sort counting the elements that move from the right half to the left half past each element is the alternative O(n log n) method.)
Complexity: O(n log V) time · O(V) space, V = 20001
Solution
C++ solution
#include <bits/stdc++.h>
using namespace std;
const int OFF = 10001, SZ = 20003;
int bit[SZ];
void add(int i) {
for (; i < SZ; i += i & -i) bit[i]++;
}
int sum(int i) {
int s = 0;
for (; i > 0; i -= i & -i) s += bit[i];
return s;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n), res(n);
for (auto &x : a) cin >> x;
for (int i = n - 1; i >= 0; i--) {
res[i] = sum(a[i] + OFF - 1);
add(a[i] + OFF);
}
for (int i = 0; i < n; i++) cout << res[i] << (i + 1 < n ? ' ' : '\n');
return 0;
}Python solution
import sys
data = list(map(int, sys.stdin.read().split()))
n = data[0]
a = data[1 : 1 + n]
OFF, SZ = 10001, 20003
bit = [0] * SZ
res = [0] * n
for i in range(n - 1, -1, -1):
j, s = a[i] + OFF - 1, 0
while j > 0:
s += bit[j]
j -= j & -j
res[i] = s
j = a[i] + OFF
while j < SZ:
bit[j] += 1
j += j & -j
print(*res)Java solution
import java.io.*;
public class Main {
static final int OFF = 10001, SZ = 20003;
static int[] bit = new int[SZ];
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
int n = (int) in.nval;
int[] a = new int[n], res = new int[n];
for (int i = 0; i < n; i++) {
in.nextToken();
a[i] = (int) in.nval;
}
for (int i = n - 1; i >= 0; i--) {
int s = 0;
for (int j = a[i] + OFF - 1; j > 0; j -= j & -j) s += bit[j];
res[i] = s;
for (int j = a[i] + OFF; j < SZ; j += j & -j) bit[j]++;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) sb.append(res[i]).append(i + 1 < n ? " " : "\n");
System.out.print(sb);
}
}Solve Count of Smaller Numbers After Self online · All practice problems