Given two arrays like {1,2,3,4,5}......
Software QA/Tests Interview Questions from Microsoft
(Continued from previous question...)
Given two arrays like {1,2,3,4,5}......
Question:
Given two arrays like {1,2,3,4,5} and {3,2,4,5,1}. Output an array which has the index positions on the elements from the first array in the second array.
So the answer would be {5,2,1,3,4} as 1 from 1st array exists at 5th position in the second array, 2 from first array exists at 2nd position in the 2nd array and so on.
maybe an answer:
Assuming the arrays to be like what u have put as example . To avoid extra space(without hashtable) and arrays are faster than Hash table.replace the 2nd array with
for(i<arr2.lenght)
arr2[arr2[i]]= i //O(m) say
and then scan through the arr1 and then get it by:
arr2[arr1[i]]
nt arr1[]= {1,2,3,4,5};
int arr2[]= {3,2,4,5,1};
int arr3[]= new int[6];
for(int i=0;i<arr2.length;i++)
arr3[arr2[i]]=i;
for(int i=0;i<arr1.length;i++)
{
System.out.println(arr3[arr1[i]]);
}
(Continued on next question...)
Other Interview Questions
|