Java > Array-2 > evenOdd (CodingBat Solution)

Problem:

Return an array that contains the exact same numbers as the given array, but rearranged so that all the even numbers come before all the odd numbers. Other than that, the numbers can be in any order. You may modify and return the given array, or make a new array.

evenOdd({1, 0, 1, 0, 0, 1, 1}) → {0, 0, 0, 1, 1, 1, 1}
evenOdd({3, 3, 2}) → {2, 3, 3}
evenOdd({2, 2, 2}) → {2, 2, 2}


Solution:

public int[] evenOdd(int[] nums) {
  int countE = 0;
  int countO = nums.length-1;
  int[] array = new int[nums.length];
  
  for (int i = 0; i < nums.length; i++) {
    if (nums[i] % 2 == 0) {
      array[countE] = nums[i];
      countE++;
    } 
    else {
      array[countO] = nums[i];
      countO--;
    }
  }
  return array;
}


5 comments :

  1. I know why my solution doesn't work, but I can't seem to figure out how to fix it. Can you find a way to fix my code?

    public int[] evenOdd(int[] nums) {
    int[] result = nums;
    for (int i = 0; i < nums.length - 1; i++)
    {
    if (nums[i] % 2 == 1)
    {
    result[nums.length - 1] = nums[i];
    for (int n = 0; n < nums.length - 1; n++)
    {
    result[i] = nums[i + 1];
    }
    }
    }
    return result;
    }

    ReplyDelete
  2. public int[] evenOdd(int[] nums) {
    int start = 0;
    for(int i = 0; i < nums.length; i++) {
    if(nums[i] % 2 == 0) {
    int temp = nums[i];
    nums[i] = nums[start];
    nums[start] = temp;
    start++;
    }
    }
    return nums;
    }

    ReplyDelete
  3. int len = nums.length;
    int temp = 0;
    int pos = 0;

    for (int i = 0; i < nums.length; i++) {
    if (nums[i] % 2 == 0) {
    temp = nums[pos];
    nums[pos] = nums[i];
    nums[i] = temp;
    pos++;
    }
    }

    return nums;

    ReplyDelete
  4. public int[] evenOdd(int[] nums) {
    int indexE=0;
    int indexO=nums.length-1;
    int[] arr=new int[nums.length];

    for (int i = 0; i < nums.length; i++) {
    if(nums[i]%2==0){
    arr[indexE]=nums[i];
    indexE++;
    }
    else{
    arr[indexO]=nums[i];
    indexO--;
    }
    }
    return arr;
    }

    ReplyDelete
  5. This is my solution. It is no the best and i think that it is a little dummie, but i think that is a little intuitive

    public int[] evenOdd(int[] nums) {
    int odd=0;
    int even=0;

    for(int i=0;i<nums.length;i++) {

    if(nums[i]%2==0) even++;
    if(nums[i]%2!=0) odd++;
    }

    int[]oddArr= new int[odd];
    int aux=0;

    for(int i=0;i<nums.length;i++) {

    if(nums[i]%2!=0)
    oddArr[aux++]=nums[i];

    }

    int [] evenArr= new int[even];
    int count=0;
    for(int j=0;j<nums.length;j++) {

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

    evenArr[count]=nums[j];
    count++;
    }
    }

    int [] finish= new int [evenArr.length+oddArr.length];

    int fls=0;
    for(int i=0;i<evenArr.length;i++) {

    finish[i]=evenArr[i];
    fls++;


    }




    for(int j=0;j<oddArr.length;j++) {

    finish[fls++]=oddArr[j];
    }




    return finish;



    }



    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact