LeetCode-Feedback / LeetCode-Feedback

673 stars 331 forks source link

Run Time Error for no reason #22887

Closed bountychaser closed 4 months ago

bountychaser commented 4 months ago

LeetCode Username

Chinmay624

Problem Number, Title, and Link

https://leetcode.com/problems/minimum-size-subarray-sum/description/

Bug Category

Editorial

Bug Description

I am yet to know if this is the correct solution or not but that is not the problem here. The issue is I keep getting runtime error in line 9 saying"Line 9: Char 27: runtime error: signed integer overflow: -1094795583 + -1094795586 cannot be represented in type 'int' (solution.cpp)". Though the values i am adding are valid and lie in the range . idk but i m very frustrated.

Language Used for Code

None

Code used for Submit/Run operation

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int res = INT_MAX;
        int i = 0 , j = 0,  n = nums.size();
        int sum = 0;
        while(j < n || i < n){
            while(sum < target){
                sum = sum + nums[j];
                j++;
            }
            while(sum >= target){
                res = min(res, j - i);
                sum = sum - nums[i];
                i++;
            }

        }
        return res;
    }
};

Expected behavior

Code should run.

Screenshots

No response

Additional context

No response

exalate-issue-sync[bot] commented 4 months ago

Winston Tang commented: Dear Chinmay624,

Thank you for reaching out to us. We understand your frustration and appreciate your patience as we navigate through the issue together.

The runtime error you encountered is likely due to accessing out of bounds indices or summing of the array elements leading to integer overflow in your code. When manipulating array indices, it's essential to ensure you're within the array bounds. As for an integer overflow, you might want to revise your code in such a way that ensures preventing such cases.

I'd suggest revisiting the loop conditions and addition operation in your code. LeetCode's discussions and solutions section for the mentioned problem can be beneficial resources to gain insights into different ways to solve this problem. Always cross-check your code for possible edge cases related to array indices or arithmetic operations.

We hope this helps and please reach out if you have further questions.

Best Regards, LeetCode Support Team