-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththirdLargest.c
79 lines (64 loc) · 1.42 KB
/
thirdLargest.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LARGEST 2
#define MIDDLE 1
#define THIRD_LARGEST 0
inline
void insertLargest(int n, int * topThree)
{
// Where n is greater than nums[2]
topThree[THIRD_LARGEST] = topThree[MIDDLE];
topThree[MIDDLE] = topThree[LARGEST];
topThree[LARGEST] = n;
}
inline
void insertMiddle(int n, int * topThree)
{
// Where n is greater than middle but smaller than largest
topThree[THIRD_LARGEST] = topThree[MIDDLE];
topThree[MIDDLE] = n;
}
inline
void insertSmallest(int n, int * topThree)
{
// where n is the third largest
topThree[THIRD_LARGEST] = n;
}
void placeNum(int n, int * topThree)
{
if (n > topThree[LARGEST])
{
insertLargest(n, topThree);
}
else if (n > topThree[MIDDLE])
{
insertMiddle(n, topThree);
}
else if(n > topThree[THIRD_LARGEST])
{
insertSmallest(n, topThree);
}
}
int thirdMax(int* nums, int numsSize) {
if (numsSize < 3)
{
if (numsSize < 2) return nums[0];
if (nums[0] > nums[1]) return nums[0];
else return nums[1];
}
int topThree[3];
int i;
for (i = 0; i < numsSize; i++)
{
placeNum(nums[i], topThree);
}
return topThree[THIRD_LARGEST];
}
int main(void)
{
int arr[] = {1,2,3,4,5,6};
int numsSize = 6;
printf("num: %d\n", thirdMax(arr, numsSize));
return 0;
}