-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.

Mr Null Pointer
  • 101
  • 1
  • 2
  • 8
  • 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?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Jan 15 '21 at 01: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 [mre] 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 '21 at 01:21
  • 1
    Also, the provided code does not compile without warnings. See [Why should I always enable compiler warnings?](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) – JaMiT Jan 15 '21 at 01:22
  • 1
    @Peter The condition `leftIdx >= 0` is checked before accessing `array[leftIdx]`. – JaMiT Jan 15 '21 at 01: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. – Mr Null Pointer Jan 15 '21 at 14:53
  • In another question (that you have deleted) you wrote. `Please correct me if I am wrong but I am getting a vibe here that I am dumb to ask this question on stackoverflow.` That's absolutely not the case. But this kind of questions (the deleted one) often indicates that you used a bad source for learning (like some competitive programming, or other bad online tutorials). For C++ (as it is close to hardware and allows much control) it is crucial to learn the basics of the language from a good source, which teaches you the concepts of the language in the correct order. – t.niese Jan 22 '21 at 10:21

1 Answers1

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.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 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 '21 at 01: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. – Mr Null Pointer Jan 15 '21 at 01: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. – Mr Null Pointer Jan 15 '21 at 14:53