1 a 135 b 246 810 Merge two array in order and become one
1)
a = { 1,3,5}
b= {2,4,6, 8,10}
Merge two array in order and become one {1,2,3,4,5,6,8,10}
thanks a lot
Solution
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;
public class Program {
public static void main(String[] args)
{
int[] a = {1,3,5};
int[] b = {2,4,6,8,10};
int len_a = a.length;
int len_b = b.length;
int[] merge = new int[len_a+len_b];
int i=0,j=0,k=0;
while(i<len_a && j<len_b)
{
if(a[i]<=b[j])
{
merge[k] = a[i];
i++;
}
else
{
merge[k] = b[j];
j++;
}
k++;
}
while(i<len_a)
{
merge[k] = a[i];
i++;
k++;
}
while(j<len_b)
{
merge[k] = b[j];
k++;
j++;
}
System.out.print(\"Content of merged array is : \");
for(i=0;i<(len_a+len_b);i++)
System.out.print(merge[i]+\" \");
}
}
OUTPUT:
run:
Content of merged array is : 1 2 3 4 5 6 8 10 BUILD SUCCESSFUL (total time: 0 seconds)

