Given an array of n integers, find the no of subarrays whose minimal and maximum elements are same. Subarray is defined as non-empty sequence of consecutive elements.
Input: 2 3 1 1 Output: 5 Explanation: The subarrays are (2), (3), (1), (1) and (1, 1) Input: 2 4 5 3 3 3 Output: 9 Explanation: The subarrays are (2), (4), (5), (3), (3, 3), (3, 3, 3), (3), (3, 3) and (3)
The first thing to observe is that only those subarrays whose all elements are same will have the same minimum and maximum. Having different elements clearly means different minimum and maximum. Hence we just need to calculate the number of continuous same elements(say d), then by combinations formula we get the no of subarrays to be –
No of subarrays possible with d elements = ( d * (d+1) / 2 )
where d is number of continuous same elements.
We traverse from 1-n and then from I+1 to n and then find the number of continuous same elements and then add to the result the no subarrays possible.
Time complexity : O(n^2)
Auxiliary Space : O(1)
This article is contributed by Raja Vikramaditya (Raj). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum and Maximum of all subarrays of size K using Map
- Sum of minimum and maximum elements of all subarrays of size k.
- Sum of all differences between Maximum and Minimum of increasing Subarrays
- Min difference between maximum and minimum element in all Y size subarrays
- Split array into two subarrays such that difference of their maximum is minimum
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Number of subarrays with maximum values in given range
- Find number of subarrays ending with arr[i] where arr[i] is the minimum element of that subarray
- Maximum and minimum of an array using minimum number of comparisons
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time
- Sliding Window Maximum (Maximum of all subarrays of size k)
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Find a number such that maximum in array is minimum possible after XOR
- Minimum and Maximum number of pairs in m teams of n people
- Break a number such that sum of maximum divisors of all parts is minimum
- Minimum and Maximum element of an array which is divisible by a given number k
- Minimum number of elements to be removed to make XOR maximum
- Minimum and maximum number of N chocolates after distribution among K students
- Sum of Maximum and Minimum prime factor of every number in the Array