Home > Back-end >  How do I tweak the increasing longest array sequence in C to equal or greater?
How do I tweak the increasing longest array sequence in C to equal or greater?

Time:10-17

I am trying to code a function such that it gives me the longest consecutive increasing or equal sequence n within an array, for instance { 1, 2, 3, 4, 4, 3, 2, 1 } would give n = 5.

However when I tried with { 7, 7 }, my code returns 0. I am not sure if I got the if (array[i] >= array[i-1]) right. Thank you in advance!

int longest(int array[], unsigned n) {
    //assign longest sequence
    int k = 0;
    int running_k = 1;
    
    for (int i = 0; i < n;   i) {
        if (array[i] > array[i - 1]) {
              running_k;
        } else
        if (array[i] == array[i - 1]) {
            running_k = running_k   1;
        } else {
            if (running_k > k) {
                k = running_k;
            }
            running_k = 1;
        }
    }
    return k;
}

CodePudding user response:

There are multiple problems:

  • the argument n, the return value and the local variables should have type size_t.

  • you should not read array[i - 1] when i is 0, so the initial value of i should not be 0, but 1.

  • the 2 tests inside the loop should be combined as:

      if (array[i] >= array[i - 1])
    
  • you should check if running_k > k at the end of the loop, in case the longest running sequence is at the end of the array, which is the case for { 7, 7 }.

  • you should special case arrays of length 1 and 0.

Here is a modified version:

size_t longest(const int *array, size_t n) {
    if (n <= 1)
        return n;

    size_t k = 0;
    size_t running_k = 1;

    for (size_t i = 1; i < n; i  ) {
        if (array[i] >= array[i - 1]) {
              running_k;
        } else {
            if (k < running_k) {
                k = running_k;
            }
            running_k = 1;
        }
    }
    if (k < running_k) {
        k = running_k;
    }
    return k;
}

CodePudding user response:

Your code will return 0 if the longest sequence is of length 1.

int longest(int array[], unsigned n) {
    //assign longest sequence
    int k = 0;
    int running_k = 1;
    
    for (int i = 1; i < n;   i){
        if (array[i] >= array[i-1] ){
              running_k;
        }
        else {
            if (running_k > k){
                k = running_k;
            }
            running_k = 1;
        }
    }
    return k;
}

CodePudding user response:

Here's a shortened version of what you seek. I adjusted some of the types for consistency. size_t is a typedef for an unsigned int or unsigned long. You can likely change it back to int or unsigned as needed.

size_t longest(int* array, size_t n) {

   size_t length=1, best=1;

   if (n <= 1) {
      return n;
   }

   for (size_t i = 1; i < n; i  ) {
       length = (array[i] >= array[i-1]) ? (length   1) : 1;      
       best = (length > best) ? length : best;
   }

   return best;
}
  •  Tags:  
  • c
  • Related