Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 68 lines (54 sloc) 1.574 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
// http://programmingpraxis.com/2011/04/01/maximum-difference-in-an-array/

#include <iostream>
#include <cstdlib>
#include <ctime>

std::pair<int, int> fnQuaratic(const int anArray[], int nSize) {
std::pair<int, int> result(0, 0);
int nMax = 0;

for (int i = 0; i < nSize; ++i) {
for (int j = i; j < nSize; ++j) {
if (nMax < anArray[j] - anArray[i]) {
nMax = anArray[j] - anArray[i];
result.first = i;
result.second = j;
}
}
}

return result;
}
std::pair<int, int> fnLinear(const int anArray[], int nSize) {
std::pair<int, int> result(0, 0);
int nMax = 0, nSmallestPos = 0;
int nSmallest = anArray[0];

for (int i = 1; i < nSize; ++i) {
if (nSmallest > anArray[i]) {
nSmallest = anArray[i];
nSmallestPos = i;
}

if (nMax < anArray[i] - nSmallest) {
nMax = anArray[i] - nSmallest;
result.first = nSmallestPos;
result.second = i;
}
}

return result;
}

int main(int argc, char **argv) {
int anArray[10];
const int nArray = sizeof(anArray) / sizeof(int);
std::pair<int, int> result1, result2;

srand(time(0));

for (int nTest = 1; nTest <= 1000000; ++nTest) {
for (int i = 0; i < nArray; ++i) {
anArray[i] = rand() % 10 + 1;
}

if ((result1 = fnQuaratic(anArray, nArray)) != (result2 = fnLinear(anArray, nArray))) {
for (int j = 0; j < nArray; ++j) {
std::cout << anArray[j] << ' ';
}

std::cout << std::endl << result1.first << ", " << result1.second << std::endl;
std::cout << result2.first << ", " << result2.second << std::endl;
}
}

return 0;
}
Something went wrong with that request. Please try again.