Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# zeroFront challenge

Posted on 2016-08-14
Medium Priority
211 Views
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;
}
``````

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
0
Question by:gudii9
• 4
• 3

LVL 38

Accepted Solution

Gerwin Jansen, EE MVE earned 2000 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

ID: 41756480
understood now. let me try
0

LVL 38

Expert Comment

ID: 41757049
OK
0

LVL 7

Author Comment

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)

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 38

Expert Comment

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

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;

}

``````

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 38

Expert Comment

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

## Featured Post

Question has a verified solution.

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

A solution for Fortify Path Manipulation.
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
###### Suggested Courses
Course of the Month11 days, 2 hours left to enroll