First negative integer in every window of size k | Love Babbar DSA sheet
Вставка
- Опубліковано 3 жов 2024
- If you wish to donate to the channel:
Google pay UPI ID: adimantheboss123@okaxis
-------------------------------------------------------------------------------------------------------------------------
A great E-book on Data structures and algorithms written by a software engineer who has himself worked in MNC's. Its available for a very affordable price.
Do check it out by clicking on the link below and boost your preparation.
imojo.in/ajGui...
-------------------------------------------------------------------------------------------------------------------------
Problems:
1. First negative integer in every window of size k:
practice.geeks...
solution:
practice.geeks...
-------------------------------------------------------------------------------------------------------------------------
Playlist link:
• 450 questions DSA sheet
Link to the questions:
drive.google.c...
Connect with me on Linked in:
/ aditya-rajiv-0606491a5
Connect with me on Instagram:
/ adi_rajiv
#lovebabbardsasheet #stacks #queues #gfg #Coding #Fun #SDE #Placement #competitiveprogramming
There is no need of j variable in this problem. Instead of j we can use (i-k)
Great explanation buddy!
Glad you liked it!
is this in O SPACE ? If not how can we optimise
im not sure about further optimization. but yeah this solution works!
This isn't O(1) space since we are using a queue to store the negative elements.
For O(1) we can maintain a pointer which keeps track of next negative element . Once our window crosses that pointer , we move the pointer to next negative element.
Code( accepted in gfg) :
vector sol ;
long long n =0 ; // pointer to negative elements
while(n < N && A[n] >=0)n++;
for(long long i=0 ; i=s && n=0)n++; // move pointer to next negative element
}
}
return sol;
Thanks!
Your welcome 😀.
Please share the channel with all your friends and subscribe 😀🙏.
Can't we do this with vector..?
try it maybe its not possible
Bro use list
@@adityarajiv6346 through vector
vector v;
int neg=0;
for(int i=0;i
Hi Aditya, queue works fine:
vector printFirstNegativeInteger(long long int A[],
long long int n, long long int k) {
vector v;
queue q;
int i=0;
int j=0;
while(j
Bhai explanation is not up to mark.
okay is shall c if i can explain better next time
Bro is competitive programming necessary??
It's good way to build problem solving skills
vector printFirstNegativeInteger(long long int a[],
long long int n, long long int k) {
vectorans;
queueq;
for(int i=0;i