1

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?

1
  • 1
    By thinking about what you're doing. Look at your code. Is it normal to initialize stretched[i] twice? Is it normal that the element from the original array at position 5 ends up as position 5 in the output array? Commented Jul 26, 2015 at 16:41

2 Answers 2

3

You are reusing the i index that refers to the positions in the original array. Instead, since you're stretching your array, the target indexes should be:

if (array[i]%2 == 1)
{
    stretched[2 * i] = array[i]/2 + 1;
    stretched[2 * i + 1] = array[i]/2;
}
else
{
    stretched[2 * i] = array[i]/2;
    stretched[2 * i + 1] = array[i]/2;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. Can you explain the problem that was happening?
@JohnSmith: That was the explanation. You were reusing i which mapped directly to the original array's index location. By multiplying it by a factor of 2 (which is what your new array size is, by the way), you are now ensuring that you're adding the right values in the right places.
1

There is a very big error in this code.

  if (array[i]%2 == 1)
  {
    // Here array[i]/2+1 to index i
    stretched[i] = array[i]/2 + 1;
    // Here array[i]/2 to index i
    stretched[i] = array[i]/2;
  }
  else
  {
    // Here array[i]/2 to index i
    stretched[i] = array[i]/2;
    // Here array[i]/2 to index i
    stretched[i] = array[i]/2;
  }

Here, you are assigning to the stretched array two values at the same index where as what you really want is to assign them to consecutive indices.

Rather you must modify your code like the following

import java.util.Arrays;

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[2 * i] = array[i]/2 + 1;
        stretched[2 * i + 1] = array[i]/2;
      }
      else
      {
        stretched[2 * i] = array[i]/2;
        stretched[2 * i + 1] = array[i]/2;
      }
    }
    return stretched;
  }
}

Hope you understand the bug!!

4 Comments

Not a fan of using j as a separate counter; mathematically speaking, using i * 2 gets you to the right spots. j only obscures this fact, making it tougher to reason about the behavior.
@Makoto I understand what you are trying to imply. But I used j to make it easy for understanding.
What I'm saying is that I'm not finding it easy to understand. It feels more like it'd serve to confuse than to truly guide.
@Makoto Got your point! Changed it to make the code more readable. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.