Number of subarrays whose minimum and maximum are same
Last Updated :
18 Sep, 2023
Given an array of n integers, find the no of subarrays whose minimal and maximum elements are the same. A subarray is defined as a non-empty sequence of consecutive elements.
Examples:
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 a 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.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int calculate( int a[], int n)
{
int ans = 0;
for ( int i = 0; i < n; i++) {
int r = i + 1;
for ( int j = r; j < n; j++) {
if (a[i] == a[j])
r += 1;
else
break ;
}
int d = r - i;
ans += (d * (d + 1) / 2);
i = r - 1;
}
return ans;
}
int main()
{
int a[] = { 2, 4, 5, 3, 3, 3 };
int n = sizeof (a) / sizeof (a[0]);
cout << calculate(a, n);
return 0;
}
|
Java
class Subarray
{
static int calculate( int a[], int n)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i++) {
int r = i + 1 ;
for ( int j = r; j < n; j++) {
if (a[i] == a[j])
r += 1 ;
else
break ;
}
int d = r - i;
ans += (d * (d + 1 ) / 2 );
i = r - 1 ;
}
return ans;
}
public static void main(String[] args)
{
int a[] = { 2 , 4 , 5 , 3 , 3 , 3 };
System.out.println(calculate(a, a.length));
}
}
|
Python3
def calculate(a, n):
ans = 0 ;
i = 0 ;
while (i < n):
r = i + 1 ;
for j in range (r, n):
if (a[i] = = a[j]):
r = r + 1 ;
else :
break ;
d = r - i;
ans = ans + (d * (d + 1 ) / 2 );
i = r - 1 ;
i = i + 1 ;
return int (ans);
a = [ 2 , 4 , 5 , 3 , 3 , 3 ];
n = len (a);
print (calculate(a, n));
|
C#
using System;
class Subarray {
static int calculate( int [] a, int n)
{
int ans = 0;
for ( int i = 0; i < n; i++) {
int r = i + 1;
for ( int j = r; j < n; j++) {
if (a[i] == a[j])
r += 1;
else
break ;
}
int d = r - i;
ans += (d * (d + 1) / 2);
i = r - 1;
}
return ans;
}
public static void Main()
{
int [] a = { 2, 4, 5, 3, 3, 3 };
Console.WriteLine(calculate(a, a.Length));
}
}
|
PHP
<?php
function calculate( $a , $n )
{
$ans = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$r = $i + 1;
for ( $j = $r ; $j < $n ; $j ++)
{
if ( $a [ $i ] == $a [ $j ])
$r += 1;
else
break ;
}
$d = $r - $i ;
$ans += ( $d * ( $d + 1) / 2);
$i = $r - 1;
}
return $ans ;
}
$a = array ( 2, 4, 5, 3, 3, 3 );
$n = count ( $a );
echo calculate( $a , $n );
?>
|
Javascript
<script>
function calculate(a, n)
{
let ans = 0;
for (let i = 0; i < n; i++) {
let r = i + 1;
for (let j = r; j < n; j++) {
if (a[i] == a[j])
r += 1;
else
break ;
}
let d = r - i;
ans += (d * (d + 1) / 2);
i = r - 1;
}
return ans;
}
let a = [ 2, 4, 5, 3, 3, 3 ];
document.write(calculate(a, a.length));
</script>
|
Time Complexity: O(n), where n is the size of the given array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...