0

The Problem: Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].

Return the array in the form [x1,y1,x2,y2,...,xn,yn].

I figured I could copy the first half in one array, the second half in another, and take turns putting them in a new array but I'm getting the error "java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3"... What can I do to fix that error?

The hint said I could use pointers, but what was wrong with the way I thought of going about it?

Here's my code:

class Solution {
    public int[] shuffle(int[] nums, int n) {
        int[] first = new int[n];
        int[] second = new int[n];
        int[] ans = new int[n+n];
        System.arraycopy(nums, 0, first, 0, n);
        System.arraycopy(nums, n + 1, second, 0, n - 1);
        for(int i = 0; i < (nums.length); i++){
            ans[i] = first[i]; //GETTING ERROR HERE
            ans[i + 1] = second[i];
        }
        return ans;
    }
}
j k
  • 1
  • 1
    If you want to become a professional software developer, or regularly develop software, you will need to have debugging skills. And, this is a good problem to practice debugging on. An IDE usually will have a debugger tool. It will allow you to set a break point, and then follow the code step-by-step, while monitoring the values of variables. – Old Dog Programmer Jul 12 '23 at 17:46
  • When referencing tasks that have web pages, such as problems from coding challenge and practice sites, please include a link to the page in the question. For reference, the LeetCode page seems to be this one: https://leetcode.com/problems/shuffle-the-array/ – Old Dog Programmer Jul 12 '23 at 17:54
  • What exactly is the purpose of `n`. It isn't defined in the question. – WJS Jul 12 '23 at 18:58
  • 1
    @WJS "Given the array nums consisting of **2n** elements" – Unmitigated Jul 12 '23 at 19:00
  • Off-topic: After you solve your current problem, see if you can think of a way to do this without creating intermediate arrays, such as `first` or `second`. – Old Dog Programmer Jul 14 '23 at 18:28
  • Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) You can also [search for other Java questions about `ArrayIndexOutOfBoundsException`](https://stackoverflow.com/search?q=%5Bjava%5D+arrayindexoutofboundsexception)` – Old Dog Programmer Jul 14 '23 at 20:12

4 Answers4

1

You get this exception because nums.length is two times n, so the index goes out of bounds when i reaches n.

You could solve this with just one loop of n iterations:

public int[] shuffle(int[] nums, int n) {
    int[] ans = new int[2 * n];
    for (int i = 0; i < n; i++) {
        ans[i * 2] = nums[i];
        ans[i * 2 + 1] = nums[n + i];
    }
    return ans;
}
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

At ans[i + 1] For i= (length of the array-1 ).you are assigning it to ans array .Since the indies starts from 0 ,at ans[n] there won’t be any assignment and the index will be out of bound.Solution is to run the loop n-2 times only

Kumar Shanoo
  • 125
  • 4
0

The array nums is of size 2n whereas first and second are of size n. Let's say nums has a size of 100, then first and second will have a size of 50. At some point your loop is trying to access indices that don't exist in first and second.

Also, you're going to have an exception in the next line too, since ans[i+1] will eventually try to access the index 100, being the maximum 99.

0

Firstly, your parameters for the second arraycopy are incorrect.

Consider the following; where n is 5.

int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int[] second = new int[n];
System.arraycopy(nums, n + 1, second, 0, n - 1);

Output

[7, 8, 9, 0, 0]

The srcPos and length parameter should both be n.

System.arraycopy(nums, n, second, 0, n);

Output

[6, 7, 8, 9, 0]

"... I'm getting the error "java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3"... What can I do to fix that error? ..."

You can utilize two loop parameters.
One to hold the index for the first and second array, and one to hold the index of the ans array.

public static int[] shuffle(int[] nums, int n) {
    int[] first = new int[n];
    int[] second = new int[n];
    int[] ans = new int[n+n];
    System.arraycopy(nums, 0, first, 0, n);
    System.arraycopy(nums, n, second, 0, n);
    for(int indexA = 0, indexB = 0; indexA < n; indexA++, indexB += 2){
        ans[indexB] = first[indexA];
        ans[indexB + 1] = second[indexA];
    }
    return ans;
}

Output

[1, 6, 2, 7, 3, 8, 4, 9, 5, 0]
Reilas
  • 3,297
  • 2
  • 4
  • 17