The fastest way to solve this

Given an array S S which is a sorted array of integers.

What is the fastest running time to find the maximum x y x-y where x , y S x, y \in S

O ( n ) O(n) O ( n log n ) O(n\log n) O ( 1 ) O(1) O ( log n ) O(\log n)

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Andri Ys
Aug 5, 2014

to get maximum of x - y, x must be the greatest element in the array, and y must be the smallest element.

because the array is already sorted, you only need the first and the last element of the array to get those elements.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...