Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <climits>
- #include <algorithm>
- using namespace std;
- struct bucket {
- bool used;
- int min_val;
- int max_val;
- };
- int main(int argc, char const *argv[])
- {
- int n;
- cin >> n;
- int arr[n];
- if(n < 2)
- {
- cout << 0 << endl;
- exit(0);
- }
- for (int i = 0; i < n; ++i)
- {
- cin >> arr[i] ;
- }
- int mini = *std::min_element(arr, arr+n);
- int maxi = *std::max_element(arr, arr+n);
- //cout << "min " << mini << " max " << maxi << endl;
- //minimum gap needed between elements of a size n
- int bucketSize = max(1, ((maxi-mini)/(n-1)));
- //number of buckets needed
- int bucketNumber = (maxi - mini)/bucketSize + 1;
- //cout << bucketSize << " " << bucketNumber << endl;
- struct bucket b[bucketNumber];
- //initialise buckets
- for (int i = 0; i < bucketNumber; ++i)
- {
- b[i].used = false;
- b[i].min_val = INT_MAX;
- b[i].max_val = INT_MIN;
- }
- //place numbers in their appropriate buckets
- //the range of values in each bucket is such that
- //the maxDiff elements HAVE to be in different buckets
- for (int i = 0; i < n; ++i)
- {
- int ind = (arr[i] - mini)/ bucketSize;
- cout << ind << endl;
- b[ind].used = true;
- b[ind].min_val = min(arr[i], b[ind].min_val);
- b[ind].max_val = max(arr[i], b[ind].max_val);
- }
- int previousMax = mini, answer = 0;
- for (int i = 0; i < bucketNumber; ++i)
- {
- if(!b[i].used)
- continue;
- //cout << previousMax << endl;
- //cout << b[i].min_val << " " << b[i].max_val << endl;
- answer = max(answer, b[i].min_val - previousMax);
- previousMax = b[i].max_val;
- }
- cout << answer << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment