find the max item A[i].
A[i]=A[x]+A[y].
1) hash table way O(n^2)
each time, select an element A[i], and check the hash table if ht[A[i]-A[y]] exist, we find the A[x] which is A[A[i]-A[y]].
int foo(int A[], int len)
{
int max=min_int;
for(int i=0;i < len;i++)
{
if(A[i] < max) continus;
hashtable ht;
for(int j=0;j
if(ht[A[i]-A[j]]==1)
{
if(A[i]>max)
max=A[i];
}
ht[A[j]]==1;
}
}
return max;
}
2)sort the array and use two pointers, each of which point to max element and min element, do the linear search sort array use two pointers.
No comments:
Post a Comment