Solved

EvenOdd challenge

Posted on 2016-08-14
10
81 Views
Last Modified: 2016-08-21
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;
}

Open in new window



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

How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
10 Comments
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 250 total points
Comment Utility
Go through array once, odd number found -> exchange with first position in array. Increase first replace position when exchange is done.
0
 
LVL 20

Assisted Solution

by:Amitkumar Panchal
Amitkumar Panchal earned 125 total points
Comment Utility
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;
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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 37

Expert Comment

by:Gerwin Jansen
Comment Utility
Nothing, work out an example and see what happens ;) Change to even numbers to see the difference.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 125 total points
Comment Utility
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

by:gudii9
Comment Utility
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;
		}

Open in new window


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	

Open in new window


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.
*/
}

Open in new window


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

Author Comment

by:gudii9
Comment Utility
any improvements, refinement or optimization to my code?
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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;
}

Open in new window


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;

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
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…

771 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

11 Experts available now in Live!

Get 1:1 Help Now