Given two arrays, arr1 and arr2, that have been sorted in descending order, output an array which appends the values from both arr1 and arr2 while being sorted in descending order.
MyApproach
I found 2 -3 links helpful and I noticed that I have a similar kind of code here.But I am unable to get the expected output.
One of my Resources:How to merge two sorted arrays into a sorted array?
Can Anyone guide me what I am doing wrong.
I have made the code for length of array1 >= length of array2 for other part I will append later
public int[] join(int[] arr1,int[] arr2)
{
int l1=arr1.length;
int l2=arr2.length;
int i=0,j=0,k=0;
int c[]=new int[arr1.length+arr2.length];
if(l1>=l2)
{
for(;i<arr1.length;)
{
if(arr1[i]>=arr2[j])
{
c[k]=arr1[i++];
System.out.println(c[k]);
k++;
}
else if(arr2[j]>arr1[i])
{
c[k]=arr2[j++];
System.out.println(c[k]);
k++;
}
}
//System.out.println(j);
//System.out.println(i);
while(i<arr1.length)
{
c[k]=arr1[i++];
System.out.println(c[k]);
k++;
}
while(j<arr2.length)
{
c[k]=arr2[j++];
System.out.println(c[k]);
k++;
}`
}
return c;
//write your code here
}
Parameters ActualOut Expected Out
{100,90,80,70,60}
null {105,100,95,90,85,80,75,70,65,60} {105,95,85,75,65}