Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# EvenOdd challenge

Posted on 2016-08-14
Medium Priority
187 Views
Hi,

I am working on below challenge

http://codingbat.com/prob/p105771

My psuedo code of the logical approach is
1. loop throguh given array
2. find given elemt is even or odd.
3. if even group to front
4. if odd group to end
5. return array

I wrote my code as below

``````public int[] evenOdd(int[] nums) {
for(int i=0;i<nums.length;i++){

if(nums[i]%2==0){

}
else if(nums[i]%2==1){

}
}
return nums;
}
``````

I am not passing my tests as i just created skeleton code as i am not sure how to code this?

0
Question by:gudii9
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 38

Accepted Solution

Gerwin Jansen, EE MVE earned 1000 total points
ID: 41756457
Go through array once, odd number found -> exchange with first position in array. Increase first replace position when exchange is done.
0

LVL 21

Assisted Solution

Amitkumar Panchal earned 500 total points
ID: 41758115
Agree with Gerwin.

``````public int[] evenOdd(int[] nums) {
for(int i=0;i<nums.length-1;i++){
for(int j=i+1;j<nums.length;j++){
if(nums[i]%2==0){
break;
} else {
if(nums[j]%2==0){
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
break;
}
}
}

}
return nums;
}
``````
0

LVL 7

Author Comment

ID: 41758237
Go through array once, odd number found -> exchange with first position in array. Increase first replace position when exchange is done.

what i supposed to do when i see even number?
0

LVL 38

Expert Comment

ID: 41758271
Nothing, work out an example and see what happens ;) Change to even numbers to see the difference.
0

LVL 36

Assisted Solution

mccarl earned 500 total points
ID: 41759354
Another possible solution, would be to create a new array of the same size, loop through the original array, if the number is even fill the result array from the front, but if it is odd, fill the result array from the back. It does require a bit extra memory to store an extra array plus you need 3 index variables... One for the main loop index, one to track where you are filling the result array from the front and one to track where you are filling from the back. But the advantage is that you only need to loop through the array once, whereas the other solutions loop through many times.
0

LVL 7

Author Comment

ID: 41759583
i like above approach which is simple and passing all tests
``````public int[] evenOdd(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 if(nums[i]%2==1){
arr[back]=nums[i];
back--;
}
}
return arr;
}
``````

``````Expected	Run
evenOdd([1, 0, 1, 0, 0, 1, 1]) → [0, 0, 0, 1, 1, 1, 1]	[0, 0, 0, 1, 1, 1, 1]	OK
evenOdd([3, 3, 2]) → [2, 3, 3]	[2, 3, 3]	OK
evenOdd([2, 2, 2]) → [2, 2, 2]	[2, 2, 2]	OK
evenOdd([3, 2, 2]) → [2, 2, 3]	[2, 2, 3]	OK
evenOdd([1, 1, 0, 1, 0]) → [0, 0, 1, 1, 1]	[0, 0, 1, 1, 1]	OK
evenOdd([1]) → [1]	[1]	OK
evenOdd([1, 2]) → [2, 1]	[2, 1]	OK
evenOdd([2, 1]) → [2, 1]	[2, 1]	OK
evenOdd([]) → []	[]	OK
other tests
OK
``````

``````import java.util.Arrays;

public class EvenOdd {

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

public static int[] evenOdd(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 if(nums[i]%2==1){
arr[back]=nums[i];
back--;
}
}
return arr;
}

/*Another possible solution, would be to create a new array of the same size,
loop through the original array, if the number is even fill the result array from the
front, but if it is odd, fill the result array from the back. It does require a bit
extra memory to store an extra array plus you need 3 index variables... One for the main
loop index, one to track where you are filling the result array from the front and one to
track where you are filling from the back. But the advantage is that you only need to
loop through the array once, whereas the other solutions loop through many times.
*/
}
``````

value-->[0, 0, 0, 1, 1, 1, 1]
0

LVL 7

Author Comment

ID: 41759593
any improvements, refinement or optimization to my code?
0

LVL 7

Author Comment

ID: 41759600
``````public int[] evenOdd(int[] nums) {
for(int i=0;i<nums.length-1;i++){
for(int j=i+1;j<nums.length;j++){
if(nums[i]%2==0){
break;
} else {
if(nums[j]%2==0){
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
break;
}
}
}

}
return nums;
}
``````

above solution is not clear to me. why we are using two for loops one with counter i starting at 0other with counter j starting at 1?
and what is happening inside else loop for odd number case as below?
``````  int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
``````
0

LVL 7

Author Comment

ID: 41764729
i think i got it.

for every i which is even we are breaking.\

if i is odd checking consecutive elements is even then swapping them till end.

so {1,0.1.0.0,1,1} becomes {0,0.0.1.1.1.1}
0

## Featured Post

Question has a verified solution.

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

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.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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, 9 hours left to enroll