I have written the following code which takes the input of an array and returns it stretched.
For example
{18, 7, 9, 90}
should be returned as:
{9, 9, 4, 3, 5, 4, 45, 45}
This is the code I wrote:
import java.util.Arrays;
public class Stretching
{
public static void main(String[] args)
{
int[] list = {18, 7, 9, 90};
int[] list2 = stretch(list);
System.out.println(Arrays.toString(list));
System.out.println(Arrays.toString(list2));
}
public static int[] stretch(int[] array)
{
int[] stretched = new int[2*array.length];
for (int i = 0; i < array.length; i++)
{
if (array[i]%2 == 1)
{
stretched[i] = array[i]/2;
stretched[i] = array[i]/2 + 1;
}
else
{
stretched[i] = array[i]/2;
stretched[i] = array[i]/2;
}
}
return stretched;
}
}
Unfortunately, the output is like this:
[9, 3, 4, 45, 0, 0, 0, 0]
How can I fix this error?