Skip to content

Instantly share code, notes, and snippets.

@KodeSeeker
Created April 7, 2013 04:19
Show Gist options
  • Save KodeSeeker/5328998 to your computer and use it in GitHub Desktop.
Save KodeSeeker/5328998 to your computer and use it in GitHub Desktop.
Print all pairs of numbers from an array that adds up to M. Assume input is sorted.
public static void PrintAllPairs(int[] sorted, int M)
{
//as the key is to keep track of two indexes, left and right, we define the indexes
int left = 0;//leftmost
int right = sorted.length-1;//rightmost
//now we start our loop, and only make sure left not meet right will we proceed
while(left<right)
{
int tempSum = sorted[left] + sorted[right];//calculate the temp sum and use it to compare to M
if(tempSum==M)//great we find one pair!
{
//format the output per line as "Sum{1,10}=11"
System.out.println("Sum of {"+sorted[left]+", "+sorted[right]+"} = "+M);
//do not forget the update both index values
left++;
right--;
}
else if(tempSum>M)//as the temp sum is bigger than M,
//we need more right focus left to make the future sum smaller
right--;
else//the only remaining case is temp sum smaller than M, so move left focus right
left++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment