A calculation is tried with several different settings. Each trial produces an array of n integers and an integer score. A larger score means a better result. The program must keep the array from a highest-scoring trial so that another part of the program can use it afterward. If several trials tie, any of their arrays is acceptable.
The trials are numbered from 0 to m - 1, where m >= 1 and n >= 1. Their results are produced by this supplied function:
int run_trial(int trial_number, int *out, int n);Each call writes all n result values into the array supplied as out, then returns that trial's score. It does not need the previous contents of out. It does not allocate or release that array, and it does not use the array again after returning. Scores may be any int values. You do not need to know how the calculation works or change run_trial.
Your task is to design a function with this interface:
int *keep_best(int m, int n);It must call run_trial exactly once for every trial number and return a pointer to a highest-scoring trial's array. The caller will use that array after keep_best returns and eventually release it with free.
Before the trials begin, keep_best may allocate at most two arrays of n integers with malloc. It may also use a few integer and pointer variables. It must reuse those arrays throughout the calculation: no further arrays may be allocated, and no trial's result may be copied from one array into the other. The writes performed by run_trial itself are allowed. Assume allocations succeed and allocation sizes fit their types.
When keep_best returns, only the returned array should remain allocated. The caller receives no other allocation to manage. You do not need to write code.
