I need help with problem 13 For a sequence s a1 a2 an of n

I need help with problem 13?

For a sequence s: a_1, a_2, ...., a_n of n numbers, write an algorithm that determines two numbers a_i and a_j, i notequalto j, in s for which |a_i - a_j| is minimum Then show that your algorithm has time complexity theta(f(n)) for some common function f. A sequence s:a_1, a_2, .. a_n, of n distinct numbers is increasing if a_1

Solution

int min_diff(int a[], int n)
{
// non-decreasing order
sort(a, a+n);

# initialize max number in integers
int diff = INT_MAX;

for (int i=0; i<n-1; i++)
if (a[i+1] - a[i] < diff)
diff = a[i+1] - a[i];

return diff;
}

I need help with problem 13? For a sequence s: a_1, a_2, ...., a_n of n numbers, write an algorithm that determines two numbers a_i and a_j, i notequalto j, in

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site