Count Subarrays Where Max Element Appears at Least K Times | 2 Approaches | Leetcode 2962

Subscribers:
92,300
Published on ● Video Link: https://www.youtube.com/watch?v=06VaWkj8e-0



Duration: 36:06
8,033 views
375


Whatsapp Community Link : https://www.whatsapp.com/channel/0029Va6kVSjICVfiVdsHgi1A
This is the 18th Video of our Playlist "Sliding Window : Popular Interview Problems".

In this video we will try to solve an very classic sliding window problem :
Count Subarrays Where Max Element Appears at Least K Times | 2 Approaches | Leetcode 2962 | 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 : Count Subarrays Where Max Element Appears at Least K Times | 2 Approaches | Leetcode 2962 | codestorywithMIK
Company Tags : will update soon
My solutions on Github(C++ & JAVA) : https://github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Sliding%20Window/Count%20Subarrays%20Where%20Max%20Element%20Appears%20at%20Least%20K%20Times.cpp
Leetcode Link : https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/


My DP Concepts Playlist : https://youtu.be/7eLMOE1jnls
My Graph Concepts Playlist : https://youtu.be/5JGiZnr6B5w
My Recursion Concepts Playlist : https://www.youtube.com/watch?v=pfb1Zduesi8&list=PLpIkg8OmuX-IBcXsfITH5ql0Lqci1MYPM
My Sliding Window Playlist : https://www.youtube.com/watch?v=mrUBUWb23hk&list=PLpIkg8OmuX-J2Ivo9YdY7bRDstPPTVGvN&index=1
My GitHub Repo for interview preparation : https://github.com/MAZHARMIK/Interview_DS_Algo
Instagram : https://www.instagram.com/codestorywithmik/
Facebook : https://www.facebook.com/people/codestorywithmik/100090524295846/
Twitter : https://twitter.com/CSwithMIK
Subscribe to my channel : https://www.youtube.com/@codestorywithMIK

Approach Summary :
Approach-1 (Classic sliding window):
This approach utilizes the sliding window technique to solve the problem efficiently. It begins by identifying the maximum element in the array. Then, it iterates through the array using two pointers, `i` and `j`, to define the sliding window. The `j` pointer moves forward, while the `i` pointer adjusts when necessary to maintain the condition of having at most `k` occurrences of the maximum element within the window. The number of subarrays satisfying the condition is accumulated in the `result` variable. The time complexity of this approach is O(n), where n is the size of the input array, and the space complexity is O(1) since only a fixed number of variables are used irrespective of the input size.

Approach-2 (Without Sliding Window):
In this approach, the sliding window technique is not employed. Instead, it relies on a vector (`maxIndices`) to keep track of the indices where the maximum element occurs. It iterates through the array once, storing the indices of occurrences of the maximum element. Then, it checks for the condition of having at least `k` occurrences of the maximum element. For each occurrence after the `k`th, it calculates the number of subarrays that satisfy the condition and accumulates the result. The time complexity remains O(n) since it iterates through the array once, but the space complexity increases to O(n) due to the vector used to store indices.

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

✨ Timelines✨
00:00 - Introduction
2:20 - Approach-1 (Sliding Window)
13:07 - Coding Approach-1
14:46 - Approach-2 (Without Sliding Window)
23:08 - Multiple Dry Runs
34:17 - Coding Approach-2

#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 #newyear2024




Other Videos By codestorywithMIK


2024-04-07Valid Parenthesis String | 4 Detailed Approaches | Leetcode 678 | codestorywithMIK
2024-04-06Print Shortest Common Supersequence | DP On Strings | Leetcode 1092 | DP Concepts & Qns-21
2024-04-05Minimum Remove to Make Valid Parentheses | 3 Detailed Approaches | Leetcode 1249 | codestorywithMIK
2024-04-03Maximum Nesting Depth of the Parentheses | 2 Approaches | Leetcode 1614 | codestorywithMIK
2024-04-01Isomorphic Strings | Simple Thought Process | Leetcode 205| LinkedIn | codestorywithMIK
2024-03-31Length of Last Word | 2 Ways | Leetcode 58 | Amazon | codestorywithMIK
2024-03-31April, we are coming 🔥🔥🔥 #leetcode #maths #leetcodequestionandanswers #codestorywithmik
2024-03-30Count Alternating Subarrays | Super Easy | Leetcode 3031 | Weekly Contest 391 | codestorywithMIK
2024-03-30Subarrays with K Different Integers | Approach-2 | One Pass | Leetcode 992 | codestorywithMIK
2024-03-29Subarrays with K Different Integers | Approach-1 | With Reasons | Leetcode 992 | codestorywithMIK
2024-03-28Count Subarrays Where Max Element Appears at Least K Times | 2 Approaches | Leetcode 2962
2024-03-28Relatable ? 🥹 #codestorywithmik
2024-03-27Length of Longest Subarray With at Most K Frequency | 2 Ways | Leetcode 2958 | codestorywithMIK
2024-03-27Comment down your lines 👨🏻‍💻
2024-03-26Subarray Product Less Than K | Khandani Sliding Window template | Leetcode 713 | codestorywithMIK
2024-03-25First Missing Positive | In Depth Intuition | Clean Code | Leetcode 41 | codestorywithMIK
2024-03-24Longest Common Suffix Queries | Simple TRIE | Clean Code | Leetcode 3093 | codestorywithMIK
2024-03-24Most Frequent IDs | Complete Intuition | Leetcode 3092 | codestorywithMIK
2024-03-22Reorder List | 3 Approaches | Leetcode 143 | codestorywithMIK
2024-03-21Palindrome Linked List | 4 Approaches | Leetcode 234| codestorywithMIK
2024-03-20Task Scheduler | Using Greedy Only | No Heap | Leetcode 621 | codestorywithMIK