Solved

zeroFront challenge

Posted on 2016-08-14
7
80 Views
Last Modified: 2016-08-17
Hi,

I am working on below challenge

http://codingbat.com/prob/p193753

My psuedo code of the logic is
1. Loop through given array
2. check if the given element is zero.
3. if true bring to front.
4. else keep as it is.
5. return the modified array


I wrote my code as below
public int[] zeroFront(int[] nums) {
   for(int i=1;i<nums.length;i++){
     if(nums[i]==0){
       nums[0]=0;//check other elements till end and bring to front if 0
     }
     else{}
     
   }
  return nums;
}

Open in new window





I am not passing all tests
Expected      Run            
zeroFront([1, 0, 0, 1]) → [0, 0, 1, 1]      [0, 0, 0, 1]      X      
zeroFront([0, 1, 1, 0, 1]) → [0, 0, 1, 1, 1]      [0, 1, 1, 0, 1]      X      
zeroFront([1, 0]) → [0, 1]      [0, 0]      X      
zeroFront([0, 1]) → [0, 1]      [0, 1]      OK      
zeroFront([1, 1, 1, 0]) → [0, 1, 1, 1]      [0, 1, 1, 0]      X      
zeroFront([2, 2, 2, 2]) → [2, 2, 2, 2]      [2, 2, 2, 2]      OK      
zeroFront([0, 0, 1, 0]) → [0, 0, 0, 1]      [0, 0, 1, 0]      X      
zeroFront([-1, 0, 0, -1, 0]) → [0, 0, 0, -1, -1]      [0, 0, 0, -1, 0]      X      
zeroFront([0, -3, 0, -3]) → [0, 0, -3, -3]      [0, -3, 0, -3]      X      
zeroFront([]) → []      []      OK      
zeroFront([9, 9, 0, 9, 0, 9]) → [0, 0, 9, 9, 9, 9]      [0, 9, 0, 9, 0, 9]      X      
other tests
X
How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
  • 4
  • 3
7 Comments
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 500 total points
ID: 41756288
You should keep track of the amount of zero's you've found. Exchange a 0 found with the position in the array where you've found one.

So if you start with 1, 0, 1, 1, 0, 1 the array changes like this:

0,1,1,1,0,1 (one 0 found)
0,0,1,1,1,1 (two 0 found)
0
 
LVL 7

Author Comment

by:gudii9
ID: 41756480
understood now. let me try
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 41757049
OK
0
3 Use Cases for Connected Systems

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

 
LVL 7

Author Comment

by:gudii9
ID: 41758461
So if you start with 1, 0, 1, 1, 0, 1 the array changes like this:

0,1,1,1,0,1 (one 0 found)
i got above part not below part?
0,0,1,1,1,1 (two 0 found)
please advise

how to modify my psuedo code?
My psuedo code of the logic is
1. Loop through given array
2. check if the given element is zero.
3. if true bring to front.
4. else keep as it is.
5. return the modified array
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 41758484
Your pseudo code 3 - you say "bring to front", I mean "exchange 0 found with current position" - in essence the same. As long as you move the non zero number to the place where you've found a 0 then it's OK.

Like this:

1, 0, 1, 1, 0, 1 (start, my counter is 0)
0, 1, 1, 1, 0, 1 (one 0 found, my counter is 1)
0, 0, 1, 1, 1, 1 (two 0 found, my counter is 2)
0
 
LVL 7

Author Comment

by:gudii9
ID: 41759640
public int[] zeroFront(int[] nums) {
  int len=nums.length;
		int[] arr=new int[len];
		int front=0;
		int back=len-1;
		  for(int i=0;i<nums.length;i++){		    
		    if(nums[i]%2==0){	
		    	arr[front]=nums[i];
		    	front++;
		    	
		  }
		  else {
			  arr[back]=nums[i];
		    	back--;
		  }
		  }
		  return arr;
  
}




		

Open in new window


i just followed approach similar to evenOdd challenge and passing all tests.
Expected      Run            
zeroFront([1, 0, 0, 1]) → [0, 0, 1, 1]      [0, 0, 1, 1]      OK      
zeroFront([0, 1, 1, 0, 1]) → [0, 0, 1, 1, 1]      [0, 0, 1, 1, 1]      OK      
zeroFront([1, 0]) → [0, 1]      [0, 1]      OK      
zeroFront([0, 1]) → [0, 1]      [0, 1]      OK      
zeroFront([1, 1, 1, 0]) → [0, 1, 1, 1]      [0, 1, 1, 1]      OK      
zeroFront([2, 2, 2, 2]) → [2, 2, 2, 2]      [2, 2, 2, 2]      OK      
zeroFront([0, 0, 1, 0]) → [0, 0, 0, 1]      [0, 0, 0, 1]      OK      
zeroFront([-1, 0, 0, -1, 0]) → [0, 0, 0, -1, -1]      [0, 0, 0, -1, -1]      OK      
zeroFront([0, -3, 0, -3]) → [0, 0, -3, -3]      [0, 0, -3, -3]      OK      
zeroFront([]) → []      []      OK      
zeroFront([9, 9, 0, 9, 0, 9]) → [0, 0, 9, 9, 9, 9]      [0, 0, 9, 9, 9, 9]      OK      
other tests
OK

any improvement, modification, optimization to above code?
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 41760126
Looks OK to me, maybe use len in the for loop instead of nums.lenght
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java 8 lambda expresssions exception handling 3 75
Base1 Encode/Decode 3 68
MySQL  on Tomcat 8 30
java stored proc example 9 24
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…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 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

20 Experts available now in Live!

Get 1:1 Help Now