MindTree Coding Questions in Easy Section – 3

Write a program to return a sorted array from two unsorted array?

Examples:

Input : a[] = {10, 5, 15}
        b[] = {20, 3, 2}
Output : Merge List :
        {2, 3, 5, 10, 15, 20}

Input : a[] = {1, 10, 5, 15}
        b[] = {20, 0, 2}
Output : Merge List :
        {0, 1, 2, 5, 10, 15, 20}

Please also comment the code down below in other languages.

#include <bits/stdc++.h>
using namespace std;

// Function to merge array in sorted order
void sortedMerge(int a[], int b[], int res[],
int n, int m)
{
// Concatenate two arrays
int i = 0, j = 0, k = 0;
while (i < n) {
res[k] = a[i];
i += 1;
k += 1;
}
while (j < m) {
res[k] = b[j];
j += 1;
k += 1;
}

// sorting the res array
sort(res, res + n + m);
}

// Driver code
int main()
{
int a[] = { 10, 5, 15 };
int b[] = { 20, 3, 2, 12 };
int n = sizeof(a) / sizeof(a[0]);
int m = sizeof(b) / sizeof(b[0]);

// Final merge list
int res[n + m];
sortedMerge(a, b, res, n, m);

cout << “Sorted merged list :”;
for (int i = 0; i < n + m; i++)
cout << ” ” << res[i];
cout << “n”;

return 0;
}

6 comments on “MindTree Coding Questions in Easy Section – 3”


  • VIVEK

    #include
    int main()
    {
    int s1[100],s2[100],s3[1000],n,i,j=0,temp,h,k,l;
    scanf(“%d”,&n);
    for(i=1;i<=n;i++)
    {
    scanf("%d",&s1[i]);
    }
    for(i=1;i<=n;i++)
    {
    scanf("%d",&s2[i]);
    }
    for(i=1;i<=n;i++)
    {
    s3[j]=s1[i];
    j++;
    }
    for(i=1;i<=n;i++)
    {
    s3[j]=s2[i];
    j++;
    }

    for(k=0;k<(2*n);k++)
    {
    for(l=0;l<(2*n);l++)
    {
    if(s3[k]<s3[l])
    {
    temp=s3[l];
    s3[l]=s3[k];
    s3[k]=temp;
    }
    }
    }
    for(i=0;i<(2*n);i++)
    {
    printf("%d ",s3[i]);
    }
    }


  • Suhail

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;

    class mergels{
    public static void main(String[] args)throws IOException{
    int [] a = {10,5,15};
    int [] b = {20,3,2};
    ArrayList ar1 = new ArrayList();
    ArrayList ar2 = new ArrayList();
    for(int i: a){
    ar1.add(i);
    }
    for(int j: b){
    ar2.add(j);
    }
    ar1.addAll(ar2);
    //ArrayList sor = new ArrayList();
    Collections.sort(ar1);
    for(int p: ar1){
    System.out.println(p);
    }

    }
    }


  • Prabitha Prabhakar

    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    m=[]
    for i in range(len(a)):
    m.append(a[i])
    for j in range(len(b)):
    m.append(b[j])
    m.sort()
    print(m)