Initially i thought i can not solve this problem. But applying brute force, i had solved it on first try. Then I came to your video. Thanks for the optimized approach ❤
Easy Question Tha Aaj Ka. Was able to solve it on my own. 100% faster. class Solution { public: vector resultsArray(vector& nums, int k) { int n = nums.size(); vector results(n - k + 1, -1); int j = 0; while (j < n - k + 1) { int i = j; bool isValid = true; while (i < j + k - 1) { if (nums[i] < nums[i + 1] && nums[i + 1] == nums[i] + 1) { i++; } else { isValid = false; break; } } if (isValid) { results[j] = nums[j + k - 1]; } j++; } return results; } };
can be done in one loop also. class Solution { public int[] resultsArray(int[] nums, int k) { int n=nums.length; int[] res=new int[n-k+1]; Arrays.fill(res,-1); int counter=1; for(int i=0;i0 && nums[i]==nums[i-1]+1){ counter++; } else{ counter=1; } if(counter>=k){ res[i+1-k]=nums[i]; } } return res; } }
TC: O(n), SC: O(1) class Solution { public: vector resultsArray(vector& nums, int k) { int power = 0, prevNum = INT_MAX, n = nums.size(); vector ans; for (int i = 0; i < n; i++) { if (nums[i] (prevNum + 1)) power = 1; else power++; if (i + 1 >= k && power != k) ans.push_back(-1); if(power == k) { ans.push_back(nums[i]); power--; } prevNum = nums[i]; } return ans; } }; //Brute force
You can choose to not preprocess it. I did it for getting a count value before hand. No need to preprocess. Check in the comments, someone has posted a code without preprocessing ❤️
Because when you are at jth element, it’s one element and we keep its count as 1 and we expect that in future we will see more consecutive elements to this jth element
finally after 3 wrong submissions I've successfully solved it in O(n) runtime and beats 100%. Feeling happy Thanks to you #MIK bhaiya here is my solution class Solution { public: vector resultsArray(vector& nums, int k) { int n = nums.size(); if (n == 1) return {nums[0]}; int f = -1; vector ans(n - k + 1, -1); for (int i = 1; i < k; i++) { if (nums[i] != nums[i - 1]+1) { f = (i - 1); } } if (f == (-1)) ans[0] = nums[k - 1]; int i = 1;
for (int j = k; j < n; j++) { if (k == 1) { ans[j - k + 1] = nums[j]; } else { if (i > f && nums[j] == nums[j - 1]+1) ans[j - k + 1] = nums[j]; else if(nums[j]!=nums[j-1]+1) f = (j - 1); } i++; } return ans; } };
you can do it in same loop as well. Both work fine. Here is the one loop code class Solution { public int[] resultsArray(int[] nums, int k) { int n=nums.length; int[] res=new int[n-k+1]; Arrays.fill(res,-1); int counter=1; for(int i=0;i0 && nums[i]==nums[i-1]+1){ counter++; } else{ counter=1; } if(counter>=k){ res[i+1-k]=nums[i]; } } return res; } }
Initially i thought i can not solve this problem. But applying brute force, i had solved it on first try. Then I came to your video. Thanks for the optimized approach ❤
same
❤️🙏
Tag krne waalo me se Ek Success story meri thi mik bhaiya Knight bn gya mai😁😁Thank you so much for your awesome explanations!!
congrats man.
@gui-codes Thanks broo
bhai mai to 1 ya 2 hi solve kar paata hun contest me pls help
@@universalcosmologist3675 Koi ni bhai Lage rho dheere dheere hojayga
@@dhruvchopra26Congrats bro...
Able to solve in my own, Intuition apne ap banne laga bhaiya... MIK magic✨❤
Way to go ❤️
@@codestorywithMIK ❤🙌
keep doing such a kind of series (if possible contest discussion as well),god bless you good health and life .
Means a lot ❤️🙏
Solved the problem in n*k TC but still not satisfied. SO watching your video now😁😁😁
i solved it using the optimized method,thanks to your previous videos
Great 👍
to not support brute force the length of array should be >= 10^5, great problem
Thanks for the videos.
loved your optimised approach ♥
Radhe Radhe ❤
Was able to solve 👌🏻
pls solve using monotonic queue as well
th-cam.com/video/7xkCA80h5a4/w-d-xo.htmlsi=BTZmLnId5xMES3gg
Hope this helps ❤️🙏
@codestorywithMIK thanks a lot ❤
@@codestorywithMIK Much appreciated.
Solved before the video using same concept!
thank you bhiay
Chalo bhaiya Aaj logic toh ban gaya tha but kuch mistake thi magar you me or saath saath aur acchi practice se bhi mistake bhi aana band ho jayegi ❤
Way to go ❤️
Unique
class Solution {
public:
vector resultsArray(vector& nums, int k) {
int n=nums.size();
vectorans(n-k+1,-1);
int i=0,j=0;
while(j0 && nums[j]-nums[j-1]!=1){
i=j;
}
while(ik){
i++;
}
if(j-i+1==k)
ans[j-k+1]=nums[j];
j++;
}
return ans;
}
};
class Solution {
public:
vector resultsArray(vector& nums, int k) {
int n = nums.size();
vector ans(n-k+1);
for(int i = 0 ; i
Easy Question Tha Aaj Ka. Was able to solve it on my own.
100% faster.
class Solution {
public:
vector resultsArray(vector& nums, int k) {
int n = nums.size();
vector results(n - k + 1, -1);
int j = 0;
while (j < n - k + 1) {
int i = j;
bool isValid = true;
while (i < j + k - 1) {
if (nums[i] < nums[i + 1] && nums[i + 1] == nums[i] + 1) {
i++;
} else {
isValid = false;
break;
}
}
if (isValid) {
results[j] = nums[j + k - 1];
}
j++;
}
return results;
}
};
Awesome♥️
class Solution {
public:
vector resultsArray(vector& nums, int k) {
vector ans;
// [1,2,3,4,3,2,5], k = 3
vector temp(nums.size(), 1); // 1 2 3 4 1 1 2
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] == nums[i - 1] + 1) {
temp[i] = temp[i - 1] + 1;
}
}
int currMax = INT_MIN;
for (int i = 0; i < k; ++i) {
currMax = max(currMax, nums[i]);
}
int i = 0, j = k - 1;
while (j < nums.size()) {
if (temp[i] + (k - 1) != temp[j]) {
ans.push_back(-1);
}
else {
ans.push_back(currMax);
}
if (currMax == nums[i]) {
currMax = INT_MIN;
}
if (j + 1 < nums.size()) {
currMax = max(currMax, nums[j + 1]);
}
i++, j++;
}
return ans;
}
}; This is Accepted Solution for problem. Can someone suggest me any improvements in the code quality
can be done in one loop also.
class Solution {
public int[] resultsArray(int[] nums, int k) {
int n=nums.length;
int[] res=new int[n-k+1];
Arrays.fill(res,-1);
int counter=1;
for(int i=0;i0 && nums[i]==nums[i-1]+1){
counter++;
}
else{
counter=1;
}
if(counter>=k){
res[i+1-k]=nums[i];
}
}
return res;
}
}
I have done till sliding but when i saw this count was like aha...
Brute-Force :
T.C --> O(n^2) || S.C --> O(1)
class Solution {
int checkSorted(vector &nums, int idx, int k) {
for(int i = idx; i < (idx + k - 1); i++) {
if(nums[i+1] != (nums[i] + 1))
return -1;
}
return nums[idx + k - 1];
}
public:
vector resultsArray(vector& nums, int k) {
int n = nums.size();
vector ans;
for(int i = 0; i
you can easily solve in O(n)
@@rickdutta942 Yeah, but this approach is just for intuition building for beginners. 1st approach to discuss in the interview.
@@AS-gf3ci Agreed.
@@AS-gf3ci yes bro agree.
bhaiya can you please make a video for post contest solution
TC: O(n), SC: O(1)
class Solution {
public:
vector resultsArray(vector& nums, int k) {
int power = 0, prevNum = INT_MAX, n = nums.size();
vector ans;
for (int i = 0; i < n; i++) {
if (nums[i] (prevNum + 1)) power = 1;
else power++;
if (i + 1 >= k && power != k) ans.push_back(-1);
if(power == k) {
ans.push_back(nums[i]);
power--;
}
prevNum = nums[i];
}
return ans;
}
}; //Brute force
Please use a black screen instead of a white one, as the high contrast of white is uncomfortable for the eyes.
Sure thing. Actually I use black screen only. Actually I am currently travelling and it slipped my mind to use black screen.
Why 1st window is preprocessed ??
You can choose to not preprocess it. I did it for getting a count value before hand. No need to preprocess. Check in the comments, someone has posted a code without preprocessing ❤️
at first glance its too much difficult to think this otimized version
Brute Force:
T.C: O(N*K)
class Solution {
public:
int isposs(vector&temp){
for(int i=0;i0) res.push_back(ans);
else res.push_back(-1);
}
}
return res;
}
};
pls tell me why we set consecutive count to 1 when a non consecutive element is seen
Because when you are at jth element, it’s one element and we keep its count as 1 and we expect that in future we will see more consecutive elements to this jth element
@codestorywithMIK thankyou bhaiya understood
Brute forces
public int[]resultsArray(int[]nums,int k){
int n=nums.length;
int[]ans=new int[n-k+1];
for(int i=0;i
Not able to do own my own feeling low...
How long have you been dng bro
@@tauheedahmed4073 from may 2024 started Dsa
Don’t feel low.
This is the process of learning. Such phase will always come. With consistency, things will improve slowly but definitely ❤️
finally after 3 wrong submissions I've successfully solved it in O(n) runtime and beats 100%. Feeling happy Thanks to you #MIK bhaiya
here is my solution
class Solution {
public:
vector resultsArray(vector& nums, int k) {
int n = nums.size();
if (n == 1)
return {nums[0]};
int f = -1;
vector ans(n - k + 1, -1);
for (int i = 1; i < k; i++) {
if (nums[i] != nums[i - 1]+1) {
f = (i - 1);
}
}
if (f == (-1))
ans[0] = nums[k - 1];
int i = 1;
for (int j = k; j < n; j++) {
if (k == 1) {
ans[j - k + 1] = nums[j];
} else {
if (i > f && nums[j] == nums[j - 1]+1)
ans[j - k + 1] = nums[j];
else if(nums[j]!=nums[j-1]+1)
f = (j - 1);
}
i++;
}
return ans;
}
};
Glad to see this ❤️
monotonic and please ... and why are we processing the first window .... seperately why not in that loop only
you can do it in same loop as well. Both work fine.
Here is the one loop code
class Solution {
public int[] resultsArray(int[] nums, int k) {
int n=nums.length;
int[] res=new int[n-k+1];
Arrays.fill(res,-1);
int counter=1;
for(int i=0;i0 && nums[i]==nums[i-1]+1){
counter++;
}
else{
counter=1;
}
if(counter>=k){
res[i+1-k]=nums[i];
}
}
return res;
}
}
Using Monotonic deque -
th-cam.com/video/7xkCA80h5a4/w-d-xo.htmlsi=BTZmLnId5xMES3gg
Hope this helps ❤️