runnig

i<j<k, a[i]<a[j]<a[k]

Jan 18th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. // http://www.careercup.com/question?id=14960662
  2. #include <stdio.h>
  3.  
  4. int main()
  5. {
  6.     int a[] = {1, 2, -1, 0, 7, 6, 5};
  7.    
  8.     const int N = sizeof(a)/sizeof(a[0]);
  9.  
  10.     int i = 0;     
  11.     for(int s = 1; s < N-2; ++s)
  12.     {
  13.         if(a[s] < a[i]) { i = s;}
  14.         else if(a[s] > a[i]) { break;}
  15.     }
  16.    
  17.     int k = N - 1;
  18.     for(int s = N - 2; s > i; --s)
  19.     {
  20.         if(a[s] > a[k]) { k = s; }
  21.         else if(a[s] < a[k]) { break; }
  22.     }
  23.    
  24.     for(int j = i+1; j < k; ++j)
  25.     {
  26.         if(a[i] < a[j] && a[j] < a[k])
  27.         {
  28.             printf("%d, %d, %d\n", a[i], a[j], a[k]);
  29.             return 0;
  30.         }
  31.     }
  32.     return -1;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment