Why my code gives me RunTimeError


  • 0
    S

    My code works in XCode, but when I submitted it to Leetcode, it gave me a RunTimeError on big input.

    Here is my code:

     class Solution {
        public:
            int majorityElement(vector<int> &num) {
                int max = findMax(num);
                vector<int> count(max,0);
                for (int i = 0; i < num.size(); i++){
                    count[num[i]-1] ++;
                }
                int majEleCount = num.size()/2;
                for (int i = 0; i < max; i++){
                    if (count[i] >= majEleCount) return (i+1);
                }
                return 0;
            }
            
            int findMax(vector<int> &num){
                int max = num[0];
                for (int i =0; i < num.size(); i++){
                    if (num[i] > max) max = num[i];
                }
                return max;
            }
        };

  • 2
    S

    If I understand it correctly, findMax() in your code is trying to find the element that has the max value. For example findMax will return 5 if the array elements are {2, 5, 0, -3, 2, 2, 2, 2}. I'm not sure why you are trying to find the max value in the array. But I exactly know what's the problem in the code. If it is not obvious from the example I supplied, let me explain how your code might be doing a out of bound exception.

    vector contains elements: {2, 5, 0, -3, 2, 2, 2, 2}
    That means size of the vector is 8
    
    let's run through your code with the above example.
    
    int majorityElement(vector<int> &num) {
        //
        // Following call to findMax will return 5 making max = 5
        //
        int max = findMax(num);  
    
        //
        // Following line will construct a 5 element vector
        //
        vector<int> count(max,0); 
    
        //
        // num.size() will yeild 8
        // so the following line is same as "for (int i = 0; i < 8; i++) {"
        //
        for (int i = 0; i < num.size(); i++){ 
    
            //
            // When i is 2 "num[i]-1" will yield -1 and will cause out of bound exception.
            // This can happen for any element with value 0 or less.
            //
            count[num[i]-1] ++; 
        }
        int majEleCount = num.size()/2;
        for (int i = 0; i < max; i++){
            if (count[i] >= majEleCount) return (i+1);
        }
        return 0;
    }
    

  • 0
    S

    Hi Satyakam, thanks for taking your time looking at my code. However, in that for loop, I don't see how it is out of bound. If i = 6, then num[i] = 2 and thus count[num[i]-1] gives count[1] which is 2.


  • 0
    S

    You are right. I realized that little after posting, but was lazy to update. Now I have updated the post with the actual error. Let me know if that does not make sense.


  • 0
    S

    Awesome. Looks like you got the issue resolved now. Thank you for the vote.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.