Find the maximum difference in an unsorted array with the index of max greater than min. array cant be sorted
Anonymous
This should do in single pass O(N), with min value being always before the max: int max_dist(const vector& data, size_t& start, size_t& end) { size_t min = 0; size_t max = 0; start = end = 0; if (data.size() == 0) { return 0; } for (size_t i = 1; i data[max]) { max = i; if (data[max] - data[min] > data[end] - data[start]) { // found new biggest distance start = min; end = max; } } } return data[end] - data[start]; }
Check out your Company Bowl for anonymous work chats.