top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Merge two arrays in such a way that the resultant array is also sorted?

0 votes
379 views
Merge two arrays in such a way that the resultant array is also sorted?
posted Jul 12, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
both input arrays are already sorted or not?

1 Answer

0 votes
#include<stdio.h>
#include<conio.h>
#define size 
main()
{
    int arr1[size],arr2[size],arr3[size+size],i,j,temp=0,no,num,k=0;
    printf("Enter number of element in array first:\t");
    scanf("%d",&no);
    printf("\nEnter element of array first:\n");
    for(i=0;i<no;i++)
    {
        scanf("%d",&arr1[i]);
    }
    printf("\nEnter number of element in array second:\t");
    scanf("%d",&num);
    printf("\nEnter element of array second:\n");
    for(i=0;i<num;i++)
    {
        scanf("%d",&arr2[i]);
    }
    printf("\n element of array first:\n");
    for(i=0;i<no;i++)
    {
        printf("%d  ",arr1[i]);
    }
    printf("\n element of array second:\n");
    for(i=0;i<num;i++)
    {
        printf("%d  ",arr2[i]);
    }
    for(i=0;i<no+num;i++)
    {
        k=0;
        if(i>no-1)
        {
            arr3[i]=arr2[k];
            k++;
        }
        arr3[i]=arr1[i];
    }
    printf("\n mearg element of array first and second are:\n");
    for(i=0;i<no+num;i++)
    {
        printf("%d  ",arr3[i]);
    }
    printf("\nsorted array3 are\n");
    for(i=0;i<no+num;i++)
    {
        for(j=i+1;j<no+num+1;j++)
        {
            if(arr3[j]>arr3[i])
            {
                temp=arr3[i];
                arr3[i]=arr3[j];
                arr3[j]=temp;
            }
        }
    }
    for(i=0;i<no+num;i++)
    {
        printf("%d ",arr3[i]);
    }
    getch();
    return 0;
}
answer Jul 1, 2017 by Ajay Kumar
Similar Questions
0 votes

Given an array of integers, rearrange the array in such a way that the first element is first maximum and second element is first minimum.

–1 vote

Say you are given two arrays where second array is duplicate with one missing element. What should be the best possible way to find that missing element -
array1: [1,2,3,4,5,6,7]
array2: [1,3,4,5,6,7]

Missing Element: 2

0 votes

Given an array of sorted integers and find the closest value to the given number. Array may contain duplicate values and negative numbers.

Example : Array : 2,5,6,7,8,8,9
Target number : 5
Output : 5

Target number : 11
Output : 9

Target Number : 4
Output : 5

0 votes

Given an unsorted array which has a number in the majority (a number appears more than 50% in the array), how to find that number?

...