This code is wrong as in the inner 1st while loop the condition should be sum>=x (go to 11:51) and in the given example the ans should be 2 (45+6) not 3 (go to 17:30).
Code is right. x is greater than equal to sum && n is greater than end.then sum +=arr[end++].output is 3 (45+6+4=55) because sum>x and it is also smallest subarray .but code is not work for negative elements.
#include using namespace std; int minSubarraySum(int arr[], int n, int x) { int k=0, sum=0, i=0, j=0, ans=INT_MIN; while(ix) { sum-=arr[j]; j++; } } if(j==0){ return ans+1;//no elements exist in the array that exceeds the accumulated value } return ans; } int main() { int arr[]={1, 4, 45, 6, 10, 19}; cout
Is it ok, int MinSubArrSize(int arr[], int n , int x){ int ans = INT_MAX; int sum = 0; int index = 0; for(int i = 0 ; i< n ; i++){ sum += arr[i]; if(sum > x and index < i){ ans = min(ans, i - index); sum -= arr[index]; index++; } } return ans; }
try one day multiple subject, but if you like to study one subject then go on with only one subject, and when you bored at one subject then move on with other subjects
Diii kya C language bhi shekna padhega.....agr java ya c++ me se koi ek shek le to kaffi h ....ya phir sabse pehle C learn kare.....? Please koi reply de dena
Bro no matter ki which language you choose, but if you do c first you easily understandable and clear the basics for java or c++. So I recommend you to do c language first then go for cpp or java
here's my code sliding window approch but little bit different implementation . i guess easy to understand. #include #include using namespace std; int smallestsubarray(int arr[], int n, int x) { if (n == 0) return -1; int lo = 0; //start int currentLength = 0; int ans = INT_MAX; int sum = 0; for (int i = 0; i < n; i++) { sum += arr[i]; currentLength++; if (sum > x) { while (sum > x) { sum -= arr[lo]; lo++; ans = min(currentLength, ans); // compare just before sum becomes less than x; currentLength--; } } } return ans; } int main() { int arr[] = {1, -5, 45, 55, 10, 8}; int n = sizeof(arr) / sizeof(arr[0]); int x = 51; int ans = smallestsubarray(arr, n, x); if (ans < n + 1) cout
u can again minimize the code like there is no use of that if condition and no need to find the length also just use two pointer tech int minSubArrayLen(int target, vector& nums) { int min_size=INT_MAX,sum=0; int left=0;
please recheck its not o(n) ,time limit exceeded.
Exactly
int shortestSubarray(vector& nums, int k)
{
int ans = INT_MAX,n = nums.size(),start = 0,sum = 0;
for(int i = 0;i < n;i++)
{
sum += nums[i];
if(sum >= k)
{
ans = min(ans,i - start + 1);
}
while(sum > k && start < i)
{
sum -= nums[start++];
if(sum >= k)
{
ans = min(ans,i - start + 1);
}
}
}
if(ans == INT_MAX)
{
return -1;
}
return ans;
}
This code is wrong as in the inner 1st while loop the condition should be sum>=x (go to 11:51) and in the given example the ans should be 2 (45+6) not 3 (go to 17:30).
true
no, actually we want the subarray sum to be "greater" than the given value, not "greater than or equal to", so ans will be 3
@@ayushikathait1312 ya u r right but for the coding platform like gfg..if you write the condition
Code is right. x is greater than equal to sum && n is greater than end.then sum +=arr[end++].output is 3 (45+6+4=55) because sum>x and it is also smallest subarray .but code is not work for negative elements.
but it is not working when we take nagative. element in array
How long this course will be please comment if anyone can predict
I guess course me basic topic ho chuke h
Sirf ye que k series hi chl rha h
Will not work if negative elements are present in the array
Then problem turns into binary search problem
#include
using namespace std;
int minSubarraySum(int arr[], int n, int x)
{
int k=0, sum=0, i=0, j=0, ans=INT_MIN;
while(ix)
{
sum-=arr[j];
j++;
}
}
if(j==0){
return ans+1;//no elements exist in the array that exceeds the accumulated value
}
return ans;
}
int main()
{
int arr[]={1, 4, 45, 6, 10, 19};
cout
Didn't we have to add 1 to the difference of end-start ??
No as the end already exceeds the original end by 1 when we do end++
@@aastikchaudhary3714 I was having the same doubt thank you
@@aastikchaudhary3714 i had missed this ....thanks for telling🙂
Sir ap bout accha explain Karte Ho
Sir java / javascript ya python ispar bhi banao sir plzzz
when I am trying to run same code on same question for LEETCODE it's not working.
It doen't work for negative no
It's not working because In this example she is taking only positive value but In leet code problem negative value is also present
From where to get notes of this lecture???
gfg
This course have no notes yet
Is it ok,
int MinSubArrSize(int arr[], int n , int x){
int ans = INT_MAX;
int sum = 0;
int index = 0;
for(int i = 0 ; i< n ; i++){
sum += arr[i];
if(sum > x and index < i){
ans = min(ans, i - index);
sum -= arr[index];
index++;
}
}
return ans;
}
Use while instead of if
time complexity jyada hogi iski
Waiting for Java course 🙂🙂
💯
when u didn't learn one language properly but you eager to learn more languages, that's why most people are unsuccessful to be a expert in coding
Haa bhai
@@al-hadees6236 maybe he is talking about java course left half way in apni kaksha channel.
choose only one coding language like Java or C++..if you learn both then may be you will stuck or contradict both language's syntax 😅
Hi bro/sir, i m not in 12th but can you please tell me that, one day one subject or one day multiple subject 🙏🙏🙏🙏 please tell me i m so much confused.
try one day multiple subject, but if you like to study one subject then go on with only one subject, and when you bored at one subject then move on with other subjects
Diii kya C language bhi shekna padhega.....agr java ya c++ me se koi ek shek le to kaffi h ....ya phir sabse pehle C learn kare.....?
Please koi reply de dena
depends job ke liye toh koi bhi lang chalega lekin operating systems ya robotics mei jana hai toh C sikhna padega
Bro no matter ki which language you choose, but if you do c first you easily understandable and clear the basics for java or c++. So I recommend you to do c language first then go for cpp or java
Are dp, graph and trees the important to get into a software company
for product based company it is must
Of Course !!
Thank you 🙏🙏
wrong solution if array contains negative values.
check constraints
can this code work if array elements include negative elements?
No
No , for that check leetcode 862 s solution
Bhaiya java course basic se start kariye
here's my code sliding window approch but little bit different implementation .
i guess easy to understand.
#include
#include
using namespace std;
int smallestsubarray(int arr[], int n, int x)
{
if (n == 0)
return -1;
int lo = 0; //start
int currentLength = 0;
int ans = INT_MAX;
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
currentLength++;
if (sum > x)
{
while (sum > x)
{
sum -= arr[lo];
lo++;
ans = min(currentLength, ans); // compare just before sum becomes less than x;
currentLength--;
}
}
}
return ans;
}
int main()
{
int arr[] = {1, -5, 45, 55, 10, 8};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 51;
int ans = smallestsubarray(arr, n, x);
if (ans < n + 1)
cout
hmm fairly simple
but you can return (ans==INT_MAX)? -1:ans)
we will check if no subarray is avaliable with sum >x
u can again minimize the code like there is no use of that if condition and no need to find the length also just use two pointer tech
int minSubArrayLen(int target, vector& nums)
{
int min_size=INT_MAX,sum=0;
int left=0;
for(int right=0;right=target)
{
min_size=min(min_size,right-left+1);
sum-=nums[left++];
}
}
return (min_size!=INT_MAX)?min_size:0;
}
one advice whenever u guys use sliding window technique try to use two pointer technique.
Dont anyone think that its Time complexity increases to O(n*n) coz there is a nested loop tooo...??
wrong solution he, if sum== target value toh voh length ko update nahi karta
Aap shraddha didi hai kya
test case pass nhi hore is se leetcode pe
😂
True love
Noice
Dislike kon karta hy yaar😠
Hote h bhai kuch chutiye log
@@anubhavgoyal2458 ha
@@ishmartishtiaq371 aap kha se ho bhai, college??
Thanks ❤️
Thank you 👍👍