Advertisement
Guest User

Find Sum in a sorted array

a guest
Feb 23rd, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.35 KB | None | 0 0
  1. // Find Sum in a sorted array
  2.  
  3. int left = 0;
  4. int right = data.Length - 1;
  5.  
  6. while(true)
  7. {
  8.     if (left == right)
  9.     {
  10.         return false;
  11.     }
  12.     int tempSum = data[left] + data[right];
  13.     if (tempSum == sum)
  14.     {
  15.         return true;
  16.     }
  17.     if (tempSum > sum)
  18.     {
  19.         --right;
  20.     }
  21.     else
  22.     {
  23.         ++left;
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement