Created
December 3, 2014 13:49
-
-
Save kamalbanga/d499d23ba7fdcb0ac45a to your computer and use it in GitHub Desktop.
Max Difference of Numbers in an Array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment