Directi Interview Question
SDE-2sCountry: India
Interview Type: In-Person
import sys
a=[int(x) for x in sys.stdin.readline().split('')]
b=[int(x) for x in sys.stdin.readline().split('')]
l=[]
for i in range(0,len(a)):
for j in range(i+1,len(b)):
if a[i]>b[j]:
l.append((a[i],b[j]));
print l
Solution in C#
public int[,] Pairs(int[] arrayOne, int[] arrayTwo)
{
List<int> list = new List<int>();
for (int i = 0; i < arrayOne.Length; i++)
{
for (int j = i + 1; j < arrayTwo.Length; j++)
{
if (arrayOne[i] > arrayTwo[j])
{
list.Add(arrayOne[i]);
list.Add(arrayTwo[j]);
}
}
}
int listLenght = list.Count / 2;
int[,] returnArray = new int[listLenght / 2, 2];
for (int i = 0; i < listLenght; i += 2)
{
returnArray[i / 2, 0] = list[2 * i];
returnArray[i / 2, 1] = list[(2 * i) + 1];
}
return returnArray;
}
all of the i*j solutions are nice but if you sort the lists storing (index, value) you can then run a binary search to find where in the first array the value is smaller.
Nice thing about this is that now the solutions can build on each other.
Actually you can't because we also need to check i<j for that. After sorting A[i]<B[j] condition can be checked by using 2 pointer method but i<j won't be in sequence.
Presently we have i<j in sequence(in sorted order) and A[i]<B[j].
Sorting both brings us to square 1 with an overhead of (nlogn+mlogm)
- srterpe September 03, 2014