Jing's Interview Corner
Sunday, October 14, 2012
find all pairs sum to k
void findpairs(int a[], int k)
{
int n = sizeof(a)/sizeof(a[0]);
int small = 0;
int big = n-1;
while(small< big)
{
int sum = a[small]+a[big];
if(sum == k)
{
cout<<"("<<small<<","<<big<<")"<<",";
}
else if(sum < k)
{
small++
}
else
{
big--;
}
}
}
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment