#K95602. Fill Missing Values in Array
Fill Missing Values in Array
Fill Missing Values in Array
Given an array of integers, some of which may have missing values denoted by -1, your task is to fill in these missing values. For each occurrence of -1:
- If it appears at the beginning of the array, replace it with the value of its only neighbor (the second element).
- If it appears at the end of the array, replace it with the value of its only neighbor (the second-to-last element).
- Otherwise, replace it with the arithmetic mean (in (\LaTeX): (\frac{a_{i-1}+a_{i+1}}{2})) of its two immediate neighbors.
The resulting array should be printed with each number separated by a space. Note that the arithmetic mean should be computed in floating point. Assume that the input array will have at least one element and that the missing value replacements do not affect subsequent computations (i.e., process the array in a single left-to-right pass).
inputFormat
The first line of input contains an integer (n) representing the number of elements in the array. The second line contains (n) space-separated integers. Missing values are represented by -1.
outputFormat
Output the modified array in a single line with each number separated by a space. The filled values must use the floating point format when necessary.## sample
7
10 -1 5 6 -1 3 8
10 7.5 5 6 4.5 3 8