Solved

Fix45 challenge

Posted on 2016-08-23
15
134 Views
Last Modified: 2016-08-27
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
0
Comment
Question by:gudii9
  • 9
  • 4
  • 2
15 Comments
 
LVL 27

Expert Comment

by:d-glitch
ID: 41767723
What do all the cases you are failing have in common?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41767729
one extra 5
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41767737
one extra 5
That's what's wrong with your result.

But what do the input arrays have in common?
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 7

Author Comment

by:gudii9
ID: 41767743
same number of 4's and 5's
0
 
LVL 7

Author Comment

by:gudii9
ID: 41767747
also every 4 has a number after it that is not a 4
0
 
LVL 27

Accepted Solution

by:
d-glitch earned 500 total points
ID: 41767751
same number of 4's and 5's

also every 4 has a number after it that is not a 4
Both of those must be true for every case according to the problem definition.

What do the input arrays that you get wrong have in common that is different from the ones you get correct?   <== This is what debugging is all about.

How is this challenge harder than the fix34?  What is the difference?
As a hint:  Note that the challenge uses the words "this" and "version" twice.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41768155
they have 5 at beginning and failing

Below passes all tests

public int[] fix45(int[] nums) {
		  for (int x = 0; x < nums.length; x++) {
		    if (nums[x] == 4) {
		      for (int y = 0; y < nums.length; y++) {
		        if (nums[y] == 5) {
		          if (y > 0 && nums[y-1] != 4) {
		            int buffer = nums[x+1];
		            nums[x+1] = 5;
		            nums[y] = buffer;
		          } 
		          else if (y == 0) {
		            int buff = nums[x+1];
		            nums[x+1] = 5;
		            nums[y] = buff;
		          }
		                     
		        }
		      }
		    }
		  }
		  return nums;
		}

Open in new window


Expected      Run            
fix45([5, 4, 9, 4, 9, 5]) → [9, 4, 5, 4, 5, 9]      [9, 4, 5, 4, 5, 9]      OK      
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]      [1, 1, 4, 5, 4, 5]      OK      
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]      [1, 4, 5]      OK      
fix45([]) → []      []      OK      
fix45([5, 4, 5, 4, 1]) → [1, 4, 5, 4, 5]      [1, 4, 5, 4, 5]      OK      
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]      [1, 4, 5]      OK      
fix45([2, 4, 2, 5]) → [2, 4, 5, 2]      [2, 4, 5, 2]      OK      
other tests

package com.solution;

import java.util.Arrays;

public class Fix45 {

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

	}

	
	public static int[] fix45(int[] nums) {
		  for (int x = 0; x < nums.length; x++) {
		    if (nums[x] == 4) {
		      for (int y = 0; y < nums.length; y++) {
		        if (nums[y] == 5) {
		          if (y > 0 && nums[y-1] != 4) {
		            int buffer = nums[x+1];
		            nums[x+1] = 5;
		            nums[y] = buffer;
		          } 
		          else if (y == 0) {
		            int buff = nums[x+1];
		            nums[x+1] = 5;
		            nums[y] = buff;
		          }
		                     
		        }
		      }
		    }
		  }
		  return nums;
		}
}

Open in new window


fix45 -->[1, 4, 5, 1]

any improvement/refinement/alternate approach?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41768178
I don't know if my solution is any better than yours.  I create a new array. I fill it with the values from the input array. I return the new array.
public int[] fix45(int[] nums) {
          int[] arr = new int[nums.length];
  	    for (int z = 0; z < nums.length; z++){
	        if (nums[z] == 4) {
	          arr[z] = 4;
	          arr[z + 1] = 5;
	        }
        }
        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] != 4 && nums[k] != 5){
	               arr[j] = nums[k];
	               mark = k + 1;
	               break;
	           }
	          }
	        } 
        }
	      return arr;
}

Open in new window

0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41768779
How does your pseudo code compare with this:
b = 0
for  i= 0 to length
     if (nums[i] == 4) 
          buff[b] = nums[i+1]
          nums[i+1] = 5
          b ++

b = 0
for i= 0 to length	
     if (nums[i] == 5)
          if (i == 0)
               nums[i] = buff[b]
               b++
          else if (nums[i-1] != 4)
               nums[i] = buff[b]
               b++

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41769211
what is the purpose of

  mark = k + 1;

public int[] fix45(int[] nums) {
          int[] arr = new int[nums.length];
            for (int z = 0; z < nums.length; z++){
              if (nums[z] == 4) {
                arr[z] = 4;
                arr[z + 1] = 5;
              }
        }
        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] != 4 && nums[k] != 5){
                     arr[j] = nums[k];
                     mark = k + 1;
                     break;
                 }
                }
              }
        }
            return arr;
}

package com.solution;

