Subarray with given sum

preview_player
Показать описание
This video explains how to find a subarray from a given array having sum equals to a given sum value. This problem is simple to solve but has been very frequently asked in programming interview rounds. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
Рекомендации по теме
Комментарии
Автор

🟣 JOIN our 𝐋𝐈𝐕𝐄 𝐢𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐭𝐫𝐚𝐢𝐧𝐢𝐧𝐠 𝐩𝐫𝐨𝐠𝐫𝐚𝐦 through whatsapp query: +91 8918633037

techdoseu
Автор

Nice explanation.
Isn't it the same question as subarray sum equals k. You have uploaded that video too and that works for every edge case as well as negative numbers.

mikasaackerman
Автор

Thanks for sharing such a valuable programming content

mohit
Автор

Hey what if after 10 there is 5.
So when we get currentsum as 38, adding next element -5 would make it as 33. Perhaps it isn't suitable for arrays containing negatives

sushobhitjakhmolaAdmin
Автор

What if there is more than one subarray whose sum is equal to the target sum. Then how will you modify your code? For reference see LeetCode 1171 problem. I know this 1171 LeetCode can be solved by another approach(I used Stack for solving it). But I want to apply Target sum subarray algorithm here in this question.

K_EC_AushoRoup
Автор

halo bro what will we do if our first element is greater then sum
{ 43, 1, 17, 26, 15 } and sum is 32

SurajKumar-cgmm
Автор

is it an implementation of sliding window technique?

arijitchandra
Автор

NOTE:- I'd recommend you guys to know the logic and then write a simple JAVA, C/C++ code, vector isn't much required.
those who are unable to solve it with vector, here's the full solution. :)

JAVA:-
static void subarraySum(int n, int s, int[] arr) {

int start=0, end=0, sum=0, r=0;

int i=0;
while(i<n) {="" sum="sum+arr[i];" if(sum="">s)//if sum becomes greater
{
start=start+1;
sum=0;
i=start-1;
}

if(sum==s)//if we found all index
{
r++;
end=i+1;
start++;
break;
}
i++;
}
if(r>0)
System.out.print(start+" "+end);
else
System.out.print("-1");

}

C++, #include<vector>
vector<int> subarraySum(int arr[], int n, int s){
int l = 0;
int r = 0;
int count = 0;
vector<int> res;

while (r<=n) {
if (count == s) {
res.push_back(l+1);
res.push_back(r);
return res;
}
if (l==r || count < s) {
r++;
count += arr[r-1];
}
else {
l++;
count -= arr[l-1];
}
}

res.push_back(-1);
return res;
}

sujitgoswami
Автор

approach not work on array having negative numbers arr : [-1, -1, 1] target = 0, Not a generalize solution.

mayurkoli
Автор

what if the subarray does not need to be consecutive numbers. There can be "holes" in the middle. That's you get the minimum number of elements from the array to reach the sum.

howardlam
Автор

What of there is 9 at index 4 and sum is still 33? I mean what if we have subarray sums greater than or lower than the given sum, but not exactly equal to it?

askchaitanya
Автор

Hi...How can we do the same thing using maps??

msahai
Автор

This was a good explanation thanks a lot for making this video.

ankitchawla
Автор

sir your videos has helped a lot . Sir can you tell me from where should i prepare for technical mcqs .
I have exam of accolite company next week. Thankyou for the help.

Kushagra_
Автор

What is negative elements are also present [4, -1, -2, 0, 3, 1] and sum 1

gaurika
Автор

really explanation, u should keep on uploading like these content.

jawwadakhter
Автор

Hello friend, you're doing a great job and I am very thankful for these videos.
I had a small doubt regarding your explanation of the time complexity. You said that since we're" passing" through each element a max of two times, the complexity should be O(2*N) which obviously is O(N). I think, since incrementing left is an O(1) operation, it doesn't cause the time complexity to be O(2*N).It should still be O(N), simply because the loop will run for a max of n times(doing O(1) work each time) . Even though we get the same complexity eventually, getting the analysis wrong might be disastrous in an otherwise highly effective answer.

suyashmisra
Автор

thank you so much sir simple and easy explaination! radhavallabh shri harivansh

braj_marg
Автор

what if sum = 15.
right pointer will stop at 2.
And till 2 we shall not have any subarray with sum =15

GdLongerHandle
Автор

Please Explain Longest subarray with given sum using HashSet if possible

tejasdonadkar