📜  具有最大元素的子数组的计数至少是剩余元素中最大元素的两倍

📅  最后修改于: 2022-05-13 01:56:05.408000             🧑  作者: Mango

具有最大元素的子数组的计数至少是剩余元素中最大元素的两倍

给定一个由N个正整数组成的数组arr[] ,任务是找到子数组的计数,使得子数组的最大元素大于数组中所有其他元素的最大值的两倍。

例子:

朴素方法:解决给定问题的最简单方法是生成给定数组arr[]的所有可能子数组,然后计算最大元素大于所有其他元素最大值两倍的子数组的数量。检查所有子数组后,打印获得的子数组的计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find count of subarrays
// which have max element greater than
// twice maximum of all other elements
void countSubarray(int arr[], int n)
{
    // Stores the count of subarrays
    int count = 0;
 
    // Generate all possible subarrays
    for (int i = 0; i < n; i++) {
 
        for (int j = i; j < n; j++) {
 
            // Stores the maximum element
            // of the subarray
            int mxSubarray = 0;
 
            // Stores the maximum of all
            // other elements
            int mxOther = 0;
 
            // Find the maximum element
            // in the subarray [i, j]
            for (int k = i; k <= j; k++) {
                mxSubarray = max(mxSubarray,
                                 arr[k]);
            }
 
            // Find the maximum of all
            // other elements
            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 the maximum of subarray
            // is greater than twice the
            // maximum of other elements
            if (mxSubarray > (2 * mxOther))
                count++;
        }
    }
 
    // Print the maximum value obtained
    cout << count;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 6, 10, 9, 7, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
    countSubarray(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to find count of subarrays
// which have max element greater than
// twice maximum of all other elements
public static void countSubarray(int arr[], int n)
{
     
    // Stores the count of subarrays
    int count = 0;
 
    // Generate all possible subarrays
    for(int i = 0; i < n; i++)
    {
        for(int j = i; j < n; j++)
        {
             
            // Stores the maximum element
            // of the subarray
            int mxSubarray = 0;
 
            // Stores the maximum of all
            // other elements
            int mxOther = 0;
 
            // Find the maximum element
            // in the subarray [i, j]
            for(int k = i; k <= j; k++)
            {
                mxSubarray = Math.max(
                    mxSubarray, arr[k]);
            }
 
            // Find the maximum of all
            // other elements
            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 the maximum of subarray
            // is greater than twice the
            // maximum of other elements
            if (mxSubarray > (2 * mxOther))
                count++;
        }
    }
 
    // Print the maximum value obtained
    System.out.println(count);
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 6, 10, 9, 7, 3 };
    int N = arr.length;
     
    countSubarray(arr, N);
}
}
 
// This code is contributed by Potta Lokesh


Python3
# Python 3 program for the above approach
 
# Function to find count of subarrays
# which have max element greater than
# twice maximum of all other elements
def countSubarray(arr, n):
    # Stores the count of subarrays
    count = 0
 
    # Generate all possible subarrays
    for i in range(n):
        for j in range(i, n, 1):
            # Stores the maximum element
            # of the subarray
            mxSubarray = 0
 
            # Stores the maximum of all
            # other elements
            mxOther = 0
 
            # Find the maximum element
            # in the subarray [i, j]
            for k in range(i, j + 1, 1):
                mxSubarray = max(mxSubarray, arr[k])
 
            # Find the maximum of all
            # other elements
            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 the maximum of subarray
            # is greater than twice the
            # maximum of other elements
            if (mxSubarray > (2 * mxOther)):
                count += 1
 
    # Print the maximum value obtained
    print(count)
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 6, 10, 9, 7, 3]
    N = len(arr)
    countSubarray(arr, N)
     
    # This code is contributed by bgangwar59.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find count of subarrays
// which have max element greater than
// twice maximum of all other elements
static void countSubarray(int []arr, int n)
{
     
    // Stores the count of subarrays
    int count = 0;
 
    // Generate all possible subarrays
    for(int i = 0; i < n; i++)
    {
        for(int j = i; j < n; j++)
        {
             
            // Stores the maximum element
            // of the subarray
            int mxSubarray = 0;
 
            // Stores the maximum of all
            // other elements
            int mxOther = 0;
 
            // Find the maximum element
            // in the subarray [i, j]
            for(int k = i; k <= j; k++)
            {
                mxSubarray = Math.Max(mxSubarray,
                                      arr[k]);
            }
 
            // Find the maximum of all
            // other elements
            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 the maximum of subarray
            // is greater than twice the
            // maximum of other elements
            if (mxSubarray > (2 * mxOther))
                count++;
        }
    }
 
    // Print the maximum value obtained
    Console.Write(count);
}
 
