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;
}


18 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
  6. public int[] evenOdd(int[] nums) {
    int pos = 0;
    for (int i = nums.length - 1; i >= 0 && pos < i; i--)
    {
    while (pos < i && nums[pos] % 2 == 0)
    {
    pos++;
    }
    if (nums[i] % 2 == 0)
    {
    int temp = nums[pos];
    nums[pos] = nums[i];
    nums[i] = temp;
    }
    }
    return nums;
    }

    ReplyDelete
  7. public int[] evenOdd(int[] nums) {
    //Two Pointer Solution One loop//
    int i = 0;
    for (int j = 1; j < nums.length; j++)
    {
    if (nums[j] % 2 == 0 && nums[i] % 2 != 0)
    {
    int temp = nums[i];
    nums[i] = nums[j];
    nums[j] = temp;
    i++;
    }
    else if (nums[i] % 2 == 0)
    {
    i++;
    }
    }
    return nums;
    }

    ReplyDelete
  8. public static int[] evenOdd(int[] nums) {
    int count = 0;
    int countOdd= 0;
    int[] numsNew = new int[nums.length];
    for(int i=0; i<nums.length; i++){
    if(nums[i]%2==0){
    numsNew[count]=nums[i];
    count++;
    }
    else {
    countOdd++;
    numsNew[nums.length-countOdd]=nums[i];
    }
    }
    return numsNew;
    }

    ReplyDelete
  9. public int[] evenOdd(int[] nums) {

    int countE = 0; //زوجي
    int countO = nums.length-1; //حط الفردي من الاخر للاول
    int[] arr = new int[nums.length];

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

    */

    ReplyDelete
  10. public int[] evenOdd(int[] nums) {

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

    }


    return nums;

    }

    ReplyDelete
  11. Found a way that is pretty clean and keeps the array in order if the need ever arose. Took the concept Nishi used and made the program work a little longer to keep things in order.

    public int[] evenOdd(int[] nums) {

    for(int i=0;i nums[i+1] % 2) { //If i is odd and i+1 is even
    int temp = nums[i];
    nums[i] = nums[i+1];
    nums[i+1] = temp;
    if (i != 0) i -= 2; \\ prevent i = -1 during code
    else i--;
    }
    }
    return nums;
    }

    ReplyDelete
  12. public int[] evenOdd(int[] nums) {

    int[] newArray = new int[nums.length];
    int iterations = 0;
    int i = 0;
    for (int x = 0; x < nums.length ; x++){


    if(iterations == 1 && nums[x] % 2 == 1){
    newArray[i] = nums[x];
    i++;
    }

    if(iterations == 0 && nums[x] % 2 == 0 ){
    newArray[i] = nums[x];
    i++;
    }

    if(x==nums.length - 1){
    iterations++;
    x=-1;
    }
    if (iterations == 2) {
    break;
    }
    }
    return newArray;
    }

    ReplyDelete
  13. public int[] evenOdd(int[] nums) {
    int[] newArray = new int[nums.length];
    int x = 0;

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


    }

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

    return newArray;
    }

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

      return nums;
      }

      Delete
  14. With Java Stream

    public static int[] evenOdd(int[] nums) {

    int[] even = Arrays.stream(nums).filter(i -> i % 2 == 0).toArray();
    int[] odd = Arrays.stream(nums).filter(i -> i % 2 != 0).toArray();
    return IntStream.concat(Arrays.stream(even), Arrays.stream(odd)).toArray();

    }

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

    return nums;
    }

    ReplyDelete
  16. public int[] evenOdd(int[] nums) {
    int a[] = new int[nums.length];
    int b[] = new int[nums.length];
    int odd = 0;
    int even = 0;

    for(int i=0 ; i<nums.length ; i++){
    if(nums[i]%2==0){
    a[even] = nums[i];
    even++;
    }
    else {
    b[odd] = nums[i];
    odd++;
    }
    }

    for(int j=0 ; j<odd ; j++){

    a[even] = b[j];
    even++;


    }
    return a;
    }

    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