fix34 challenge

Hi,

I am working on below challenge

http://codingbat.com/prob/p159339

I have not understood the description properly
Array-3 > fix34
prev  |  next  |  chance
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]

JavaJava EEProgramming Languages-OtherProgrammingSystem Programming

Last Comment
gudii9

8/22/2022 - Mon
Gerwin Jansen

You need to put the numbers 4 that exist in the given array after the numbers 3.

In the given array, numbers 3 are not followed by a 4.

So:

1,3,5,4 will become 1,3,4,5
gudii9

how
fix34([1, 3, 1, 4, 4, 3, 1]) becomes  [1, 3, 4, 1, 1, 3, 4] ?
or just move 4 next 3 without moving 3?
what happens to non 4 and non 3 numbers? where we have to move them?
Gerwin Jansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
gudii9

not getting idea on best approach?
Gerwin Jansen

don't understand what you mean, explain
SOLUTION
noci

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
gudii9

``````public int[] fix34(int[] nums) {

for (int z = 0; z < nums.length; z++)
if (nums[z] == 3) {
int buffer = nums[z + 1];
nums[z + 1] = 4;
for (int j = z + 2; j < nums.length; j++)
if (nums[j] == 4)
nums[j] = buffer;
}
return nums;

}
``````
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, 1, 3, 4]      OK
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]      OK
other tests
OK

above passes all tests
``````package com.solution;

import java.util.Arrays;

public class Fix34 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ar={1, 3, 1, 4, 4, 3, 1};
System.out.println("value-->"+Arrays.toString(fix34(ar)));

}

public static int[] fix34(int[] nums) {
for (int z = 0; z < nums.length; z++)
if (nums[z] == 3) {
int buffer = nums[z + 1];
nums[z + 1] = 4;
for (int j = z + 2; j < nums.length; j++)
if (nums[j] == 4)
nums[j] = buffer;
}
return nums;
}

}
``````
value-->[1, 3, 4, 1, 1, 3, 4]
gudii9

``````public int[] fix34(int[] nums) {

int[] arr = new int[nums.length];
for (int z = 0; z < nums.length; z++){
if (nums[z] == 3) {
arr[z] = 3;
arr[z + 1] = 4;
}
}
int mark = 0;
for (int j = 0; j < nums.length; j++){
if(arr[j] == 0){
for (int k = mark; k < nums.length; k++){
if(nums[k] != 3 && nums[k] != 4){
arr[j] = nums[k];
mark = k + 1;
break;
}
}
}
}
return arr;
}
``````

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, 1, 3, 4]      OK
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]      OK
other tests
OK
above similar to fix45 challenge passed all tests where fill new array simply with values which seems to me more efficient and easy way
gudii9