Solved

fix34  challenge

Posted on 2016-08-14
9
164 Views
Last Modified: 2016-08-24
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]

please advise
0
Comment
Question by:gudii9
  • 5
  • 3
9 Comments
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41756057
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 41756444
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?
0
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 250 total points
ID: 41756536
Don't move 3
Move 4 to right side of a 3
You may move other numbers
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 7

Author Comment

by:gudii9
ID: 41760248
not getting idea on best approach?
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41760812
don't understand what you mean, explain
0
 
LVL 40

Assisted Solution

by:noci
noci earned 250 total points
ID: 41764258
Moving does include exchanging  numbers.... Only 3's are fixed.... and there is a hint in the question as well.
There will be a 3 before any 4 anyway..., Also for every three there will be a 4....
so it can be done in a single pass through the array.

so
fix34([1, 3, 1, 4, 4, 3, 1]) 
becomes [ 1, 3, 4, 1, 4, 3, 1]
               -----
becomes  [1, 3, 4, 1, 1, 3, 4] ?
                     --    --

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41766305
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;
	
}

Open in new window

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;
	}

}

Open in new window

value-->[1, 3, 4, 1, 1, 3, 4]
1
 
LVL 7

Author Comment

by:gudii9
ID: 41769255
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;
}

Open in new window


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
0
 
LVL 7

Author Comment

by:gudii9
ID: 41769277
i see you are using mark to increment inner for loop with k as you are breaking out of it prematurely for k++ to take place later.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 53
Infrastructure development vs software development 3 79
Tagging and Merging on Branch 1 40
program arguments vs VM arguments 4 28
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question