This can be done at compile time with this code (keep in mind you need latest GCC or Clang, won't work in MSVC2k12 at this time). It makes it compile slow but it is instant at run-time.
#include <iostream>
constexpr int array[10] = { 1, 0, 2, 3, 0, 2, 7, 1, 9, 2 };
template<int maxest, int index>
struct find_biggest_r {
enum { value = find_biggest_r<(array[index] > maxest ? array[index]:maxest),index-1>::value };
};
template<int maxest>
struct find_biggest_r<maxest,0> {
enum { value = (array[0] > maxest ? array[0] : maxest) };
};
template<int index>
struct find_biggest {
enum { value = find_biggest_r<array[index-1],index-2>::value };
};
int main()
{
std::cout << find_biggest<10>::value;
}
EDIT: Sorry this was for just the largest. For the kth largest, I need to add an argument or two, I will do it later today.