```
int maximumGap(vector<int> &num) {
if (num.size() < 2) {
return 0;
}
int min_val = num[0];
int max_val = num[0];
int n = num.size();
for (int i=0;i<n;i++) {
if (num[i] < min_val) {
min_val = num[i];
}
if (num[i] > max_val) {
max_val = num[i];
}
}
int gap = ceil((double)(max_val - min_val)/(n - 1));
int *mins = new int[n-1]();
for (int i=0;i<n-1;i++) {
mins[i] = -1;
}
int *maxs = new int[n-1]();
for (int i=0;i<n-1;i++) {
maxs[i] = -1;
}
for (int i=0;i<n;i++) {
int index = (num[i] - min_val)/gap;
if (mins[index] == -1 || mins[index] > num[i]) {
mins[index] = num[i];
}
if (maxs[index] == -1 || maxs[index] < num[i]) {
maxs[index] = num[i];
}
}
int max_gap = -1;
int prev_max = maxs[0];
for (int i=1;i<n-1;i++) {
if (mins[i] == -1) {
continue;
}
if (mins[i] - prev_max > max_gap) {
max_gap = mins[i] - prev_max;
}
prev_max = maxs[i];
}
return max_gap;
}
```