LeetCode problem#1470
Shuffle the Array
Solved in 1 languageJava
Solution
1class Solution
2{
3 int[] shuffle(int[] nums, int n)
4 {
5 int[] arr = new int[2 * n];
6
7 for (int k = 0, i = 0, j = n; k < 2 * n; k += 2, i++, j++)
8 {
9 arr[k] = nums[i];
10 arr[k + 1] = nums[j];
11 }
12
13 return arr;
14 }
15
16 void test()
17 {
18 int[] testdata1 = {2, 5, 1, 3, 4, 7};
19 shuffle(testdata1, 3);
20 assert testdata1[0] == 2;
21 assert testdata1[1] == 3;
22 assert testdata1[2] == 5;
23 assert testdata1[3] == 4;
24 assert testdata1[4] == 1;
25 assert testdata1[5] == 7;
26
27 int[] testdata2 = {1, 2, 3, 4, 4, 3, 2, 1};
28 shuffle(testdata2, 4);
29 assert testdata2[0] == 1;
30 assert testdata2[1] == 4;
31 assert testdata2[2] == 2;
32 assert testdata2[3] == 3;
33 assert testdata2[4] == 3;
34 assert testdata2[5] == 2;
35 assert testdata2[6] == 4;
36 assert testdata2[7] == 1;
37
38 int[] testdata3 = {1, 1, 2, 2};
39 shuffle(testdata3, 2);
40 assert testdata3[0] == 1;
41 assert testdata3[1] == 2;
42 assert testdata3[2] == 1;
43 assert testdata3[3] == 2;
44 }
45
46 public static void main(String[] args)
47 {
48 new Solution().test();
49 }
50}How it works
The provided code defines a Java class Solution with a method shuffle that rearranges elements in a given array nums according to a specified pattern, and a test() method to validate the correctness of the shuffle() method with some test cases. Let's break down the solution step by step.
Problem Explanation
The problem involves an array of integers nums of length 2*n, where the first half of the array consists of n elements and the second half also consists of n elements. The goal of the shuffle method is to rearrange the elements such that elements from both halves are interleaved in the resulting array.
Example Input:
nums = [2, 5, 1, 3, 4, 7]
n = 3Desired Output:
Output = [2, 3, 5, 4, 1, 7]The output array contains elements from the first half (2, 5, 1) and the second half (3, 4, 7) interleaved.
Key Points in the Code
-
Method Signature:
- The
shufflemethod takes an arraynumsand an integern, returning a new array of length2*n.
- The
-
Array Declaration:
int[] arr = new int[2 * n];- Here, a new array
arrof size2*nis created to hold the shuffled results.
- Here, a new array
-
Loop for Shuffling:
1for (int k = 0, i = 0, j = n; k < 2 * n; k += 2, i++, j++) 2{ 3 arr[k] = nums[i]; 4 arr[k + 1] = nums[j]; 5}- A single loop iterates through the indices of the resulting array
arrusingkwhich increments by 2 on each iteration. istarts from0and points to elements in the first half ofnums, whilejstarts fromnand points to elements in the second half ofnums.- In each iteration,
arr[k]is assigned an element from the first half ofnums, andarr[k + 1]is assigned an element from the second half.
- A single loop iterates through the indices of the resulting array
-
Return Statement:
- After the loop ends, the newly shuffled array
arris returned.
- After the loop ends, the newly shuffled array
Test Method
The test() method checks the correctness of the shuffle method by asserting the output for three different test cases:
-
Test Case 1:
int[] testdata1 = {2, 5, 1, 3, 4, 7}; shuffle(testdata1, 3);It checks that the output in
testdata1matches the expected interleaving. -
Subsequent Test Cases: Similar checks are performed for
testdata2andtestdata3with different inputs.
Main Method
The main() method instantiates the Solution class and calls the test() method to run the assertions.
Important Notes
- The assertions in the test method will throw an
AssertionErrorif the condition is false, indicating a failure in the output for that test case. shuffle()method does not modify the original array (nums), but instead creates a new array (arr) to store and return the result.
In summary, this solution accurately implements the required interleaving of the first and second halves of the input array through effective index management, ensuring that both halves are traversed accordingly and efficiently constructed into a new output array.
Written by AI from the code above, so double-check anything important.
Problem statements belong to LeetCode and are on leetcode.com. We're not affiliated with LeetCode.