Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
also need to check i , j, k does not below 0.
in case of array1 is having single element which is largest. we cant decrement the counter.
Like @Apostle's solution below, I was thinking of using min-heap of size 5, but this solution is better. Good job! +1 from me.
This is essentially the "merge" step in merge sort (for the k-merge problem), but which starts from the end of the array instead of the head of the array, and which stops when there are 5 elements already. So the "officially suggested" solution uses a max heap of size 3, and in each step it extracts the maximum, then fill the blank with the next element in the array that extracted max belongs to.
Use a min-heap of size 5. Insert the last 5 elements of first array into it.
Use the last 5 elements again from each of the remaining arrays. Insert the element into the heap if it is greater than the min-element of the heap by evicting the min-element.
@Apostle .. Let suppose arrays are not in sorted form. then what approach should we follow .
@sunny.gupta
For unsorted or sorted arrays just take out the six maximum elements from the three arrays two from each array. And then from those six take out the 5 maximum elements. Time complexity is O(n). I can also provide the code if you want.
@Vgeek
But there is a possibility to get the all 5 largest number in one array or two array also. picking up two elements from each array wont work here.
@sunny
Yes you are right that approach wont work here. We have to consider some other approach.
If the arrays are sorted then you just have to merge the three arrays and then match for the 5 largest numbers . Here is the code.
#include <stdio.h>
#include <conio.h>
int n;
int *topLargestElement(int a1[],int a2[],int a3[])
{
int i=0,j=0,didx=n;
int dest[3*n],top5ele[5];
while(i<n&&j<n)
{
if(a1[i]<=a2[j])
{
dest[didx]=a1[i];
++i;
++didx;
}
else
{
dest[didx]=a2[j];
++j;
++didx;
}
}
if(i<n)
{
while(i<n)
{
dest[didx]=a1[i];
++didx;++i;
}
}
else if(j<n)
{
while(j<n)
{
dest[didx]=a2[j];
++didx;
++j;
}
}
i=0,j=n,didx=0;
while(i<n&&j<3*n)
{
if(a3[i]<=dest[j])
{
dest[didx]=a3[i];
++didx;++i;
}
else
{
dest[didx]=dest[j];
++didx;++j;
}
}
if(i<n)
{
while(i<n)
{
dest[didx]=a3[i];
++didx;++i;
}
}
j=0;
for(i=didx-1;i>=didx-5;i--)
{
top5ele[j]=dest[i];
j++;
}
return top5ele;
}
int main()
{
int a1[]={2,4,9,16,19,68,78};
int a2[]={4,8,67,106,109,115,120};
int a3[]={9,15,19,108,119,130,190};
n=sizeof(a1)/sizeof(a1[0]);
int *arr=topLargestElement(a1,a2,a3);
int i;
printf(" %d %d %d %d %d ",arr[0],arr[1],arr[2],arr[3],arr[4]);
}
i=size of first array
j=size of second array
k=size of third array
for(l=0;l<5;l++)
{
if(A1[i]>A2[j]&&A1[i]>A3[k])
{
ans[l]=A1[i];
i--;
}
else if(A2[j]>A1[i]&&A2[j]>A3[k])
{
ans[l]=A2[j];
j--;
}
else{
ans[l]=A3[k]
k--;
}
}
private static int[] ReturnMaxFive(int[] arr1, int[] arr2, int[] arr3)
{
int []rArr = new int[5];
if (arr1.Length + arr2.Length + arr3.Length < 5)
{
throw new ArgumentException("Length is less than 5");
}
int iArr1 = arr1.Length - 1;
int iArr2 = arr2.Length - 1;
int iArr3 = arr3.Length - 1;
int irARR = 0;
while (irARR < 5)
{
int max = int.MinValue;
int ArrayNum = 0;
if (iArr1 > 0 && arr1[iArr1] > max)
{
max = arr1[iArr1];
ArrayNum = 1;
}
if (iArr2 > 0 && arr2[iArr2] > max)
{
max = arr2[iArr2];
ArrayNum = 2;
} if (iArr3 > 0 && arr3[iArr3] > max)
{
max = arr3[iArr3];
ArrayNum = 3;
}
switch (ArrayNum)
{
case 1:
rArr[irARR] = arr1[iArr1];
iArr1--;
break;
case 2:
rArr[irARR] = arr2[iArr2];
iArr2--;
break;
case 3:
rArr[irARR] = arr3[iArr3];
iArr3--;
break;
}
irARR++;
}
return rArr;
}
private static int[] ReturnMaxFive(int[] arr1, int[] arr2, int[] arr3)
{
int []rArr = new int[5];
if (arr1.Length + arr2.Length + arr3.Length < 5)
{
throw new ArgumentException("Length is less than 5");
}
int iArr1 = arr1.Length - 1;
int iArr2 = arr2.Length - 1;
int iArr3 = arr3.Length - 1;
int irARR = 0;
while (irARR < 5)
{
int max = int.MinValue;
int ArrayNum = 0;
if (iArr1 > 0 && arr1[iArr1] > max)
{
max = arr1[iArr1];
ArrayNum = 1;
}
if (iArr2 > 0 && arr2[iArr2] > max)
{
max = arr2[iArr2];
ArrayNum = 2;
} if (iArr3 > 0 && arr3[iArr3] > max)
{
max = arr3[iArr3];
ArrayNum = 3;
}
switch (ArrayNum)
{
case 1:
rArr[irARR] = arr1[iArr1];
iArr1--;
break;
case 2:
rArr[irARR] = arr2[iArr2];
iArr2--;
break;
case 3:
rArr[irARR] = arr3[iArr3];
iArr3--;
break;
}
irARR++;
}
return rArr;
}
#include <iostream>
#include <stdlib.h>
using namespace std;
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main(int argc, char** argv)
{
int A1[7] = {2,4,9,16,19,68,78};
int A2[7] = {4,8,67,106,109,115,120};
int A3[7] = {9,15,19,108,119,130,190};
int* tab = new int[21];
for(unsigned int z=0; z<21; ++z)
{
if(z<7)
tab[z] = A1[z];
else if(z>=7 && z<14)
tab[z] = A2[z-7];
else if(z>=14 && z<21)
tab[z] = A3[z-14];
}
qsort (tab, 21, sizeof(int), compare);
for(int z=20; z>14; --z)
cout << tab[z] << endl;
return 0;
}
Thank u dude..But Why so err?..ur code is right ..Here is updAted prog in c++
#include <iostream.h>
#include <stdlib.h>
#include<conio.h>
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main(int argc, char** argv)
{
int A1[7] = {2,4,9,16,19,68,78};
int A2[7] = {4,8,67,106,109,115,120};
int A3[7] = {9,15,19,108,119,130,190};
int* tab = new int[21];
for(unsigned int z=0; z<21; ++z)
{
if(z<7)
tab[z] = A1[z];
else if(z>=7 && z<14)
tab[z] = A2[z-7];
else if(z>=14 && z<21)
tab[z] = A3[z-14];
}
qsort (tab, 21, sizeof(int), compare);
for(z=20; z>14; --z)
cout << tab[z] << endl;
getch();
}
single scan, logic start from end of arrays and compare
- pirate July 26, 2013