Alpha and Beta
Our friends Alpha and Beta found a magical treasure of Asgard. It consists of piles of gold coins. It is magical since if anyone tries to take a pile of coins, all the other piles of exactly coins (if they exist) disappear.
Alpha and Beta only have one turn each to choose a pile for themselves starting with Alpha. In one turn, a complete pile of gold coins can be chosen and since our friends are smart they will choose the pile with the maximum coins.
Find the number of coins Beta will get in his turn.
Function Description
Complete the alphaBeta function in the editor below. It should return an integer representing the number of coins Beta will get in his turn.
alphaBeta has the following parameter(s):
pile: an integer array
Input Format
- First line of the input contains a single integer , number of piles.
- Second line of the input contains space seperated integers, number of gold coins in each pile.
Constraints
- (where )
Output Format
- Single integer which is the number of coins Beta will receive in his first turn.
Sample Input 0
6
1 2 3 3 2 1
Sample Output 0
2
Explanation 0
Alpha will select a pile of coins in his turn. So due to magic the other pile of coins will disappear and Beta will be left with 4 piles:
1 2 2 1
Hence Beta will select a pile of coins.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
4
Explanation 1
Alpha will select a pile of coins in his turn. There are no other piles of coins so none disappear. Hence Beta will select a pile of coins.
xxxxxxxxxx
char* readline();
char* ltrim(char*);
char* rtrim(char*);
char** split_string(char*);
int parse_int(char*);
/*
* Complete the 'alphaBeta' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY pile as parameter.
*/
int alphaBeta(int pile_count, int* pile) {
}
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
int n = parse_int(ltrim(rtrim(readline())));
char** pile_temp = split_string(rtrim(readline()));
int* pile = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
int pile_item = parse_int(*(pile_temp + i));
*(pile + i) = pile_item;
}
int result = alphaBeta(n, pile);
fprintf(fptr, "%d\n", result);
fclose(fptr);
return 0;
}
char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);
while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);
if (!line) {
break;
}
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
break;
}
alloc_length <<= 1;
data = realloc(data, alloc_length);
if (!data) {
data = '\0';
break;
}
}
if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
data = realloc(data, data_length);
if (!data) {
data = '\0';
}
} else {
data = realloc(data, data_length + 1);
if (!data) {
data = '\0';
} else {
data[data_length] = '\0';
}
}
return data;
}
char* ltrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
while (*str != '\0' && isspace(*str)) {
str++;
}
return str;
}
char* rtrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
char* end = str + strlen(str) - 1;
while (end >= str && isspace(*end)) {
end--;
}
*(end + 1) = '\0';
return str;
}
char** split_string(char* str) {
char** splits = NULL;
char* token = strtok(str, " ");
int spaces = 0;
while (token) {
splits = realloc(splits, sizeof(char*) * ++spaces);
if (!splits) {
return splits;
}
splits[spaces - 1] = token;
token = strtok(NULL, " ");
}
return splits;
}
int parse_int(char* str) {
char* endptr;
int value = strtol(str, &endptr, 10);
if (endptr == str || *endptr != '\0') {
exit(EXIT_FAILURE);
}
return value;
}