LaoArchAngel

maxDifference

Mar 24th, 2016
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.36 KB | None | 0 0
  1. static int maxDifference(int[] a)
  2. {
  3.     int high = a[0];
  4.     int low = a[0];
  5.     int diff = -1;
  6.  
  7.     for (int i = 1; i < a.Length; i++)
  8.     {
  9.         int current = a[i];
  10.  
  11.         if (current > high)
  12.         {
  13.             high = current;
  14.             diff = high - low;
  15.             continue;
  16.         }
  17.  
  18.         if (current < low)
  19.         {
  20.             low = current;
  21.             high = diff > 0 ? low + diff : low;
  22.         }
  23.     }
  24.  
  25.     return diff;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment