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;
}
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
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE