Avatar of gudii9
gudii9
Flag for United States of America

asked on 

Fix45 challenge

Hi,

I am working on below challenge.
http://codingbat.com/prob/p125819

My psedo code of approach is
1. iterate given arry
2. find position of 4
3. set next position to 5 by keeping orignal value in buffere
4. create other for loop from  index position where we found 4 plus two and if that value is 5 replace with buffere
5. return modified array

public int[] fix45(int[] nums) {
  

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

}

Open in new window


i am failing below tests. please advise
Expected      Run            
fix45([5, 4, 9, 4, 9, 5]) → [9, 4, 5, 4, 5, 9]      [5, 4, 5, 4, 5, 9]      X      
fix45([1, 4, 1, 5]) → [1, 4, 5, 1]      [1, 4, 5, 1]      OK      
fix45([1, 4, 1, 5, 5, 4, 1]) → [1, 4, 5, 1, 1, 4, 5]      [1, 4, 5, 1, 1, 4, 5]      OK      
fix45([4, 9, 4, 9, 5, 5, 4, 9, 5]) → [4, 5, 4, 5, 9, 9, 4, 5, 9]      [4, 5, 4, 5, 9, 9, 4, 5, 9]      OK      
fix45([5, 5, 4, 1, 4, 1]) → [1, 1, 4, 5, 4, 5]      [5, 5, 4, 5, 4, 5]      X      
fix45([4, 2, 2, 5]) → [4, 5, 2, 2]      [4, 5, 2, 2]      OK      
fix45([4, 2, 4, 2, 5, 5]) → [4, 5, 4, 5, 2, 2]      [4, 5, 4, 5, 2, 2]      OK      
fix45([4, 2, 4, 5, 5]) → [4, 5, 4, 5, 2]      [4, 5, 4, 5, 2]      OK      
fix45([1, 1, 1]) → [1, 1, 1]      [1, 1, 1]      OK      
fix45([4, 5]) → [4, 5]      [4, 5]      OK      
fix45([5, 4, 1]) → [1, 4, 5]      [5, 4, 5]      X      
fix45([]) → []      []      OK      
fix45([5, 4, 5, 4, 1]) → [1, 4, 5, 4, 5]      [5, 4, 5, 4, 5]      X      
fix45([4, 5, 4, 1, 5]) → [4, 5, 4, 5, 1]      [4, 5, 4, 5, 1]      OK      
fix45([3, 4, 5]) → [3, 4, 5]      [3, 4, 5]      OK      
fix45([4, 1, 5]) → [4, 5, 1]      [4, 5, 1]      OK      
fix45([5, 4, 1]) → [1, 4, 5]      [5, 4, 5]      X      
fix45([2, 4, 2, 5]) → [2, 4, 5, 2]      [2, 4, 5, 2]      OK      
other tests
JavaJava EEProgrammingProgramming TheoryGame Programming

Avatar of undefined
Last Comment
gudii9

8/22/2022 - Mon