# Thread: Homework question on swapping array values

1. Member
Join Date
Jan 2016
Posts
19
Rep Power
0

## Homework question on swapping array values

Hello I am confused on how to get these test cases to adhere to my code.

Return an array that contains exactly the same numbers as the given array, but rearranged so that every 3 is immediately followed by a 4. Do not move the 3's, but every other number may move. The array contains the same number of 3's and 4's, every 3 has a number after it that is not a 3 or 4, and a 3 appears in the array before any 4.

fix34([1, 3, 1, 4]) → [1, 3, 4, 1]
fix34([1, 3, 1, 4, 4, 3, 1]) → [1, 3, 4, 1, 1, 3, 4]
fix34([3, 2, 2, 4]) → [3, 4, 2, 2]

That is my assignment. Here are the test cases

Expected

Run

fix34([1, 3, 1, 4]) → [1, 3, 4, 1] [1, 3, 4, 1]OK
fix34([1, 3, 1, 4, 4, 3, 1]) → [1, 3, 4, 1, 1, 3, 4] [1, 3, 4, 1, 4, 3, 1] X
fix34([3, 2, 2, 4]) → [3, 4, 2, 2] [3, 4, 2, 2] OK
fix34([3, 2, 3, 2, 4, 4]) → [3, 4, 3, 4, 2, 2] [3, 4, 3, 4, 2, 2] OK
fix34([2, 3, 2, 3, 2, 4, 4]) → [2, 3, 4, 3, 4, 2, 2] [2, 3, 4, 3, 4, 2, 2] OK
fix34([3, 1, 4]) → [3, 4, 1] [3, 4, 1] OK
fix34([3, 4, 1]) → [3, 4, 1] [3, 4, 1] OK
fix34([1, 1, 1]) → [1, 1, 1] [1, 1, 1] OK
fix34([1]) → [1] [1] OK
fix34([]) → [] [] OK
fix34([7, 3, 7, 7, 4]) → [7, 3, 4, 7, 7] [7, 3, 4, 7, 7] OK
fix34([3, 1, 4, 3, 1, 4]) → [3, 4, 1, 3, 4, 1] [3, 4, 1, 3, 4, 1] OK
fix34([3, 1, 1, 3, 4, 4]) → [3, 4, 1, 3, 4, 1] [3, 4, 1, 3, 4, 1]

and here is my code

Java Code:
```public int[] fix34(int[] nums) {

for(int i = 0; i < nums.length; i++){
if(nums[i] == 3){
for( int j = i; j<nums.length; j++){
if(nums[j]== 4)
{
int temp = nums[i+1];
nums[i+1]= nums[j];
nums[j] = temp;
}
}
}
}
return nums;
}```
Right now I can satisfy all test cases but the one with the X next to it. Can I get some input on how to overcome that test case?

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Homework question on swapping array values

As far as I can tell, the problem seems to be that your algorithm assumes that for each three, if there is a
four, it must be somewhere ahead of it in the array. So you simply cascade the array down from right to left,
filling in the numbers. In your case that didn't work because there are no fours to the right of the last three. So it

Regards,
Jim

3. Member
Join Date
Jan 2016
Posts
19
Rep Power
0

## Re: Homework question on swapping array values

So should I do the first sweep through, get all the 3s that come before 4s together, and then do another separate algorithm to sweep through looking for 4s before 3s and then rearrange accordingly? I feel that this should be able to be done with more concise code, I feel that I am making it too long and unnecessary.

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Homework question on swapping array values

The algorithm is up to you. But don't worry about concise code. Get it working with the existing test cases. You may want to add a few of your own.
When it is working, then you can focus on improving it. Trying to optimize code on the fly is a waste of energy and usually unproductive.

Regards,
Jim

5. Member
Join Date
Jan 2016
Posts
19
Rep Power
0

## Re: Homework question on swapping array values

Okay I have now come up with the solution. It is not pretty but it works. Thanks.
Java Code:
```public int[] fix34(int[] nums) {

for(int i = 0; i < nums.length; i++){
if(nums[i] == 3){
for( int j = i; j<nums.length; j++){
if(nums[j]== 4)
{
int temp = nums[i+1];
nums[i+1]= nums[j];
nums[j] = temp;
}
}
}
}

for(int i = 0; i< nums.length; i++){
if(i!=0){
if(nums[i] == 4 && nums[i-1] !=3){
for(int j=0; j<nums.length; j++){
if(nums[j] == 3){
int temp = nums[j+1];
nums[j+1]= nums[i];
nums[i] = temp;
}
}
}
}
}

return nums;
}```

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Homework question on swapping array values

I haven't checked out the algorithm. But try this one. fix34(1, 3, 1, 4, 4, 4, 3, 1, 3);

Regards,
Jim

7. Member
Join Date
Jan 2016
Posts
19
Rep Power
0

## Re: Homework question on swapping array values

I initially was looking at the test cases where 3 comes last and 4s come first in the array, however, when I look at the requirements of the assignment: "Return an array that contains exactly the same numbers as the given array, but rearranged so that every 3 is immediately followed by a 4. Do not move the 3's, but every other number may move. The array contains the same number of 3's and 4's, every 3 has a number after it that is not a 3 or 4, and a 3 appears in the array before any 4."

It excludes those test cases although I can always add it in for extra practice.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•