How do you find the number of Subarrays whose Max K is?



From Google to Microsoft to Apple, every top tech giant throws some sort of complex coding questions on candidates! 


You can expect questions on arrays, longest palindromic subsequence, kth smallest element in a BST and other such questions. 


When we are talking about arrays, you may often encounter subarray related questions. 


One of the major subarray questions that we are going to discuss in this blog post is the maximum sum subarray of size k. Here, you need to find the maximum subarrays for a given sum. 


Know about this topic in detail by knowing its approaches.


So, let’s get started!



Problem Statement 


Consider a given array arr [] with N integers and an integer K. Your task would be to find the required number of subarrays whose maximum value would be equal to the k. 


Let’s consider an example:


Input is arr[] = [ 2, 1, 3, 4]


Here, k is 3 


The output would be 3 


All subarrays with their maximum value that is equal to k is [ 2, 1, 3] , [ 1, 3] [3]


So, the answer here is 3.



Simple Approach to follow 


To find the maximum subarray of size k, you need to consider the number of subarrays in an array. This arr[] can be equal to the total number of subarrays whose maximum sum may not be greater to k minus all the subarrays whose sum will not be greater than the k - 1. 


You can follow these steps:


  • Initialise all the variables from count 1 to count the number of subarrays whose sum will not be greater than the k-1

  • Initialise all variables as count 2 will be 2 for calculating the number of subarray whose sum is not greater to that of k 

  • Then define the function as total subarray in order to count all the number of subarrays by initialising the variable ans as a 0 in order to store the count of your subarrays with the maximum number is in the increasing value of i with 1. Then, initialise the variable count as 0 by storing the total possible values as subarrays. You can iterate the range in the way i would be less than the N with arr[i] in less than or equal to k.  Add the value in the value of ans 

  • Return the value as ans 

  • Store all the values [ count 2 - count 1] in your final value as answ in order to print it 



Brute Force Approach 


The simple solution for traversing all the arrays would be to use the brute force approach. You can calculate the sum and if that sum is equal to its required sum, the increment would be the desired count of an array. Then, return the count of such subarrays.


Algorithm to consider:


  • Initialise the given variable res with that of 0 in order to store the count of all its subarrays whose sum would give the value of k 

  • You can traverse an array by iterating it from the o till n. This would be the total size of your array which you need to traverse again with the starting index till the end of that array. 

  • You can keep the check on the subarray with its required index in order to see if the required incremented value will move ahead to check if all the subarrays are present in that particular array or not. 

  • Finally, you can return the outcome as the res


The time complexity in this case would be the O [N^2] where N signifies the number of desired elements in a given array arr. 


As we are iterating all the arrays in a given list with the arr to see how many subarrays are present in that ARR to give the sum equal to that of k with the increment res. So, that its complexity would go till O [ N^2]


Though, the space complexity in this case would be the O [1] as we will be using the constant space to get this space complexity.



Hashing Approach 


This would be an efficient solution in order to traverse a given array. In this case, we will keep on storing the sum so far as to get the ‘currsum’. Here, we also need to maintain different value counts in a currsum in order to get the desired value. 


The maximum sum subarray of size k would be done ideally with the help of this approach. If the value of the currsum is equal to the summ as an instance, we can increment the count of our subarray. 


The value of a given CURRSUM, would be exceeded by the value of a desired sum. And, if the value is obtained from that currsum, you can easily find the number of subarrays whose sum would be equal to that given sum.


Algorithm to follow:


  • Initialise the currsum variable as 0, in order to map or store all the elements of a given subarray whose sum would be equal to the given CURRSUM

  • Iterate through the given arr in order to check the value of given cursum whose value would be equal to sum of k

  • When the value exceeds its desired sum, you can increase the count of your subarrays to remove the given value from a currsum to get the desired value. In this case, you can map the obtained subarrays . the excluded subarrays would give you the current subarray to get a desired sum


O [N] would be the time complexity in this case. The number of array elements would be in the arr. As, we iterate over the elements of an array or list, we can calculate its prefix sum in order to check the complexity which will grow by the O[N]


The space complexity in this case would be the  O[N] with the given amount of spaces to store the respective sum with the map of index.



Wrapping Up 


From  maximum sum subarray of size k to reversing an array to kth smallest element in a BST, you can prepare all essential topics in a coding interview.


Learn about the maximum sum subarray with this blog post and fill your knowledge gap!


Comments

Popular posts from this blog

What are the 5 types of inheritance?

Remove duplicates from a sorted linked list

How often can you Buy and Sell the same stock?