import java.util.Arrays;

public class Fix46_2 {

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

	}

	public static int[] fix45(int[] nums) {
		
        int[] arr = new int[nums.length];
	    for (int z = 0; z < nums.length; z++){
	        if (nums[z] == 4) {
	          arr[z] = 4;
	          arr[z + 1] = 5;
	        }
      }
      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] != 4 && nums[k] != 5){
	               arr[j] = nums[k];
	               mark = k + 1;
	               break;
	           }
	          }
	        } 
      }
	      return arr;
}
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41769234
i see using mark and other for loop you are trying to fill non 4 and non 5 values from imput nums asrray to output new arr array.

I found your solution of filling new array is more straight forward?
why are  we starting second for loop from mark rather than 0?
public int[] fix45(int[] nums) {
          int[] arr = new int[nums.length];
            for (int z = 0; z < nums.length; z++){
              if (nums[z] == 4) {
                arr[z] = 4;
                arr[z + 1] = 5;
              }
        }
        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] != 4 && nums[k] != 5){
                     arr[j] = nums[k];
                     mark = k + 1;
                     break;
                 }
                }
              }
        }
            return arr;
}
0
 
LVL 7

Author Comment

by:gudii9
ID: 41769241
can i solve fix34 challenge with same above approach?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41769273
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
 
LVL 27

Expert Comment

by:rrz
ID: 41769725
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.
I used the mark variable to know where to begin looking for the next number to use to fill into the arr array. The increment in the loop(k++) is still used.  
There probably is room for improvement. But, when you closed this question, I stopped working on this.  Anyway, maybe adding some debug statements, will shed some light on my code. See below,
import java.util.Arrays;
public class Fix{
	public static void main(String[] args) {
		int[] input = {2, 3, 4, 6, 4, 7, 5, 5, 4, 8, 5};
		System.out.println("  input -->" + Arrays.toString(input));
		fix45(input);
	}
	public static int[] fix45(int[] nums) {
        int[] arr = new int[nums.length];
  	    for (int z = 0; z < nums.length; z++){
	        if (nums[z] == 4) {
	          arr[z] = 4;
	          arr[z + 1] = 5;
	        }
        }
		System.out.println(" output -->" + Arrays.toString(arr) + " (after first for loop)");
        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] != 4 && nums[k] != 5){
	               arr[j] = nums[k];
				   mark = k + 1;
				   System.out.println(" j is " + j + ", k is " + k + ", mark is " + mark);
				   System.out.println(" output -->" + Arrays.toString(arr));
	               break;
	            }   
	          }
	        } 
        }
	      return arr;	
}
}

Open in new window

The output is :    
  input -->[2, 3, 4, 6, 4, 7, 5, 5, 4, 8, 5]
 output -->[0, 0, 4, 5, 4, 5, 0, 0, 4, 5, 0] (after first for loop)
 j is 0, k is 0, mark is 1
 output -->[2, 0, 4, 5, 4, 5, 0, 0, 4, 5, 0]
 j is 1, k is 1, mark is 2
 output -->[2, 3, 4, 5, 4, 5, 0, 0, 4, 5, 0]
 j is 6, k is 3, mark is 4
 output -->[2, 3, 4, 5, 4, 5, 6, 0, 4, 5, 0]
 j is 7, k is 5, mark is 6
 output -->[2, 3, 4, 5, 4, 5, 6, 7, 4, 5, 0]
 j is 10, k is 9, mark is 10
 output -->[2, 3, 4, 5, 4, 5, 6, 7, 4, 5, 8]
0
 
LVL 7

Author Comment

by:gudii9
ID: 41773427
 input -->[2, 3, 4, 6, 4, 7, 5, 5, 4, 8, 5]
 output -->[0, 0, 4, 5, 4, 5, 0, 0, 4, 5, 0] (after first for loop)
 j is 0, k is 0, mark is 1
 output -->[2, 0, 4, 5, 4, 5, 0, 0, 4, 5, 0]
 j is 1, k is 1, mark is 2
 output -->[2, 3, 4, 5, 4, 5, 0, 0, 4, 5, 0]
 j is 6, k is 3, mark is 4
 output -->[2, 3, 4, 5, 4, 5, 6, 0, 4, 5, 0]
 j is 7, k is 5, mark is 6
 output -->[2, 3, 4, 5, 4, 5, 6, 7, 4, 5, 0]
 j is 10, k is 9, mark is 10
 output -->[2, 3, 4, 5, 4, 5, 6, 7, 4, 5, 8]

above output is more clear now. To understand again today taking some time around 1 hour even though i saw this day before yesterday?
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which non-HTML GUI front end to use with Java? 3 24
SequenceInputStream example 3 19
backup program with robocopy 6 47
Vector, list Questions R 6 32
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

828 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