// Driver Code
public static void Main()
{
    int []arr = { 1, 6, 10, 9, 7, 3 };
    int N = arr.Length;
     
    countSubarray(arr, N);
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find count of subarrays
// which have max element greater than
// twice maximum of all other elements
void countSubarray(int arr[], int n)
{
    int count = 0, L = 0, R = 0;
 
    // Stores the maximum element of
    // the array
    int mx = *max_element(arr, arr + n);
 
    // Traverse the given array
    for (int i = 0; i < n; i++) {
 
        // If the value of 2*arr[i] is
        // greater than mx
        if (arr[i] * 2 > mx) {
 
            // Update the value of L
            // and break out of loop
            L = i;
            break;
        }
    }
 
    for (int i = n - 1; i >= 0; i--) {
 
        // If the value 2*arr[i] is
        // greater than mx
        if (arr[i] * 2 > mx) {
 
            // Update the value of R
            // and break out of loop
            R = i;
            break;
        }
    }
 
    // Print the final answer
    cout << (L + 1) * (n - R);
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 6, 10, 9, 7, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
    countSubarray(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
    // Function to find count of subarrays
    // which have max element greater than
    // twice maximum of all other elements
    static void countSubarray(int[] arr, int n)
    {
        int L = 0, R = 0;
 
        // Stores the maximum element of
        // the array
 
        int mx = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++)
            mx = Math.max(mx, arr[i]);
 
        // Traverse the given array
        for (int i = 0; i < n; i++) {
 
            // If the value of 2*arr[i] is
            // greater than mx
            if (arr[i] * 2 > mx) {
 
                // Update the value of L
                // and break out of loop
                L = i;
                break;
            }
        }
 
        for (int i = n - 1; i >= 0; i--) {
 
            // If the value 2*arr[i] is
            // greater than mx
            if (arr[i] * 2 > mx) {
 
                // Update the value of R
                // and break out of loop
                R = i;
                break;
            }
        }
 
        // Print the final answer
        System.out.println((L + 1) * (n - R));
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 1, 6, 10, 9, 7, 3 };
        int N = arr.length;
        countSubarray(arr, N);
    }
}
 
// This code is contributed by rishavmahato348.


Python3
# Python3 program for the above approach
 
# Function to find count of subarrays
# which have max element greater than
# twice maximum of all other elements
def countSubarray(arr, n):
     
    count = 0
    L = 0
    R = 0
 
    # Stores the maximum element of
    # the array
    mx = max(arr)
 
    # Traverse the given array
    for i in range(n):
         
        # If the value of 2*arr[i] is
        # greater than mx
        if (arr[i] * 2 > mx):
 
            # Update the value of L
            # and break out of loop
            L = i
            break
     
    i = n - 1
     
    while (i >= 0):
         
        # If the value 2*arr[i] is
        # greater than mx
        if (arr[i] * 2 > mx):
 
            # Update the value of R
            # and break out of loop
            R = i
            break
         
        i -= 1
 
    # Print the final answer
    print((L + 1) * (n - R))
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 6, 10, 9, 7, 3 ]
    N = len(arr)
     
    countSubarray(arr, N)
         
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
class GFG {
 
    // Function to find count of subarrays
    // which have max element greater than
    // twice maximum of all other elements
    static void countSubarray(int[] arr, int n)
    {
        int L = 0, R = 0;
 
        // Stores the maximum element of
        // the array
 
        int mx = Int32.MinValue;
        for (int i = 0; i < n; i++)
            mx = Math.Max(mx, arr[i]);
 
        // Traverse the given array
        for (int i = 0; i < n; i++) {
 
            // If the value of 2*arr[i] is
            // greater than mx
            if (arr[i] * 2 > mx) {
 
                // Update the value of L
                // and break out of loop
                L = i;
                break;
            }
        }
 
        for (int i = n - 1; i >= 0; i--) {
 
            // If the value 2*arr[i] is
            // greater than mx
            if (arr[i] * 2 > mx) {
 
                // Update the value of R
                // and break out of loop
                R = i;
                break;
            }
        }
 
        // Print the final answer
        Console.WriteLine((L + 1) * (n - R));
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 1, 6, 10, 9, 7, 3 };
        int N = arr.Length;
        countSubarray(arr, N);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
4

时间复杂度: O(N 3 )
辅助空间: O(1)

有效方法:上述方法也可以通过观察数组的最大元素将始终是子数组的一部分进行优化,并且所有值大于最大元素一半的元素也包含在子数组中。请按照以下步骤解决问题:

