#include "max_dist.h"

int max_dist(int *tab, unsigned int n)
{
    int min = tab[0];
    int max = tab[0];
    unsigned int i = 1;
    
    /*@
        loop assigns i, max, min;
        loop invariant I1: \at(i, LoopEntry) <= i <= n;
        loop invariant I2: INT_MIN /2 <= min <= max <= INT_MAX/2;
        loop invariant I3: \forall integer j; (\at(i, LoopEntry) <= j < i ==> max >= tab[j] >= min);
        loop invariant I4: \exists integer j; ( 0 < j < i ==> max == tab[j]);
        loop invariant I5: \exists integer j; ( 0 < j < i ==> min == tab[j]);
        loop variant n - i;
    */
    while (i < n)
    {
        if (tab[i] < min)
            min = tab[i];
        if (tab[i] > max)
            max = tab[i];
        i++;
    }
    //@ assert (INT_MIN / 2) <= min;
    //@ assert min <= (INT_MAX / 2);
    //@ assert (INT_MIN / 2) <= max;
    //@ assert max <= (INT_MAX / 2);
    return max - min;
}
