Given an array arr[] consisting of N positive integers, the task is to find the count of subarrays such that the maximum element of the subarray is greater than twice the maximum of all other elements of the array.
Examples:
Input: arr[] = {1, 6, 10, 9, 7, 3}
Output: 4
Explanation:
Below are the subarrays satisfying the given condition:
- Consider the subarray {6, 10, 9, 7}. Now the maximum element of this subarray is 10 which is greater than twice the maximum elements of the remaining array elements i.e., 2*max{1, 3} = 2*3 = 6.
- Consider the subarray {6, 10, 9, 7, 3}. Now the maximum element of this subarray is 10 which is greater than twice the maximum elements of the remaining array elements i.e., 2*max{1} = 2*1 = 2.
- Consider the subarray {1, 6, 10, 9, 7}. Now the maximum element of this subarray is 10 which is greater than twice the maximum elements of the remaining array elements i.e., 2*max{3} = 2*3 = 6.
- Consider the subarray {1, 6, 10, 9, 7, 3}. Now the maximum element of this subarray is 10 which is greater than twice the maximum elements of the remaining array elements i.e., 2*max{} = 2*0 = 0.
Therefore, the total number of subarrays is 4.
Input: arr[] = {1, 10, 2, 3}
Output: 6
Naive Approach: The simplest approach to solve the given problem is to generate all possible subarrays of the given array arr[] and then count the number of subarrays having a maximum element greater than twice the maximum of all other elements. After checking for all the subarrays, print the count of the subarray obtained.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void countSubarray( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i; j < n; j++) {
int mxSubarray = 0;
int mxOther = 0;
for ( int k = i; k <= j; k++) {
mxSubarray = max(mxSubarray,
arr[k]);
}
for ( int k = 0; k < i; k++) {
mxOther = max(
mxOther, arr[k]);
}
for ( int k = j + 1; k < n; k++) {
mxOther = max(
mxOther, arr[k]);
}
if (mxSubarray > (2 * mxOther))
count++;
}
}
cout << count;
}
int main()
{
int arr[] = { 1, 6, 10, 9, 7, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
countSubarray(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG{
public static void countSubarray( int arr[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = i; j < n; j++)
{
int mxSubarray = 0 ;
int mxOther = 0 ;
for ( int k = i; k <= j; k++)
{
mxSubarray = Math.max(
mxSubarray, arr[k]);
}
for ( int k = 0 ; k < i; k++)
{
mxOther = Math.max(mxOther, arr[k]);
}
for ( int k = j + 1 ; k < n; k++)
{
mxOther = Math.max(mxOther, arr[k]);
}
if (mxSubarray > ( 2 * mxOther))
count++;
}
}
System.out.println(count);
}
public static void main(String[] args)
{
int arr[] = { 1 , 6 , 10 , 9 , 7 , 3 };
int N = arr.length;
countSubarray(arr, N);
}
}
|
Python3
def countSubarray(arr, n):
count = 0
for i in range (n):
for j in range (i, n, 1 ):
mxSubarray = 0
mxOther = 0
for k in range (i, j + 1 , 1 ):
mxSubarray = max (mxSubarray, arr[k])
for k in range ( 0 , i, 1 ):
mxOther = max (mxOther, arr[k])
for k in range (j + 1 ,n, 1 ):
mxOther = max (mxOther, arr[k])
if (mxSubarray > ( 2 * mxOther)):
count + = 1
print (count)
if __name__ = = '__main__' :
arr = [ 1 , 6 , 10 , 9 , 7 , 3 ]
N = len (arr)
countSubarray(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void countSubarray( int []arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = i; j < n; j++)
{
int mxSubarray = 0;
int mxOther = 0;
for ( int k = i; k <= j; k++)
{
mxSubarray = Math.Max(mxSubarray,
arr[k]);
}
for ( int k = 0; k < i; k++)
{
mxOther = Math.Max(mxOther, arr[k]);
}
for ( int k = j + 1; k < n; k++)
{
mxOther = Math.Max(mxOther, arr[k]);
}
if (mxSubarray > (2 * mxOther))
count++;
}
}
Console.Write(count);
}
public static void Main()
{
int []arr = { 1, 6, 10, 9, 7, 3 };
int N = arr.Length;
countSubarray(arr, N);
}
}
|
Javascript
<script>
function countSubarray(arr, n)
{
let count = 0;
for (let i = 0; i < n; i++)
{
for (let j = i; j < n; j++)
{
let mxSubarray = 0;
let mxOther = 0;
for (let k = i; k <= j; k++)
{
mxSubarray = Math.max(mxSubarray,
arr[k]);
}
for (let k = 0; k < i; k++)
{
mxOther = Math.max(mxOther, arr[k]);
}
for (let k = j + 1; k < n; k++)
{
mxOther = Math.max(
mxOther, arr[k]);
}
if (mxSubarray > (2 * mxOther))
count++;
}
}
document.write(count);
}
let arr = [ 1, 6, 10, 9, 7, 3 ];
let N = arr.length;
countSubarray(arr, N);
</script>
|
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized using the observation that the maximum element of the array will always be a part of the subarray and all elements having a value greater than half of the maximum element is also included in the subarray. Follow the steps below to solve the problem:
- Initialize a variable, say mx that stores the maximum element of the array.
- Initialize two variables L and R to store the left and the right endpoints of the subarray.
- Iterate over the range [0, N – 1] using the variable i and if the value of 2*arr[i] is greater than mx then initialize L to i and break from the loop.
- Iterate over the range [N – 1, 0] in a reverse manner using the variable i and if the value of 2*arr[i] is greater than mx then initialize R to i and break from the loop.
- After completing the above steps, print the value of (L + 1)*(N – R) as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countSubarray( int arr[], int n)
{
int count = 0, L = 0, R = 0;
int mx = *max_element(arr, arr + n);
for ( int i = 0; i < n; i++) {
if (arr[i] * 2 > mx) {
L = i;
break ;
}
}
for ( int i = n - 1; i >= 0; i--) {
if (arr[i] * 2 > mx) {
R = i;
break ;
}
}
cout << (L + 1) * (n - R);
}
int main()
{
int arr[] = { 1, 6, 10, 9, 7, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
countSubarray(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void countSubarray( int [] arr, int n)
{
int L = 0 , R = 0 ;
int mx = Integer.MIN_VALUE;
for ( int i = 0 ; i < n; i++)
mx = Math.max(mx, arr[i]);
for ( int i = 0 ; i < n; i++) {
if (arr[i] * 2 > mx) {
L = i;
break ;
}
}
for ( int i = n - 1 ; i >= 0 ; i--) {
if (arr[i] * 2 > mx) {
R = i;
break ;
}
}
System.out.println((L + 1 ) * (n - R));
}
public static void main(String[] args)
{
int [] arr = { 1 , 6 , 10 , 9 , 7 , 3 };
int N = arr.length;
countSubarray(arr, N);
}
}
|
Python3
def countSubarray(arr, n):
count = 0
L = 0
R = 0
mx = max (arr)
for i in range (n):
if (arr[i] * 2 > mx):
L = i
break
i = n - 1
while (i > = 0 ):
if (arr[i] * 2 > mx):
R = i
break
i - = 1
print ((L + 1 ) * (n - R))
if __name__ = = '__main__' :
arr = [ 1 , 6 , 10 , 9 , 7 , 3 ]
N = len (arr)
countSubarray(arr, N)
|
C#
using System;
class GFG {
static void countSubarray( int [] arr, int n)
{
int L = 0, R = 0;
int mx = Int32.MinValue;
for ( int i = 0; i < n; i++)
mx = Math.Max(mx, arr[i]);
for ( int i = 0; i < n; i++) {
if (arr[i] * 2 > mx) {
L = i;
break ;
}
}
for ( int i = n - 1; i >= 0; i--) {
if (arr[i] * 2 > mx) {
R = i;
break ;
}
}
Console.WriteLine((L + 1) * (n - R));
}
public static void Main()
{
int [] arr = { 1, 6, 10, 9, 7, 3 };
int N = arr.Length;
countSubarray(arr, N);
}
}
|
Javascript
<script>
function countSubarray(arr, n)
{
var count = 0, L = 0, R = 0;
var mx = Math.max.apply( null , arr);
var i;
for (i = 0; i < n; i++) {
if (arr[i] * 2 > mx) {
L = i;
break ;
}
}
for (i = n - 1; i >= 0; i--) {
if (arr[i] * 2 > mx) {
R = i;
break ;
}
}
document.write((L + 1) * (n - R));
}
var arr = [1, 6, 10, 9, 7, 3]
var N = arr.length;
countSubarray(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)