-2

I have anint[] a and int[] b of same size and I was trying to put them into new array int[] c and in c, the elements at odd indices would be a's elements and even elements would be b's. Heres what I have done so far.

int[] c = new int[a.length + b.length];
for (int i = 0; i < a.length; i++) {
    c[i] = a[i];
}
for (int i = 0; i < b.length; i++) {
    c[i + a.length] = b[i];
}

Here is an example of what I was asking: Given a:[0,1,2,3], b:[4,5,6,7], return [0,4,1,5,2,6,3,7].

How do I arrange them in even and odd order? I searched up everywhere but could not find a thread.

Turing85
  • 18,217
  • 7
  • 33
  • 58
iJusPan
  • 15
  • 8
  • basically, you are trying to merge two array. is it ? – Ravi Jan 12 '18 at 14:08
  • You may want to take a look at [`System.arraycopy(...)`](https://docs.oracle.com/javase/9/docs/api/java/lang/System.html#arraycopy-java.lang.Object-int-java.lang.Object-int-int-). "*How do I arrange them in even and odd order?*" - What is that suppose to mean? Can you give an example? – Turing85 Jan 12 '18 at 14:09
  • Yes Ravi, Merge & put array a in odd and array b in even order. – iJusPan Jan 12 '18 at 14:09
  • Turing85, here's an example of what I was asking given a:[0,1,2,3], b:[9,8,7,6] -> returns [0,9,1,8,2,7,3,6] – iJusPan Jan 12 '18 at 14:10
  • @iJusPan Your question is not clear. you need to merge them simply are you are checking something ? – Ravi Jan 12 '18 at 14:11
  • @iJusPan are you sure you want to put the elements of `a` on odd and the elements of `b` on even indices? This would mean that `c[0]` is an element from `b`. Also, what should be the behaviour if `a` and `b` have different sizes? – Turing85 Jan 12 '18 at 14:11
  • @iJusPan Also, are you sure both array would be same size ?? – Ravi Jan 12 '18 at 14:12
  • Well for now I was told that a and b will be the same size @Turing85 – iJusPan Jan 12 '18 at 14:13
  • Please include this information in your question by editing your question. Your solution is almost correct. You only need to modify the element acces to array `c` within both `for`-bodies. – Turing85 Jan 12 '18 at 14:15
  • @iJusPan But, what if both arrays are having different number of elements ? – Ravi Jan 12 '18 at 14:15
  • @Turing85 I edited the question. – iJusPan Jan 12 '18 at 14:16
  • @Ravi For now I was told that the array a and b will have same number of size. – iJusPan Jan 12 '18 at 14:16
  • @iJusPan but, what if they are having different size ?? what is the expected output ? – Ravi Jan 12 '18 at 14:18
  • HINT: a[0] and b[0] go into c[0] and c[1]; a[1] and b[1] go into c[2] and c[3]; a[2] and b[2] go into c[4] and c[5],.. And the pattern is: a[i] and b[i] go into c[i*2] and c[i*2+1]. – Kevin Anderson Jan 12 '18 at 14:23
  • Yes @KevinAnderson, that's what I meant but I am still lost on how to do it. – iJusPan Jan 12 '18 at 14:26
  • strangely similar to: https://stackoverflow.com/questions/48225181/how-to-combine-two-lists-without-using-foreach/48225477#comment83434490_48225477 – diginoise Jan 12 '18 at 14:27
  • Less subtle hint: `c[i*2] = a[i]; c[i*2+1] = a[i];`, Inside a loop... – Kevin Anderson Jan 12 '18 at 14:32

3 Answers3

1

Try making a int variable with 0

j = 0;

You can probably make a for loop like

for(i = 0; i < a.length; i++) 

and then you just do something like

c[j] = b[i];
j++;
c[j] = a[i];
j++;

since you said that the size of both array are the same, it shouldn't run into a size error.

Trey L.
  • 23
  • 5
1

Yo may do something like this if sometimes a and b are different size. The missing elements will always be 0 though.

public static void main(String[] args) {
   int[] a = {1,2,3,4};
   int[] b = {5,6,7,8,9};
   int[] c = new int[Math.max(a.length,b.length)*2];

   for(int i=0; i<c.length; i++){
       if(i%2==0 && i/2 < a.length){
           c[i]=a[i/2];
       }
       if(i%2==1 && i/2 < b.length){
           c[i]=b[i/2];
       }           
   }
    System.out.println(Arrays.toString(c));
} 
//[1, 5, 2, 6, 3, 7, 4, 8, 0, 9]
Eritrean
  • 15,851
  • 3
  • 22
  • 28
0

Try this

int[] c = new int[a.length+b.length];

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

            if(i%2==0) c[i]=b[i/2];

            else c[i]=a[(i-1)/2];

        }
Akash garg
  • 125
  • 6