Microsoft Interview Question
Software Engineer in Tests1. Need to do hashtable.
2. Sort the array in O{N*lgN) first.
3. Check the chosen element to see if it is sum of the any two item. Acturely, this could be O(N^3).
For i= N to 3 (no need to check first two)
do {
for (j= i-1 to 2)
for (k= j-1 to 1)
{
sum = a[j]+a[k];
if a[i] = sum
{ print (i, j, k);
break;
}
}
}
4. If we have enough memories, create an array of N*N/2 to store sum = a[x] + a[y]. This is O(N^2).
Asuming a is sorted in 2.
sum[1] = a[1] + a[2];
sum[2] = a[1] + a[3];
sum[3] = a[2] + a[3];
......
sum[N*N/2] = a[N-1]+ a[N};
This array is sorted because a is sorted.
for i= N to 3
apply binary search on sum for a[i]
This is N* O(lg(N^2)) equals to O(2*N*lgN)
So, the algorithm is O(N*N).
5. You can just do:
s= 0;
do
{
for i= 1 to N-1
for j=i+1 to N
{
temp = a[i] + a[j];
if (temp > sum[s])
{
s++;
sum[s] = temp;
}}}
|
And then, apply binary search to sum (less than N*N/2.
After sorting the array, you can check A[i] from biggest to smallest and the question became: how to find if there exists a pair of number in an array such that the sum of them is K, in O(logn).
I agree, it's nlogn,
But if sort is nlogn, and the problem of "how to find if there exists a pair of number in an array such that the sum of them is K" is also nlogn.
Then the solution is still nlogn, which is asked.
I am curious how this could be nlogn. For each element at pos i, you have to check the next n -i elements. This is O(N). You have n/2 elements to check. This is N^2. Isn't it?
say you have A[i] you want to check if A[i] can be the sum of two of A[1:i-1] then with the help of binary search ilogi is needed and you have to do all these operations for i = n : -1: 2
totally you spent n^2logn
1. first sort array in o(nlog(n)) time.
2. for each S from A[n-1] to A[0], check if there exist two elements in A such that A[x]+A[y] = S. This will take O(n^2) time since for every S, it need O(n) time to find A[x] and A[y].
any better solutions?
No better solutions are known to exist and it is believed that Omega(n^2) is a lower bound. The problem is known as 3SUM.
1. Sort the array;
2. Starting from the biggest element, say A[n-1], to find if there are a pair of elements A[i] and A[j], whose sum is equal to A[n-1]. The best way is to try A[0]+A[n-1], if smaller than A[n-1], then try A[1]+A[n-1], ..., until two indies meet. It has O(n^2) complexity, and if you use brutal search, it easily goes to O(n^3).
There is O(nlogn)
1.Sort as Descending
2.If any A[i] == 0 Then the Max is A[0]
3.Omit all negative elements.
4.For A[i] do B[j]=A[i]-A[j];
Check B[j] have the same element of A[i].
If have, Max is A[i]
I don't understand your algo. What about all elements are negative? Step 4 alone could be nlogn, how comes overall nlogn?
Step 4:You can put all elements in a hash table which takes O(n) ans then can check B[j] which takes O(1)
1.int found=n,t=n,b //n:no of elements in the array
2.QuickSort(A,1,n)
3.while(found>=3) //uptil we reach the case of a[1,2,3 ]
4. for i=1 to t/2
5. b=BinarySearch(A[t]-A[i])
6. if(b==0)
7. found--
8. t--
9. else
break
10.print A[b]"+" A[i] "=" A[t]
Running time===O(n^2)
Best case and avg case are much better
This can be done in O(n)...
int A[n], max=A[0],i;
for (i=0;i<n;i++)
{
if(max <A[i])
{
max = A[i];
}
}
cout<<"the max value is "<<max;
as the above loop is executed only once, you can find it in O(n).
O(n^2)
- 666 October 31, 2008Sort the array O(nlgn)
c: start from the last element in the array, decrement till it reaches first element
have two pointers - one in the end and one in the beginning.
if the sum of the values at the pointers is greater than the last element, decrement the end pointer
else if the sum is less, increment the first pointer
if equal, print the last element and break
goto c: