airevent

longest_seq.c

Dec 19th, 2015
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.67 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4.     int arr[] = { 10,100,5,-4,-89,29,-2,-8,-49,-499,0,94 };
  5.     int n = sizeof(arr) / sizeof(arr[0]);
  6.  
  7.     int seq_start = -1;
  8.     int longest_seq = 0;
  9.  
  10.     for ( int i=0; i<n; ++i ) {
  11.         if ( arr[i] < 0 ) {
  12.             if ( seq_start < 0 ) {
  13.                 seq_start = i;
  14.             }
  15.         } else if ( seq_start > -1 ) {
  16.             printf("seq found (%d,%d) (len=%d)\n", seq_start, i-1, i-seq_start);
  17.  
  18.             if ( i-seq_start > longest_seq ) {
  19.                 longest_seq = i-seq_start;
  20.             }
  21.  
  22.             seq_start = -1;
  23.         }
  24.     }
  25.  
  26.     printf("longest: %d\n", longest_seq);
  27.  
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment