kamalbanga
12/3/2014 - 1:49 PM

Max Difference of Numbers in an Array.

Max Difference of Numbers in an Array.

#include <iostream>
#include <vector>

using namespace std;

int main() {

    int n;
    cin >> n;

    vector<int> A(n);
    for (int i = 0; i < n; i++)
        cin >> A[i];

    int min = A[1] < A[0] ? A[1] : A[0];
    int maxDiff = A[1] - A[0];
    for (int i = 1; i < n; i++) {
        if (A[i] - min > maxDiff) 
            maxDiff = A[i] - min;
        if (A[i] < min)
            min = A[i];
    }

    cout << maxDiff;
}