-1

I am trying to find out the longest peak length in an array.

Example: Array: [1, 2, 3, 2, 1, 1] Peak Length: 5 (1,2,3,2,1)

I have used the following code to find out the longest peak in an array:

using namespace std;

int longestPeak(vector<int> array) {
    int longestPeak = 0;
    int idx = 1;
    
*`//In the while loop below, if I change the condition to (idx < (array.size() - 1)), it throws a segmentation fault'*
    while (idx < int(array.size() - 1)){
        bool isPeak = array[idx] > array[idx - 1] and array[idx] > array[idx + 1];
        if (!isPeak){
            idx += 1;
            continue;
        }
        
        int leftIdx = idx - 2;
        while (leftIdx >= 0 and array[leftIdx] < array[leftIdx + 1])
            leftIdx -= 1;
        
        int rightIdx = idx + 2;
        while (rightIdx < array.size() and array[rightIdx] < array[rightIdx - 1])
            rightIdx += 1;
        
        int currentPeak = rightIdx - leftIdx - 1;
        longestPeak = max(currentPeak, longestPeak);
        idx = rightIdx;
    } 
    return longestPeak;
}

In the first while loop below, if I change the condition to (idx < (array.size() - 1)), it throws a segmentation fault.

Please help me understand the reason behind this issue.

6
  • 1
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: What is a debugger and how can it help me diagnose problems? You may also want to read this: How to debug small programs?. Jan 15, 2021 at 1:07
  • 1
    The provided code does not give me a segmentation fault with the provided input (1, 2, 3, 2, 1, 1). Please provide a minimal reproducible example that includes the input that triggers this crash. For bonus points, identify the line where the crash occurs and the values of your variables at that point.
    – JaMiT
    Jan 15, 2021 at 1:21
  • 1
    Also, the provided code does not compile without warnings. See Why should I always enable compiler warnings?
    – JaMiT
    Jan 15, 2021 at 1:22
  • 1
    @Peter The condition leftIdx >= 0 is checked before accessing array[leftIdx].
    – JaMiT
    Jan 15, 2021 at 1:53
  • Thank you everyone, I ran my code through a debugger. My issue was related to an empty array as input where there was unsigned int overflow. Jan 15, 2021 at 14:53

1 Answer 1

1

There is unsigned promotion: 0u - 1 is the biggest value for unsigned int.

So idx < int(array.size() - 1) is true for positive idx and empty array.

3
  • 3
    Hmm, array is not being modified in the loop, so unless the OP is calling the function with an empty range (which is not what the stated input suggests), this isn't the reason for the crash.
    – cigien
    Jan 15, 2021 at 1:09
  • Thank you for replying, can you please elaborate so that I can get a clear understanding. I am still not able to figure out the reason here. Jan 15, 2021 at 1:16
  • Thank you everyone, I ran my code through a debugger. My issue was related to an empty array as input where there was unsigned int overflow. Jan 15, 2021 at 14:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.