Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.
Example
arr = [1, 3, 5, 7, 9]
The minimum sum is 1 + 3 + 5 + 7 = 16 and the maximum sum is 3 + 5 + 7 + 9 = 24. The function prints
16 24
Function Description
Complete the miniMaxSum function in the editor below.
miniMaxSum has the following parameter(s):
- arr: an array of 5 integers
Print two space-separated integers on one line: the minimum sum and the maximum sum of 4 of 5 elements.
Input Format
A single line of five space-separated integers.
Constraints
1 <= arr[i] <= 109
Output Format
Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than a 32 bit integer.)
Sample Input
1 2 3 4 5
Sample Output
10 14
Explanation
The numbers are 1, 2, 3, 4, and 5. Calculate the following sums using four of the five integers:
- Sum everything except 1, the sum is 2 + 3 + 4 + 5 = 14.
- Sum everything except 2, the sum is 1 + 3 + 4 + 5 = 13.
- Sum everything except 3, the sum is 1 + 2 + 4 + 5 = 12.
- Sum everything except 4, the sum is 1 + 2 + 3 + 5 = 11.
- Sum everything except 5, the sum is 1 + 2 + 3 + 4 = 10.
Hints: Beware of integer overflow! Use 64-bit Integer.
Need help to get started? Try the Solve Me First problem
1 2 3 4 5 10 14
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
char** split_string(char*);
void miniMaxSum(int arr_count, int* arr) {
int i, j;
long sum[5] = {0};
for (i = 0; i < arr_count; i++) {
for (j = 0; j < arr_count; j++) {
sum[i] = sum[i] + arr[j];
}
sum[i] = sum[i] - arr[i];
}
long min = sum[0], max = sum[0];
for (i = 0; i < arr_count; i++) {
if (sum[i] > max) {
max = sum[i];
}
if (sum[i] < min) {
min = sum[i];
}
}
printf("%ld %ld", min, max);
}
int main()
{
char** arr_temp = split_string(readline());
int* arr = malloc(5 * sizeof(int));
for (int i = 0; i < 5; i++) {
char* arr_item_endptr;
char* arr_item_str = *(arr_temp + i);
int arr_item = strtol(arr_item_str, &arr_item_endptr, 10);
if (arr_item_endptr == arr_item_str || *arr_item_endptr != '\0') { exit(EXIT_FAILURE); }
*(arr + i) = arr_item;
}
int arr_count = 5;
miniMaxSum(arr_count, arr);
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; }
size_t new_length = alloc_length << 1;
data = realloc(data, new_length);
if (!data) { break; }
alloc_length = new_length;
}
if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
}
data = realloc(data, data_length);
return data;
}
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;
}
1 2 3 4 5 10 14
No comments:
Post a Comment