post4 challenge

Hi,

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

Psedo code description of approach :
1. create new arrray//not sure on size?
2. assign each element with new element arrays until not equal to 4 from backwars
3. if above true return till there
 4 if false break for loop

I wrote my code as below

public int[] post4(int[] nums) {
  
  
  
  
  int len =nums.length;
  int[] arr=new int[len];
  for(int i=len;i>0;i--){
    if(nums[i]!=4){
    arr[i]=nums[i];
    }
    else{
       break;
    }
  }
  return arr;





}

Open in new window


I am not passing all tests due to edge cases

Expected      Run            
post4([2, 4, 1, 2]) → [1, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 4 (line number:9)      X      
post4([4, 1, 4, 2]) → [2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 4 (line number:9)      X      
post4([4, 4, 1, 2, 3]) → [1, 2, 3]      Exception:java.lang.ArrayIndexOutOfBoundsException: 5 (line number:9)      X      
post4([4, 2]) → [2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 2 (line number:9)      X      
post4([4, 4, 3]) → [3]      Exception:java.lang.ArrayIndexOutOfBoundsException: 3 (line number:9)      X      
post4([4, 4]) → []      Exception:java.lang.ArrayIndexOutOfBoundsException: 2 (line number:9)      X      
post4([4]) → []      Exception:java.lang.ArrayIndexOutOfBoundsException: 1 (line number:9)      X      
post4([2, 4, 1, 4, 3, 2]) → [3, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 6 (line number:9)      X      
post4([4, 1, 4, 2, 2, 2]) → [2, 2, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 6 (line number:9)      X      
post4([3, 4, 3, 2]) → [3, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 4 (line number:9)      X      
other tests
X      
Your progress graph for this problem
How to improve my design, approach, code? please advise
LVL 7
gudii9Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
rrzConnect With a Mentor Commented:
Let's focus on your for loop.
		for (int i = 0; i < len-1; i++) {
			if (nums[i] == 4) {
				val=i;
				break;
			}
		}

Open in new window

That is exactly what you used in that other challenge called pre4.  But in that challenge we were searching  for the first 4.  In this challenge, we are searching for the last 4. We could find it in a number of ways. We could start our loop search at the end and step through backwards until we find a 4.  Another way is to create a variable to hold the value of  the index of the last 4. That second way is similar to what I did in my solution to the twoTwo challenge that we a few days ago.  In that challenge, I created a variable to remember what happen earlier in the looping.
So, how do you want to proceed?
Step through backwards?
Create a variable to remember where the last 4 is?
Or maybe someone has another way?
0
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
Finish your "pre4" challenge before you worry about looking at this. Then you will have everything you should need to do this one!
2
 
ZoppoCommented:
Hint: Read and try to understand the thrown exceptions. This is a good exercise to learn debugging. Debugging is extremely important when someone wants to become a good programmer.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
krakatoaCommented:
I agree with mccarl, and also mostly agree with Zoppo.
0
 
ZoppoCommented:
Hm - why just mostly?
0
 
krakatoaCommented:
Because endless and countless previous explanations of how these things work have not been able to penetrate the asker's thinking.
0
 
ZoppoCommented:
:D
0
 
awking00Connect With a Mentor Commented:
Pseudo Code
Find the last index of 4
Find the last index of the array
The difference will be the length of the new array
Then populate it starting with the last index of 4 plus 1
0
 
rrzCommented:
mccarl showed  you a useful method in the API at  
https://www.experts-exchange.com/questions/28962285/pre4-challenge.html#a41753818   
Look at the API
http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html   
directly below the copyOf method there is the copyOfRange method which you can use in this challenge.
0
 
gudii9Author Commented:
public int[] post4(int[] nums) {
  int len = nums.length;
  int val=0;
		for (int i = 0; i < len-1; i++) {
			if (nums[i] == 4) {
				val=i;
				break;
			}
		}
		int[] arr = new int[val];
		for (int k = val; k < len-1; k++) {
		arr[k] =nums[k];
			}
		
		return arr;
		}

Open in new window

Expected      Run            
post4([2, 4, 1, 2]) → [1, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 1 (line number:12)      X      
post4([4, 1, 4, 2]) → [2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 0 (line number:12)      X      
post4([4, 4, 1, 2, 3]) → [1, 2, 3]      Exception:java.lang.ArrayIndexOutOfBoundsException: 0 (line number:12)      X      
post4([4, 2]) → [2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 0 (line number:12)      X      
post4([4, 4, 3]) → [3]      Exception:java.lang.ArrayIndexOutOfBoundsException: 0 (line number:12)      X      
post4([4, 4]) → []      Exception:java.lang.ArrayIndexOutOfBoundsException: 0 (line number:12)      X      
post4([4]) → []      []      OK      
post4([2, 4, 1, 4, 3, 2]) → [3, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 1 (line number:12)      X      
post4([4, 1, 4, 2, 2, 2]) → [2, 2, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 0 (line number:12)      X      
post4([3, 4, 3, 2]) → [3, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 1 (line number:12)      X      
other tests
X      

I am getting array index out of bound. please advise
0
 
gudii9Author Commented:
import java.util.Arrays;

public class Post4 {

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

	public static int[] post4(int[] nums) {
		int len = nums.length;
		int val = 0;
		for (int i = 0; i < len; i++) {
			if (nums[i] == 4) {
				val = i;
				break;
			}
		}
		int val2=len-(val+1);
		int[] arr = new int[val2];
		for (int k = val2; k < len; k++) {
			arr[k] = nums[k];
		}

		return arr;
	}

}

Open in new window


i modified bit getting same error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
      at Post4.post4(Post4.java:23)
      at Post4.main(Post4.java:8)
0
 
gudii9Author Commented:
arr[k] = nums[k];

Open in new window


compiler complaining about assigining 5 digit array on right into 3 digit array on left. not sure how to get last few digits after 4 into new array arr?
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Try..

arr[k-val] = nums[k];
0
 
gudii9Author Commented:
import java.util.Arrays;

public class PreFour {

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

	}

	public static int[] pre4(int[] nums) {
/*		You don't always have to try and do things in one go. Just use two loops....
		the first to find the index of the first 4, now you can create your new 
		array with the correct size, 
		and then use a second loop to copy the elements from the original to new array.*/
		int len = nums.length;		
		int val=0;
		for (int i = 0; i < len; i++) {
			if (nums[i] == 4) {
				val=i;
				break;
			}
		}
		int[] arr = new int[val];
		
		for (int k = 0; k < val; k++) {
		arr[k] =nums[k];
			}
		return arr;

}

}

Open in new window


pre4 works ok where as on similar lines post4 not woring
import java.util.Arrays;

public class Post4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arr = { 2, 4, 1, 0,8 };
		System.out.println("value is" + Arrays.toString(post4(arr)));
	}

	public static int[] post4(int[] nums) {
		int len = nums.length;
		int val = 0;
		for (int i = 0; i < len; i++) {
			if (nums[i] == 4) {
				val = i;
				break;
			}
		}
		int val2=len-(val);
		int[] arr = new int[val2];
		for (int k = (val2-1); k < len; k++) {
			arr[k] = nums[k];
		}

		return arr;
	}
	
	/*public int[] pre4(int[] nums) {
		  int len = nums.length;
		  int val=0;
				for (int i = 0; i < len; i++) {
					if (nums[i] == 4) {
						val=i;
						break;
					}
				}
				int[] arr = new int[val];
				for (int k = 0; k < val; k++) {
				arr[k] =nums[k];
					}
				
				return arr;
				}
*/

}

Open in new window


not sure where is the issue?
0
 
gudii9Author Commented:
import java.util.Arrays;

public class Post4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arr = { 2, 4, 1, 0,8 };
		System.out.println("value is" + Arrays.toString(post4(arr)));
	}

	public static int[] post4(int[] nums) {
		int len = nums.length;
		int val = 0;
		for (int i = 0; i < len; i++) {
			if (nums[i] == 4) {
				val = i;
				break;
			}
		}
		int val2=len-(val);
		int[] arr = new int[val2];
		for (int k = (val2-1); k < len; k++) {
			arr[k-val] = nums[k];
		}

		return arr;
	}
	
	/*public int[] pre4(int[] nums) {
		  int len = nums.length;
		  int val=0;
				for (int i = 0; i < len; i++) {
					if (nums[i] == 4) {
						val=i;
						break;
					}
				}
				int[] arr = new int[val];
				for (int k = 0; k < val; k++) {
				arr[k] =nums[k];
					}
				
				return arr;
				}
*/

}

Open in new window


gives below

value is[0, 0, 0, 8]
0
 
gudii9Author Commented:
arr[k-val] = nums[k];

Open in new window



what is the rule both left and right array needs to be same size?
0
 
gudii9Author Commented:
why k-val??
0
 
gudii9Author Commented:
import java.util.Arrays;

public class Post4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arr = { 2, 4, 1, 0,8 };
		System.out.println("value is" + Arrays.toString(post4(arr)));
	}

	public static int[] post4(int[] nums) {
	int len = nums.length;
	int val = 0;
	for (int i = 0; i < len; i++) {
		if (nums[i] == 4) {
			val = i;
			break;
		}
	}
	int val2=len-(val+1);
	int[] arr = new int[val2];
	for (int k = (val2-1); k < len; k++) {
		arr[k-(val+1)] = nums[k];
	}

	return arr;
	}
	
	/*public int[] pre4(int[] nums) {
		  int len = nums.length;
		  int val=0;
				for (int i = 0; i < len; i++) {
					if (nums[i] == 4) {
						val=i;
						break;
					}
				}
				int[] arr = new int[val];
				for (int k = 0; k < val; k++) {
				arr[k] =nums[k];
					}
				
				return arr;
				}
*/

}

Open in new window


i got correct result above

value is[1, 0, 8]

but coding bat failing
Expected      Run            
post4([2, 4, 1, 2]) → [1, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:13)      X      
post4([4, 1, 4, 2]) → [2]      [0, 4, 2]      X      
post4([4, 4, 1, 2, 3]) → [1, 2, 3]      [0, 0, 2, 3]      X      
post4([4, 2]) → [2]      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:13)      X      
post4([4, 4, 3]) → [3]      [4, 3]      X      
post4([4, 4]) → []      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:13)      X      
post4([4]) → []      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:13)      X      
post4([2, 4, 1, 4, 3, 2]) → [3, 2]      [0, 4, 3, 2]      X      
post4([4, 1, 4, 2, 2, 2]) → [2, 2, 2]      [0, 0, 0, 2, 2]      X      
post4([3, 4, 3, 2]) → [3, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:13)      X      
other tests
X      
Your progress graph for this problem

0
 
gudii9Author Commented:
public int[] post4(int[] nums) {
	int len = nums.length;
	int val = 0;
	for (int i = 0; i < len; i++) {
		if (nums[i] == 4) {
			val = i;
			break;
		}
	}
	int val2=len-(val+1);
	int[] arr = new int[val2];
	for (int k = (val+1); k < len; k++) {
		arr[k-(val+1)] = nums[k];
	}

	return arr;
	}

Open in new window


i am close now , failing few
Expected      Run            
post4([2, 4, 1, 2]) → [1, 2]      [1, 2]      OK      
post4([4, 1, 4, 2]) → [2]      [1, 4, 2]      X      
post4([4, 4, 1, 2, 3]) → [1, 2, 3]      [4, 1, 2, 3]      X      
post4([4, 2]) → [2]      [2]      OK      
post4([4, 4, 3]) → [3]      [4, 3]      X      
post4([4, 4]) → []      [4]      X      
post4([4]) → []      []      OK      
post4([2, 4, 1, 4, 3, 2]) → [3, 2]      [1, 4, 3, 2]      X      
post4([4, 1, 4, 2, 2, 2]) → [2, 2, 2]      [1, 4, 2, 2, 2]      X      
post4([3, 4, 3, 2]) → [3, 2]      [3, 2]      OK      
other tests
X      
Your progress graph for this problem
0
 
gudii9Author Commented:
failing more than one 4.

how to find last array and get elements after that to new array?
0
 
gudii9Author Commented:
i think backwards rings bell in my mind as easy let me try that
0
 
gudii9Author Commented:
public int[] post4(int[] nums) {
	int len = nums.length;
	int val = 0;
for (int i = len-1; i >=0; i--) {
		if (nums[i] == 4) {
			val = i;
			break;
		}
	}
	int val2=len-(val+1);
	int[] arr = new int[val2];
	for (int k = (val+1); k < len; k++) {
		arr[k-(val+1)] = nums[k];
	}

	return arr;
	}

Open in new window

Expected      Run            
post4([2, 4, 1, 2]) → [1, 2]      [1, 2]      OK      
post4([4, 1, 4, 2]) → [2]      [2]      OK      
post4([4, 4, 1, 2, 3]) → [1, 2, 3]      [1, 2, 3]      OK      
post4([4, 2]) → [2]      [2]      OK      
post4([4, 4, 3]) → [3]      [3]      OK      
post4([4, 4]) → []      []      OK      
post4([4]) → []      []      OK      
post4([2, 4, 1, 4, 3, 2]) → [3, 2]      [3, 2]      OK      
post4([4, 1, 4, 2, 2, 2]) → [2, 2, 2]      [2, 2, 2]      OK      
post4([3, 4, 3, 2]) → [3, 2]      [3, 2]      OK      
other tests
OK      

All Correct
boom...backward approach worked fine

Another way is to create a variable to hold the value of  the index of the last 4. That second way is similar to what I did in my solution to the twoTwo challenge that we a few days ago.  In that challenge, I created a variable to remember what happen earlier in the looping.

let me refresh memory and try that approach also
0
 
rrzCommented:
Your backwards stepping for loop is good.
0
 
gudii9Author Commented:
if challenge say for example middle 4 then second approach only works right not forward not backward?
0
 
rrzCommented:
I don't understand that last question. But let's stay focused on this challenge.
0
 
gudii9Author Commented:
twotwo challenge is not yet clear to me. But i will close this question as i got proper approach on this.
0
 
gudii9Author Commented:
That second way is similar to what I did in my solution to the twoTwo challenge that we a few days ago.  In that challenge, I created a variable to remember what happen earlier in the looping.

i am stil thinking, i was bit unclear on this way? can you please advise
0
 
rrzCommented:
This is similar to what you did above here, except it doesn't use a break statement.  When the for loop completes the fourIndex variable is equal to the index of the last four.
public int[] post4(int[] nums) {
  int fourIndex = 0;
  for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 4) {
        fourIndex = i;
    }
  }
  return Arrays.copyOfRange(nums, fourIndex + 1, nums.length); 
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.