Longest Subarray With Maximum Bitwise AND | Simple Observation | Leetcode 2419 | codestorywithMIK

preview_player
Показать описание
This is the 108th Video of our Playlist "Array 1D/2D : Popular Interview Problems" by codestorywithMIK

In this video we will try to solve a good Array Problem with Bit Magic Property : Longest Subarray With Maximum Bitwise AND | Simple Observation | Leetcode 2419 | codestorywithMIK

I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.

Problem Name : Longest Subarray With Maximum Bitwise AND | Simple Observation | Leetcode 2419 | codestorywithMIK
Company Tags : will update soon

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

Summary :
After observing the AND property and the problem statement, the goal is to find the length of the longest subarray that contains only the maximum element in the array.
Track Maximum Value: We maintain a variable maxVal to track the current maximum value encountered in the array.
Track Streak: As we traverse the array, we count the length of consecutive occurrences of maxVal (streak).
Update Maximum Streak: If we find an element equal to maxVal, we increase the streak. If the element exceeds maxVal, we reset the streak and start counting for the new maximum value.
Result: We continuously update the result with the longest streak of the maximum value encountered.
This approach ensures we traverse the array in a single pass (O(n) time complexity).

✨ Timelines✨
00:00 - Introduction

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #coding #programming #100daysofcode #developers #techjobs #datastructures #algorithms #webdevelopment #softwareengineering #computerscience #pythoncoding #codinglife #coderlife #javascript #datascience #leetcode #leetcodesolutions #leetcodedailychallenge #codinginterview #interviewprep #technicalinterview #interviewtips #interviewquestions #codingchallenges #interviewready #dsa #hindi #india #hindicoding #hindiprogramming #hindiexplanation #hindidevelopers #hinditech #hindilearning #helpajobseeker #jobseekers #jobsearchtips #careergoals #careerdevelopment #jobhunt #jobinterview #github #designthinking #learningtogether #growthmindset #digitalcontent #techcontent #socialmediagrowth #contentcreation #instagramreels #videomarketing #codestorywithmik #codestorywithmick #codestorywithmikc #codestorywitmik #codestorywthmik #codstorywithmik #codestorywihmik #codestorywithmiik #codeistorywithmik #codestorywithmk #codestorywitmick #codestorymik #codestorwithmik
Рекомендации по теме
Комментарии
Автор

Please upsolve videos of contest also, only 3 and 4.

Study-tk
Автор

maturity is when you realise this wasnt a bit question, , , you have taught this and concept in some of your previous videos i was thus able to do it myself thank you mik....if we take and of two numbers result always decreases

YashMalav-khov
Автор

Solve kar liya tha guruji khud se
My way was
Find max in the array
Find the maximum length of that sub array that contains the max number. ❤️

Kal wala pod ke karan hi aaj wala logic build huwa thank you guruji

aman_v
Автор

Solved myself but still came here sir!

AAO-CODING-KARE
Автор

Sir please make videos on bit masking, it's a humble request

aasthasachdeva
Автор

why we cannot done this by sliding window????

Love-xsmk
Автор

Sir Please make a video on maximize score of numbers in renges

AmitYadav-lodv
Автор

Solved on my own.
Here to support your MIK

FanIQQuiz
Автор

solved by sliding window

int longestSubarray(vector<int>& nums) {
int maxi=*max_element(begin(nums), end(nums));
int i=0, j=0;
int maxlen=INT_MIN;
int n=nums.size();
while(j<n){
if(nums[j]==maxi){
maxlen=max(maxlen, j-i+1);
j++;
}
else{
j++;
i=j;
}
}
return maxlen;
}

DevanshGupta-iorl
Автор

Bhaiyya pls solve
1397. Find All Good Strings pls

Study-tk
Автор

bhaiya please do code in c++ in videos

nothingtolose
Автор

Neither a sub array question nor bits its just your observation based question

debasismuduli
Автор

bit manipulation week hai!
suggestions to improve this topics!
I have finished your dp, graphs playlist!

mostlysane
Автор

thanks for giving some love to java..

waise logic jab pata chal gaya toh bada easy that code karna..

aizadiqbal
Автор

class Solution {
public:
int solve(vector<int> &nums, int maxi){
int n = nums.size();
int len = 1;
int i = 0;
int j = 0;
while(j < n){
if(nums[j] != maxi){
i = j;
i++;
j++;
}
else{
while(j < n && nums[j] == maxi){
j++;
}
len = max(len, j-i);
}
}
return len;
}
int longestSubarray(vector<int>& nums) {
int maxi = *max_element(begin(nums), end(nums));
int ans = solve(nums, maxi);
return ans;
}
};❤❤

utkarshsahay
Автор

*Java code:*

//TC: O(2n)
//SC: O(1)
public int longestSubarray(int[] nums) {
int maxi = 0;
int n = nums.length;
//calculating maximum bitwise AND
for(int val : nums) {
maxi = Math.max(maxi, val);
}
int count = 0;
int ans = 1;

//calculating longest subarray with maximum bitwise AND
for(int i = 0; i < n - 1; i++) {
if(nums[i] == maxi && nums[i] != nums[i+1]) {
ans = 1;
}
else if(nums[i] == maxi) {
ans++;
count = Math.max(count, ans);
}
}
count = Math.max(count, ans);
return count;

}

rohan
Автор

Sir is the segment tree playlist complete? if not please make videos on it when there is a easy potd....we do care about your busy schedule but there is no one on youtube who have taught it at the level you can teach🥲🥲

YashMalav-khov
Автор

Focus on the maximum element. Find the longest contiguous subarray where all elements are equal to this maximum value.

ankitdimri
Автор

in if (num > maxVal) {
maxVal = num;
result = 0;
streak = 0;
}

why we reset the result (result = 0;) ?

thecrowncreation
Автор

Bhaiya I finally got internship of 50k per month in vimo company as a java backend developer. Thanks to all your intuitive tutorials that made me think intuitively rather than just mugging up solution. In my interviews there were 5 dsa questions(dp*2, greedy, sliding window)and I'm able to do all those questions without any hint. Even the interviewer praised me lot. Btw I'm a final year student and I'm following the channel when there 5k(I guess) subs.

santhosh