-
Notifications
You must be signed in to change notification settings - Fork 1
/
largestdistance.cpp
89 lines (70 loc) · 2.33 KB
/
largestdistance.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// {"category": "Algorithm", "notes": "Find largest distance in integer array"}
#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
//------------------------------------------------------------------------------
//
// Given an unsorted array of integers, find the largest distance between
// indices i and j such that array[i] < array[j].
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//
// Implementation
//
//------------------------------------------------------------------------------
int LargestDistance(int* pIntArray, unsigned int size)
{
if (0 == size || nullptr == pIntArray)
{
return 0;
}
int distance = 0;
for (distance = size - 1; distance > 0; distance--)
{
if (pIntArray[0] != pIntArray[distance] ||
pIntArray[size - 1 - distance] != pIntArray[size - 1])
{
break;
}
}
return distance;
}
//------------------------------------------------------------------------------
//
// Demo execution
//
//------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
// distance = 0
int intArray1[] = { 1 };
cout << LargestDistance(intArray1, 1) << endl;
// distance = 0
int intArray2[] = { 1, 1 };
cout << LargestDistance(intArray2, 2) << endl;
// i = 0, j = 1, distance = 1
int intArray3[] = { 1, 2 };
cout << LargestDistance(intArray3, 2) << endl;
// i = 0, j = 4, distance = 4
int intArray4[] = { 1, 2, 3, 4, 5 };
cout << LargestDistance(intArray4, 5) << endl;
// i = 4, j = 0, distance = 4
int intArray5[] = { 5, 4, 3, 2, 1 };
cout << LargestDistance(intArray5, 5) << endl;
// i = 0, j = 3, distance = 3
int intArray6[] = { 1, 1, 1, 2, 1 };
cout << LargestDistance(intArray6, 5) << endl;
// i = 4, j = 1, distance = 3
int intArray7[] = { 1, 2, 1, 1, 1 };
cout << LargestDistance(intArray7, 5) << endl;
// i = 0, j = 2, distance = 2
int intArray8[] = { 1, 1, 2, 1, 1 };
cout << LargestDistance(intArray8, 5) << endl;
// distance = 0
int intArray9[] = { 1, 1, 1, 1, 1 };
cout << LargestDistance(intArray9, 5) << endl;
return 0;
}