0
class Solution {
public:
    int findLucky(vector<int>& arr) {
    sort(arr.begin(),arr.end());
    int count=1;
    int l=-1;
    for(int i=0;i<arr.size();i++){
        if(arr[i]==arr[i-1]){  
            count=count+1;
        }
        if(arr[i]!=arr[i-1]){
            count=1;
        }
        if(count==arr[i]){
            l=arr[i];
        }
        if(count==arr[i]&& arr[i]>l){
            l=arr[i];
        }
        
        
    }
        return l;
        
    }
};

Runtime Error Message: Line 1034: Char 34: runtime error: addition of unsigned offset to 0x602000000090 overflowed to 0x60200000008c (stl_vector.h) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:34

1 Answer 1

0

well for a start this leads to UB

for(int i=0;i<arr.size();i++){
    if(arr[i]==arr[i-1])

when i = 0 you are accessing arr[-1]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.