  • 初始化一个变量,比如存储数组最大元素的mx
  • 初始化两个变量LR来存储子数组的左右端点。
  • 使用变量i[0, N – 1]范围内进行迭代,如果2*arr[i]的值大于mx ,则将L初始化为i并退出循环。
  • 使用变量i以相反的方式迭代范围[N – 1, 0] ,如果2*arr[i]的值大于mx ,则将R初始化为i并退出循环。
  • 完成上述步骤后,打印(L + 1)*(N – R)的值作为结果。

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find count of subarrays
// which have max element greater than
// twice maximum of all other elements
void countSubarray(int arr[], int n)
{
    int count = 0, L = 0, R = 0;
 
    // Stores the maximum element of
    // the array
    int mx = *max_element(arr, arr + n);
 
    // Traverse the given array
    for (int i = 0; i < n; i++) {
 
        // If the value of 2*arr[i] is
        // greater than mx
        if (arr[i] * 2 > mx) {
 
            // Update the value of L
            // and break out of loop
            L = i;
            break;
        }
    }
 
    for (int i = n - 1; i >= 0; i--) {
 
        // If the value 2*arr[i] is
        // greater than mx
        if (arr[i] * 2 > mx) {
 
            // Update the value of R
            // and break out of loop
            R = i;
            break;
        }
    }
 
    // Print the final answer
    cout << (L + 1) * (n - R);
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 6, 10, 9, 7, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
    countSubarray(arr, N);
 
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
 
class GFG {
 
    // Function to find count of subarrays
    // which have max element greater than
    // twice maximum of all other elements
    static void countSubarray(int[] arr, int n)
    {
        int L = 0, R = 0;
 
        // Stores the maximum element of
        // the array
 
        int mx = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++)
            mx = Math.max(mx, arr[i]);
 
        // Traverse the given array
        for (int i = 0; i < n; i++) {
 
            // If the value of 2*arr[i] is
            // greater than mx
            if (arr[i] * 2 > mx) {
 
                // Update the value of L
                // and break out of loop
                L = i;
                break;
            }
        }
 
        for (int i = n - 1; i >= 0; i--) {
 
            // If the value 2*arr[i] is
            // greater than mx
            if (arr[i] * 2 > mx) {
 
                // Update the value of R
                // and break out of loop
                R = i;
                break;
            }
        }
 
        // Print the final answer
        System.out.println((L + 1) * (n - R));
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 1, 6, 10, 9, 7, 3 };
        int N = arr.length;
        countSubarray(arr, N);
    }
}
 
// This code is contributed by rishavmahato348.

Python3

# Python3 program for the above approach
 
# Function to find count of subarrays
# which have max element greater than
# twice maximum of all other elements
def countSubarray(arr, n):
     
    count = 0
    L = 0
    R = 0
 
    # Stores the maximum element of
    # the array
    mx = max(arr)
 
    # Traverse the given array
    for i in range(n):
         
        # If the value of 2*arr[i] is
        # greater than mx
        if (arr[i] * 2 > mx):
 
            # Update the value of L
            # and break out of loop
            L = i
            break
     
    i = n - 1
     
    while (i >= 0):
         
        # If the value 2*arr[i] is
        # greater than mx
        if (arr[i] * 2 > mx):
 
            # Update the value of R
            # and break out of loop
            R = i
            break
         
        i -= 1
 
    # Print the final answer
    print((L + 1) * (n - R))
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 6, 10, 9, 7, 3 ]
    N = len(arr)
     
    countSubarray(arr, N)
         
# This code is contributed by SURENDRA_GANGWAR

C#

// C# program for the above approach
using System;
class GFG {
 
    // Function to find count of subarrays
    // which have max element greater than
    // twice maximum of all other elements
    static void countSubarray(int[] arr, int n)
    {
        int L = 0, R = 0;
 
        // Stores the maximum element of
        // the array
 
        int mx = Int32.MinValue;
        for (int i = 0; i < n; i++)
            mx = Math.Max(mx, arr[i]);
 
        // Traverse the given array
        for (int i = 0; i < n; i++) {
 
            // If the value of 2*arr[i] is
            // greater than mx
            if (arr[i] * 2 > mx) {
 
                // Update the value of L
                // and break out of loop
                L = i;
                break;
            }
        }
 
        for (int i = n - 1; i >= 0; i--) {
 
            // If the value 2*arr[i] is
            // greater than mx
            if (arr[i] * 2 > mx) {
 
                // Update the value of R
                // and break out of loop
                R = i;
                break;
            }
        }
 
        // Print the final answer
        Console.WriteLine((L + 1) * (n - R));
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 1, 6, 10, 9, 7, 3 };
        int N = arr.Length;
        countSubarray(arr, N);
    }
}
 
// This code is contributed by ukasp.

Javascript


输出:
4

时间复杂度: O(N)
辅助空间: O(1)