Solved

post4 challenge

Posted on 2016-08-08
28
82 Views
Last Modified: 2016-08-14
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
0
Comment
Question by:gudii9
  • 15
  • 5
  • 3
  • +3
28 Comments
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 125 total points
ID: 41748243
Finish your "pre4" challenge before you worry about looking at this. Then you will have everything you should need to do this one!
2
 
LVL 30

Expert Comment

by:Zoppo
ID: 41748366
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 41749169
I agree with mccarl, and also mostly agree with Zoppo.
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 41750030
Hm - why just mostly?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41750121
Because endless and countless previous explanations of how these things work have not been able to penetrate the asker's thinking.
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 41750153
:D
0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 125 total points
ID: 41753747
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
 
LVL 27

Expert Comment

by:rrz
ID: 41754092
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
 
LVL 7

Author Comment

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

Author Comment

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

Author Comment

by:gudii9
ID: 41754548
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
 
LVL 35

Expert Comment

by:mccarl
ID: 41754549
Try..

arr[k-val] = nums[k];
0
 
LVL 7

Author Comment

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

Author Comment

by:gudii9
ID: 41754551
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 7

Author Comment

by:gudii9
ID: 41754553
arr[k-val] = nums[k];

Open in new window



what is the rule both left and right array needs to be same size?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41754554
why k-val??
0
 
LVL 7

Author Comment

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

Author Comment

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

Author Comment

by:gudii9
ID: 41754557
failing more than one 4.

how to find last array and get elements after that to new array?
0
 
LVL 27

Accepted Solution

by:
rrz earned 250 total points
ID: 41754558
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
 
LVL 7

Author Comment

by:gudii9
ID: 41754564
i think backwards rings bell in my mind as easy let me try that
0
 
LVL 7

Author Comment

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

Expert Comment

by:rrz
ID: 41754567
Your backwards stepping for loop is good.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41754568
if challenge say for example middle 4 then second approach only works right not forward not backward?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41754569
I don't understand that last question. But let's stay focused on this challenge.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41755183
twotwo challenge is not yet clear to me. But i will close this question as i got proper approach on this.
0
 
LVL 7

Author Comment

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

Expert Comment

by:rrz
ID: 41755792
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now