Java program(even no in even index and odd no in odd index)

Aanjaneya Shah :

So the question is to arrange an array where even number of an array is in even index and odd number of an array is in odd index.

Example 1: [3, 6, 2, 0, 0, 6, 9, 9, 89, 10, 1]

Output: [6, 3, 2, 9, 0, 9, 0, 89, 6, 1, 10]

Example 2: [1, 1, 1, 1, 2]

Output: [2, 1, null, 1, null, 1, null, 1]

Example 3: [2, 2, 2]

Output: [2, null, 2, null, 2]

Example 4: [1, 1, 1]

Output: [null, 1, null, 1, null, 1]

(if the no of even or odd numbers are less, null value is put)

I have tried this program but I am unable to pass all test cases. Can anyone can come with the correct answer in java language. Here is the code which i tried:


import java.io.*;
import java.util.*;
public class eveodd
{
    public static void main(String args[])throws IOException
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the no of elements");
        int n=sc.nextInt();
        sc.nextLine();
        int p=0;
        int a[]=new int[n]; 
        System.out.println("Enter the elements");
        for(int i=0;i<n;i++)
        {
            a[i]=sc.nextInt();
            sc.nextLine();
        }

        if(n%2==0)
        {
            p=n;
        }
        else
        {
            p=n+1;
        }
        Integer b[]=new Integer[100];

        int c=0;int d=1;
        for(int i=0;i<n;i++)
        {
                if(a[i]%2==0)
                {
                    b[c]=a[i];
                    c+=2;
                }
                if(a[i]%2!=0)
                {
                    b[d]=a[i];
                    d+=2;
                }


        }
        for(int i=0;i<d;i++)
        {
            System.out.println(b[i]);
        }
    }
}

What i am doing is filling an array with null values with even number in even index and odd number in odd index of the array. The problem i am facing is till how much index should i print the final(b[]) array. Also, is there any other way to find the size of the final (b[]) array as i have given it the size of 100. Any other solution or a different code (in java language) is welcome.

Andreas :

I would start by counting the number of odd and even values, then calculate the size of the array needed for the result.

static Integer[] evenOdd(int... input) {
    int countEven = 0, countOdd = 0;
    for (int value : input) {
        if ((value & 1) == 0)
            countEven++;
        else
            countOdd++;
    }
    Integer[] result = new Integer[Math.max(countEven * 2 - 1, countOdd * 2)];

    int idxEven = -2, idxOdd = -1;
    for (int value : input)
        result[(value & 1) == 0 ? (idxEven += 2) : (idxOdd += 2)] = value;
    return result;
}

Test

public static void main(String[] args) {
    test();
    test(1);
    test(2);
    test(3, 6, 2, 0, 0, 6, 9, 9, 89, 10, 1);
    test(1, 1, 1, 1, 2);
    test(2, 2, 2);
    test(1, 1, 1);
}
static void test(int... input) {
    System.out.println(Arrays.toString(input) + " -> " + Arrays.toString(evenOdd(input)));
}

Output

[] -> []
[1] -> [null, 1]
[2] -> [2]
[3, 6, 2, 0, 0, 6, 9, 9, 89, 10, 1] -> [6, 3, 2, 9, 0, 9, 0, 89, 6, 1, 10]
[1, 1, 1, 1, 2] -> [2, 1, null, 1, null, 1, null, 1]
[2, 2, 2] -> [2, null, 2, null, 2]
[1, 1, 1] -> [null, 1, null, 1, null, 1]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=30796&siteId=1