Obviously, there can be more of such pairs. \begin{align} Time Complexity: O(N)Auxiliary Space: O(N). = \binom{n}{1}$$. Browse other questions tagged, Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide, The future of collective knowledge sharing. Example,, if I want to count for 1,2. For What Kinds Of Problems is Quantile Regression Useful? Given an array arr[] consisting of N integers, the task is to create an array brr[] of size N where brr[i] represents the count of subarrays in which arr[i] is the smallest element. Algorithm to Sort Pairs of Numbers | Saturn Cloud Blog Why is {ni} used instead of {wo} in ~{ni}[]{ataru}? I start with looking from some K length subarrays (example K = 3). Contribute your expertise and make a difference in the GeeksforGeeks portal. A subarray has to have contiguous elements. [5,3] , [6,2] and [5,3,6,2] has the mean 4. (>= 4)For subarray [5, 2], median is 5. For each element let L and R be the boundary indices on the left and right side respectively up to which arr [i] is the minimum. Thanks! Asking for help, clarification, or responding to other answers. For each rightmost element (A[j]-average), we want to add the number of times we've seen the same prefix sum before. Can you please clarify how you determine "the number of times we've seen the same prefix sum before"? Array looks like this: where X denotes the marked position. 3. An efficient approach is based on below facts : We keep track of two counts in the current subarray. To subscribe to this RSS feed, copy and paste this URL into your RSS reader. So the total number of k-element subarrays, for some particular k, must correspond to the k-element window that includes the Nth item (i.e., the window furthest to the right). Let's see, this adhoc pseudocode should be O(n): Note **1: This seems to involve a loop inside the outer loop. Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Approach: The key observation fact for the count of the subarray is the number of ends position possible for each index elements of the array can be (N - i), Therefore the count of the subarray for an array of size N can be: Count of Sub-arrays = (N) * (N + 1) --------------- 2 The time complexity of this naive method is O (n^2) where $n$ is the length of a. \; \; \; 1 & \; \text{subarray only containing} \; a_0\\ For arr[] = {1, 2, 3}, sum of subarrays is: arr[0] * ( 0 + 1 ) * ( 3 0 ) + arr[1] * ( 1 + 1 ) * ( 3 1 ) + arr[2] * ( 2 + 1 ) * ( 3 2 ), Time complexity: O(N)Auxiliary Space: O(1). So is S. Adding and finding the average in an array, Java: Finding average of doubles in array, Maximum possible average of an array sub sequence -- Need efficient solution. Input: arr[] = {4, 1, 3, 2, 5, 6}Output: 5Explanation:Desired Sub-arrays = { {4, 1, 3, 2}, {1}, {1, 3, 2}, {4, 1, 3, 2, 5}, {4, 1, 3, 2, 5, 6} }Count(Sub-arrays) = 5, Input: arr[] = {3, 2, 4, 1}Output: 2Explanation:Desired Sub-arrays = { {1}, {3, 2, 4, 1} }Count(Sub-arrays) = 2. (>= 4)For subarray [5, 2, 4, 1], median is 4. I didn't forget {3,1}. Introduction 2. beginner - Taking out number of subarrays from an array which contains all the distinct elements of that array - Code Review Stack Exchange Taking out number of subarrays from an array which contains all the distinct elements of that array Ask Question Asked 3 years, 11 months ago Modified 3 years, 11 months ago Viewed 196 times 6 Did active frontiersmen really eat 20,000 calories a day? Number of Subarrays with Bounded Maximum - LeetCode Share your suggestions to enhance the article. Count the number of subarrays | Practice | GeeksforGeeks If multiple of such pairs exist, we repeat the same trick on [p2, , b] as if it was the whole array. & \; \ldots \\ rev2023.7.27.43548. Has these Umbrian words been really found written in Umbrian epichoric alphabet? It boils down to this; given an array X and an integer S, how many contiguous fragments of X has arithmetic mean equal to S? Example 1: Input: N = 3, L = 3, R = 8 A[] = {1, 4, 6} Output: 3 Explanation: The subarrays. Optimal Approach - Hashing 5.2.1. :), No! Now recall Pascal triangle and recurrence relation $$\binom{n+1}{r} = \binom{n}{r} + \binom{n}{r-1}$$. Split array into K subarrays such that sum of maximum of all subarrays is maximized, Split given arrays into subarrays to maximize the sum of maximum and minimum in each subarrays, Check if Array can be split into subarrays such that XOR of length of Longest Decreasing Subsequences of those subarrays is 0, Differences between number of increasing subarrays and decreasing subarrays in k sized windows, Split given Array in minimum number of subarrays such that rearranging the order of subarrays sorts the array, Sum of Bitwise-OR of all subarrays of a given Array | Set 2, For all Array elements find Product of Sum of all smaller and Sum of all greater elements, Sum of products of all possible Subarrays, Maximize product of min value of subarray and sum of subarray over all subarrays of length K, Mathematical and Geometric Algorithms - Data Structure and Algorithm Tutorials, Learn Data Structures with Javascript | DSA Tutorial, Introduction to Max-Heap Data Structure and Algorithm Tutorials, Introduction to Set Data Structure and Algorithm Tutorials, Introduction to Map Data Structure and Algorithm Tutorials, A-143, 9th Floor, Sovereign Corporate Tower, Sector-136, Noida, Uttar Pradesh - 201305, We use cookies to ensure you have the best browsing experience on our website. Explanation: Desired Sub-arrays = { {4, 1, 3, 2}, {1}, {1, 3, 2}, {4, 1, 3, 2, 5}, {4, 1, 3, 2, 5, 6} } Count (Sub-arrays) = 5 Input: arr [] = {3, 2, 4, 1} Output: 2 Explanation: Desired Sub-arrays = { {1}, {3, 2, 4, 1} } Count (Sub-arrays) = 2 By using our site, you Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all. In our case, we will select the first element as the pivot. Given an array 'A' consisting of 'N' integers and an integer 'B', find the number of subarrays of array 'A' whose bitwise XOR( ) of all elements is equal to 'B'. (1,2,3),(1,2,3),1,2,2,2,3. Input: arr[] = {1, 2, 3}Output: 20Explanation: {1} + {2} + {3} + {2 + 3} + {1 + 2} + {1 + 2 + 3} = 20. To learn more, see our tips on writing great answers. 9 Anonymous User August 13, 2021 1:52 AM 6.3K VIEWS Question : Count the number of strictly increasing subarray of a specified size k in an array of size N . For each element let L and R be the boundary indices on the left and right side respectively up to which arr[i] is the minimum. Making statements based on opinion; back them up with references or personal experience. Pay and get the solution on display in a text file automaticAfter payment Correct answer( Correct solution)LeetCode Solution:-(A) Number of Employees Who Met. Count of K length subarrays containing only 1s in given Binary String, Count of K length subarrays containing only 1s in given Binary String | Set 2, Maximize product of min value of subarray and sum of subarray over all subarrays of length K, Find all K length subarrays containing only 1s in given Binary String, Count of Subarrays not containing all elements of another Array, Count ways to partition Binary Array into subarrays containing K 0s each, Maximize subarrays count containing the maximum and minimum Array element after deleting at most one element, Partition array into two subarrays with every element in the right subarray strictly greater than every element in left subarray, Reduce given array by replacing subarrays of length at least K consisting of even numbers with their length, Find length of the longest subarray containing atmost two distinct integers, Mathematical and Geometric Algorithms - Data Structure and Algorithm Tutorials, Learn Data Structures with Javascript | DSA Tutorial, Introduction to Max-Heap Data Structure and Algorithm Tutorials, Introduction to Set Data Structure and Algorithm Tutorials, Introduction to Map Data Structure and Algorithm Tutorials, A-143, 9th Floor, Sovereign Corporate Tower, Sector-136, Noida, Uttar Pradesh - 201305, We use cookies to ensure you have the best browsing experience on our website. Example: [1, 2, -3, 2, - 2, 4, -1], prefix sums: [1, 3, 0, 2, 0, 4, 3], the contiguous sums between identical prefix values. Naive Approach: The simplest approach to solve this problem is to generate all possible subarrays from the given array and for each subarray, check if its product is even or not. What should the result be in this case and in others, and what debugging have you done to try to figure out what the problem is? Before moving to the problem, we ask you, what actually are subarrays of an array. Contribute your expertise and make a difference in the GeeksforGeeks portal. Here is the code with tests (Scala): In most cases, N vs NlogN doesn't matter. How to check if two given sets are disjoint? @Polygnome Can you elaborate on "mean of the path of values to reach the node"? Given an array of integers arr and two integers k and threshold, return the number of sub-arrays of size k and average greater than or equal to threshold. the sum of the terms of an arithmetic sequence). = \binom{n+1}{2} In the end, all paths to the leaves with mean = S are the solution. @Sumurai8, if the original array contains duplicate numbers and they form a subarray and are not part of the required pair, yes. For calculating the number of subarray with a sum greater than or equal to 0: Traverse the newly created prefix array starting from index. Would appreciate some help on how to approach it. Approach: The problem can be solved based on the following idea. Assuming the input sequence: $(a_0, a_1, \ldots, a_n)$, we can count all subarrays as follows: $ Do you mean contiguous subarrays (If not, your example is wrong, ex [1, 3])? acknowledge that you have read and understood our. With 3 . By using our site, you N) we will try to find. Approach of Subarray Sum 5.1. The four values of 0 in Q give rise to six ranges of average S=4: [5,3], [6,2], [5,5,2], [5,3,6,2], [6,2,5,5,2] and [5,3,6,2,5,5,2]. Help us improve. Implementation in Java 5.1.4. \; 1 & \; \text{subarray from} \; a_0 \; \text{to} \; a_{n-1}\\ For instance, given X=[5,3,6,2] and S=4 result is 3. The above can also be represented as $\sum_{i=1}^{n}i\;$ and adds up to $n (n+1)/2$. You will be notified via email once the article is available for improvement. Count Subarrays with Given XOR - Coding Ninjas The count of subarrays with an odd product is equal to the total number of continuous odd elements present in the array. For example, Consider the array [1, 2, 3, 4], There are 10 non-empty sub-arrays. can there be duplicate numbers in that array? You might have studied about a subset of a set in your school. $. Find centralized, trusted content and collaborate around the technologies you use most. acknowledge that you have read and understood our. In general, for an array of size n, there are n* (n+1)/2 non-empty subarrays. But 2 occurrences have already come in subarrays of length 3"1,2,3". = \binom{n}{1} + \binom{n}{2} OverflowAI: Where Community & AI Come Together, Finding Arithmetic Mean of Subarrays Efficiently in an Array, belongs on Software Recommendations.SE, as per this meta, Behind the scenes with the folks building OverflowAI (Ep. Example: Input: 'N' = 4 'B' = 2 'A' = [1, 2, 3, 2] Output: 3 Explanation: Subarrays have bitwise xor equal to '2' are: [1, 2, 3, 2], [2], [2]. By clicking Post Your Answer, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct. Making statements based on opinion; back them up with references or personal experience. Brute Force Approach 5.1.1. By following the step-by-step guide and implementing the provided algorithm, you can subdivide arrays into sub-arrays that maintain a balanced distribution of elements. We basically remove all those subarrays which are formed by only elements smaller than L. Below is the implementation of the above approach-. Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. Find centralized, trusted content and collaborate around the technologies you use most. Count of subarrays starting or ending at an Index i such that arr [i Thank you for your valuable feedback! arrays - Count of subarray - Stack Overflow Could the Lightning's overwing fuel tanks be safely jettisoned in flight? Counting appropriate number of subarrays in an array excluding some specific pairs? Is it normal for relative humidity to increase when the attic fan turns on? :). By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. How to help my stubborn colleague learn new ways of coding? Contribute your expertise and make a difference in the GeeksforGeeks portal. I tried applying Sliding Window Technique but could not come up with an optimal solution . $$\binom{n+1}{r} = \binom{n}{r} + \binom{n}{r-1}$$, $\sum_{k=1}^{N}N-k+1\ = N + (N-1) + (N-2) + + 3 + 2 + 1 = (N+1)(N/2)$, Stack Overflow at WeAreDevelopers World Congress in Berlin, Calculating possible combinations for 1-3 digit code, Finding number of subarrays not including certain pairs, Counting number of times a given segment will occur in all subarrays, Number of occurrences in contiguous subarrays, Number of $N$-size configurations of coins to total a specific sum, Number of nonempty subsets of a set with $n$ elements. Partition the array of pairs into two subarrays, one containing pairs with first elements less than the pivot and the other containing pairs with first elements greater than the pivot. By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. This article is contributed by Nishant Singh. The British equivalent of "X objects in a trenchcoat". I don't consider the sub-arrays in my result set whose frequency comes out to be 1. Imagine a virtual element $a_{n+1}$ at the end; it doesnt matter what its value is. For example [2] appears in [2] and [2, 3]. Using a comma instead of and when you have a subject with two verbs. Time Complexity 5.1.6. This article is being improved by another user right now. By observation, you can see that each result is just an addition of previous result and current number. 594), Stack Overflow at WeAreDevelopers World Congress in Berlin, Temporary policy: Generative AI (e.g., ChatGPT) is banned, Preview of Search and Question-Asking Powered by GenAI, Find the number of repeated subarray in an array, Number of Subarray whose sum greater than given value, Counting contigious subsequences in an array with given pair of indices, Frequency of each element of an array considering all contiguos subarrays. To optimize the above approach we can use a Stack Data Structure. With 1 , 3 subarrays are formed. Now iterate over this vector and check whether the difference of maximum and minimum index till the ith element is less than the number of elements iterated till now, which is the value of i itself. Can't align angle values with siunitx in table. There are (n-i) such subsets. You will be notified via email once the article is available for improvement. Naive Approach: To solve the problem follow the below idea: A simple solution is to generate all sub-arrays and compute their sum. Note: Each value of X is an integer in Not the answer you're looking for? + \; 1 &\; \text{subarray from} \; a_1 \; \text{to} \; a_{n-1}\\ Subarray Product Less Than K - LeetCode Thank you for your valuable feedback! Hence, the last 2,3, only comes in the count. Let, Count of all elements smaller than or equal to R. We call it. Is the DC-6 Supercharged? 5. Please correct me wherever I am wrong. Examples : Input: arr [] = { 7, 5, 4, 9 } Output: 6 Explanation: There are total 6 subarrays { 4 } { 5, 4 } { 7, 5, 4 } { 7, 5, 4, 9 } { 5, 4, 9 } A [1,2,3] It has 6 subarrays : The name of the k-element window then keeps count of the number of k-element subarrays. Input: N = [3, 7, 2, 0, 1, 5], X = 10Output: 0Explanation: There are no subarrays with median greater than or equal to X. An example will really help. I think that for a vector of N elements the total number of subarrays is N*(N+1)/2. algorithm - Counting appropriate number of subarrays in an array The amount of subarrays in an array of length 3 is, for example, 3 + 2 + 1 = 6 = (n(n+1))/2. The subarray corresponding to indices [1, 1] will be {5}. Has these Umbrian words been really found written in Umbrian epichoric alphabet? After completing the above steps, print all the counts stored in the resultant array. Eliminative materialism eliminates itself - a familiar idea? How can I find the shortest path visiting all nodes in a connected graph as MILP? Input: arr[] = {1, 2, 3, 4, 5}Output: {5, 4, 3, 2, 1}, Naive Approach: The simplest approach is to generate all subarrays of the given array and while generating the subarray, find the element which is minimum in that subarray and then store the index of that element, then later increment count for that index by 1. I was thinking the answer should be n C 2, instead of n+ 1 C 2 that you mentioned. Given an array, compute the number of subarrays with a mean = k 1 <= n <= 1e5 1 <= a [i], k <= 1e9 Thanks! Time Complexity: O(N2)Auxiliary Space: O(N). Browse other questions tagged, Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide, The future of collective knowledge sharing. I have an array of n integers. How many subarrays are contained - Quora Next, how many subarrays of length two are there? Eg: Array= [ 5,3,5,7,8] and k=3 , The answer is 2 as [3,5,7] and [5,7,8] are the answer. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected]. The solution uses that in a given array [a, , p1, p2, , b], the amount of subarrays without the pair is the amount of subarrays for [a, , p1] + the amount of subarrays for [p2, , b]. Algebraically why must a single square root be done on all terms rather than individually? Help us improve. Brilliant idea from using prefix sum and dictionary to check any seen of previous sum before. Input: arr[] = {3, 2, 4}Output: {1, 3, 1}Explanation:For arr[0], there is only one subarray in which 3 is the smallest({3}). See your article appearing on the GeeksforGeeks main page and help other Geeks. New! How do I keep a party together when they have conflicting goals? Any element > R is never included in any subarray. Count of subarray 1,2,2 is C1:2. By using our site, you You will be notified via email once the article is available for improvement. Connect and share knowledge within a single location that is structured and easy to search. = total_1 + total_* How do you find the number of contiguous subarrays of size Similarly, count of 2,2 is 1. Thank you for your valuable feedback! Help us improve. My code below(on Java) works for small set of data but it is not efficient. Implementation in C++ 5.1.5. Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. Our answer for current subarray is countSubarrays(inc) countSubarrays(exc). Examples: Input: N=4, A = [5, 2, 4, 1], X = 4 . Given an array arr[] of length N containing all elements from 1 to N, the task is to find the number of sub-arrays that contain numbers from 1 to M, where M is the length of the sub-array. Let countSubarrays (N) = N * (N + 1)/2 We keep track of two counts in the current subarray. Connect and share knowledge within a single location that is structured and easy to search. Utilize a tree. So, we already have sum 0-1 in the hashmap i.e. This can be written as a summation: $\sum_{k=1}^{N}N-k+1\ = N + (N-1) + (N-2) + + 3 + 2 + 1 = (N+1)(N/2)$. How to draw a specific color with gpu shader. The task is to find the number of subarrays having sum in the range L to R (inclusive). Count of Subarrays in an array containing numbers from - GeeksforGeeks You don't need to find which subarrays are in an array to know how many of them there are. Given an array arr[]of integers with length N and an integer X, the task is to calculate the number of subarrays with median greater than or equal to the given integer X. But (1,2) is a subset of the subarray 1,2,2. How do I keep a party together when they have conflicting goals? To achieve the desired subdivision, we can use the following algorithm: Calculate the size of each sub-array, subarray_size, by dividing the total number of elements in the array, n, by the desired number of sub-arrays, k. This gives us an initial estimate of the size of each sub-array. Learn more about Stack Overflow the company, and our products. +9 manik.jain 2 years ago 12 Show archived | Write comment? e.g. In the above case, mark all the occurrences of 1,2,3 and 2,3,1. Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k. A subarray is a contiguous non-empty sequence of elements within an array. Given an array of numbers, let's say, 1,2,2,3,2,1,2,2,2,2 I look for subarrays and count the frequency of each. Count the number of subarrays with given xor K - takeuforward But 2,3 is also a subarray of 2,3,1 and 2,2,3. Counting appropriate number of subarrays in an array excluding some specific pairs? A subarray is a contiguous part of array, i.e., Subarray is an array that is inside another array. = & \; n Thank you for your valuable feedback! Idea is that, For each (1? Ex. Example 2: Input: nums = [1,2,3], k = 3 Output: 2. Thanks for contributing an answer to Stack Overflow! Do the 2.5th and 97.5th percentile of the theoretical sampling distribution of a statistic always contain the true population parameter? Data Structure & Algorithm Classes (Live), Data Structure & Algorithm-Self Paced(C++/JAVA), Full Stack Development with React & Node JS(Live), Top 100 DSA Interview Questions Topic-wise, Top 20 Interview Questions on Greedy Algorithms, Top 20 Interview Questions on Dynamic Programming, Top 50 Problems on Dynamic Programming (DP), Commonly Asked Data Structure Interview Questions, Top 20 Puzzles Commonly Asked During SDE Interviews, Top 10 System Design Interview Questions and Answers, Indian Economic Development Complete Guide, Business Studies - Paper 2019 Code (66-2-1), GATE CS Original Papers and Official Keys, ISRO CS Original Papers and Official Keys, ISRO CS Syllabus for Scientist/Engineer Exam, Find minimum subarray length to reduce frequency, Length of longest common subarray for given N arrays, Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum, Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible, Minimize the maximum element of N subarrays of size K, Count quadruples (i, j, k, l) in an array such that i < j < k < l and arr[i] = arr[k] and arr[j] = arr[l]. This article is being improved by another user right now. Can Henzie blitz cards exiled with Atsushi? Would [2,3] and [3,2] be counted different arrays? And what is a Turbosupercharger? \; 1 & \; \text{subarray from} \; a_0 \; \text{to} \; a_{n-2}\\ would [1,2,2] be a valid subarray to sum up to 5? = \binom{n}{1} + \binom{n}{2}$$. replacing tt italic with tt slanted at LaTeX level? Now, I look for subarrays of length 2. You will be notified via email once the article is available for improvement. One common problem is subdividing an array into semi-equal or uniformly distributed sub-arrays. After finding it for an index, add the current value to a. Count of Subarrays of given Array with median at least X + \; 1 &\; \text{subarray from} \; a_1 \; \text{to} \; a_{n-2}\\ Print the obtained value of the count of subarrays. The time complexity of this solution is O(n*n). Manga where the MC is kicked out of party and uses electric magic on his head to forget things, How do I get rid of password restrictions in passwd. $, $ Example: Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Number of subarrays with maximum values in given range & \; \ldots \\ 1,2,3,1,2,3,1,2,2,3. Join two objects with perfect edge-flow at any stage of modelling? Algorithm 5.1.2. Thank you for your valuable feedback! For an array [1, 2, 3, 4, 5] with pair [2, 3], index will be 2 when the two if-statements are true. Construct and count the number of subarrays of size k, starting with k = 1 and ending at k = N. Consider k as the size of a k-element window that scans through the items from left to right. What is Mathematica's equivalent to Maple's collect with distributed option? Also, initially when we are at index =-1 (hypothetically), the sum is 0. Why is the expansion ratio of the nozzle of the 2nd stage larger than the expansion ratio of the nozzle of the 1st stage of a rocket? Input: nums = [2,1,4,3], left = 2, right = 3 Output: 3 . Thanks for contributing an answer to Stack Overflow! How do you understand the kWh that the power company charges you for? Number of subarrays having even product zack_aayush Read Discuss Courses Practice Given an array arr [] consisting of N integers, the task is to count the total number of subarrays having even product. = total_1 + total_* Has these Umbrian words been really found written in Umbrian epichoric alphabet? Were all of the "good" terminators played by Arnold Schwarzenegger completely separate machines. Efficient Approach: To optimize the above approach, the idea is to find the boundary index for every element, up to which it is the smallest element. Connect and share knowledge within a single location that is structured and easy to search. Count of all elements smaller than or equal to R. We call it inc . Now let's assume we count all the sub-string that are not single character. Elaborating on Brian's answer, lets assume we count all the single character sub-strings. Total of above (i) and (ii) = (n-i) + (n-i)*i = (n-i)(i+1). Recommended PracticeSum of SubarraysTry It! Why is the expansion ratio of the nozzle of the 2nd stage larger than the expansion ratio of the nozzle of the 1st stage of a rocket? 1 Answer Sorted by: 3 Instead of looking at the answer for a general value of k k, let's look at specific examples. Initialize an empty list, result, to store . Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. By using our site, you How to draw a specific color with gpu shader, "Who you don't know their name" vs "Whose name you don't know". In this article, we will explore an algorithmic approach to solve this problem and provide a step-by-step guide on how to implement it. ii) In (n-i)*i subarrays where this element is not first element. Follow the below steps to solve the problem: Generate all subarrays using nested loops Take the sum of all these subarrays Below is the implementation of the above approach: C++ Java Python3 C# PHP Javascript #include <bits/stdc++.h> using namespace std; long int SubArraySum (int arr [], int n) { long int result = 0, temp = 0; Data Structure & Algorithm Classes (Live), Data Structure & Algorithm-Self Paced(C++/JAVA), Full Stack Development with React & Node JS(Live), Top 100 DSA Interview Questions Topic-wise, Top 20 Interview Questions on Greedy Algorithms, Top 20 Interview Questions on Dynamic Programming, Top 50 Problems on Dynamic Programming (DP), Commonly Asked Data Structure Interview Questions, Top 20 Puzzles Commonly Asked During SDE Interviews, Top 10 System Design Interview Questions and Answers, Indian Economic Development Complete Guide, Business Studies - Paper 2019 Code (66-2-1), GATE CS Original Papers and Official Keys, ISRO CS Original Papers and Official Keys, ISRO CS Syllabus for Scientist/Engineer Exam, Minimum operations to make all Array elements 0 by MEX replacement, Count subarrays containing at least K zero, Maximum sum of a Subarray with prime integers, Find the length of the longest subarray with atmost K occurrences of the integer X, Minimize difference between maximum and minimum element of all possible subarrays, Check if all array elements can be converted to K using given operations, Print the longest increasing consecutive subarray, Count of elements such that difference between sum of left and right sub arrays is equal to a multiple of k, Program to Reverse the subarray in between given range, Maximum length of longest increasing contiguous subarray after deleting exactly one element from array, Minimum moves to sort Binary Array in increasing order by deleting Subarray, Maximum sum after K consecutive deletions, Find the total number of Subarrays of 0s, Merge the elements in subarray of all even elements of the Array, Check if subarray with given product exists in an array, Longest Subarray with sum differences K, Longest alternating Subarray with absolute difference at least K, Minimize operations to make all elements equal by replacing left half of Subarray with right half, Jagged Array or Array of Arrays in C with Examples, Program to build a DFA to accept strings that start and end with same character.