Help me out in this problem

question link


my leetcode sbmission is passing 63 cases outof 93
can anyone tell me correct my approach i have two pointer approach

Hey @shivammishra20121999 can you please tell me about the approach you are using so that I can easily debug it for you.

@mr.encoder can you provide me your phone number so that i can explain better

@ hey are u there you have seen my code

Hey @shivammishra20121999 I am sorry cause I won’t be able to provide you with my phone number. You can explain it here and I’ll try my best to debug it for you.

@mr.encoder
i have used sliding window approach
1.i keep adding the elements to sum till the time it is less than x
2.if sum >=0 then i shrink my window size from left to get the shortest length

Okay I got it. Have you used deque as a data structure or have applied using array?

@mr.encoder i have used 1d array

@mr.encoder
i think it can be done without deque

Hey @shivammishra20121999 it can be done but when you uses deque, it will take care of side cases on it’s own. Are you familiar with deque concept?

yes @mr.encoder but can you tell me how we can do with sliding window approach

Hey @shivammishra20121999 the sliding approach you have used seems to be appropriate. Give me some time I’ll debug this code as per your requirements and let you know your mistake. Till then you can do your next question. When I’ll debug it, I’ll reply to your query here so that you can submit it :slight_smile:

how much time bro @mr.encoder

Right now I am taking few more doubts. I’ll debug it in between 6-7. Will that work for you?

bro can you debug little earlier @mr.encoder

Okay I’ll debug it till 5:30, just be patient till then,okay?

@mr.encoder
okay bro sure

1 Like

hey friend i got your mistake, take this test case for example :[84,-37,32,40,95] & 167
here we first go in this loop

while (curr_sum < x && end < n)

        { 

            // Ignore subarrays with negative sum if 
            // x is positive. 

            if (curr_sum <= 0 && x > 0) 
            { 
                start = end; 
                curr_sum = 0; 
            } 

            curr_sum += arr[end++]; 
        }

here,
cum_sum is 0, and x is also greater then 0 so we go in if statement, so csum becomes 84.
cum_sum is 84, and x is also greater then 0 so we don’t go in if statement, so csum is 47.
cum_sum is 47, and x is also greater then 0 so we don’t go in if statement, so csum is 79.
cum_sum is 79, and x is also greater then 0 so we don’t go in if statement, so csum is 119.
cum_sum is 119, and x is also greater then 0 so we don’t go in if statement, so csum is 214.
After that we goes in this condiion
// If current sum becomes greater than x.
while (curr_sum >= x )
{
// Update minimum length if needed

                if (end - start < min_len && curr_sum>=x && start<n) 
                {
                    min_len = end - start;
                }
  
            // remove starting elements 
                curr_sum -= arr[start++]; 
            
        }
  • your csum is 214>167 and
    end(5) - start(1) < min_len && curr_sum(214)>=x(167) && start(1)<n(6)
    min_len(4) = end - start;
    curr_sum() -= arr[start++]; //csum=214-(-37)

therfore giving you output as 5(6-1), your approach is partially right but won’t get fully accepted. Try to approach it using deque :smile:

@mr.encoder
can you tell me the approach how to do with deque

Try to understand from this post


A well explained approach, if still get struck into it. You can ask me :grinning: