Find Two Non-overlapping Sub-arrays Each With Target Sum

question–>


can you help with this question how to do this using sliding window

Hey @sheikhhaji18 tell me the approach you have thought i will help you in optimizing or correcting it.

I will try to explain this to you with an example[7,3,4,7]
The intuition behind this problem is:

  1. Use a sliding window to maintain a subarray whose sum is <= target
Use it in such a way that if you encounter any (i-j)th index 
then make sure that you don't count it again
  1. When the sum of the sliding window equals to target, we found a subarray [s, e]
Here in this example, the sub arrays will be
[7]
[3,4]
[7]
  1. Update ans with it’s length + shortest subarray which ends before s.
when we encounter first subarray
[7] our s would be INT_MAX, 
but after that it will be 1

Similarly after calculating subarray
[3,4] our length will be 2, so till now it's
2+1=3

'Now we encounter second [7] subarray
here its value will be 1, 
so we will subtract 2 from answer 
and add 1 in it, cause we only want 2 sub array
  1. We can use an array to store the shortest subarray which ends before s.

mr.encoder how do we come to know whether a element is visited or not, if we are using visited array concept then when should we update it?

Iterate it from starting of an array let it be i, then just use while loop and iterate jth index until you reach target value sum, when you reach the target value sum, just make your i equal to j , either you can do this way
or make a visited array of bool type, you can do it via that too. I have a code for this, wait let me attach it here for you.

Have added comments in it, which are same as the algo i have explained it here. With the same implementation i have explained it in the algo

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.