给定一个由N 个整数组成的数组arr[]和一个整数K 。任务是找到最大值等于K的子数组的数量。
例子:
Input: arr[ ] = {2, 1, 3, 4}, K = 3
Output: 3
Explanation:
Sub-arrays with maximum value is equals K are { 2, 1, 3 }, { 1, 3 }, { 3 }, hence the answer is 3.
Input: arr[ ] = {1, 2, 3}, K = 1.
Output: 1
方法:数组arr[]中的子数组个数等于最大不大于K的子数组个数减去最大不大于K-1的子数组个数。参考这里计算最大大于K的子数组的个数。按照以下步骤解决问题:
- 将变量count1初始化为0以计算最大不大于K-1的子数组的数量。
- 将变量count2初始化为0以计算最大不大于K的子数组的数量。
- 定义一个函数,例如totalSubarrays(arr, N, K)来计算最大不大于K的子数组的数量,并执行以下步骤:
- 将变量 say, ans初始化为 0以存储最大不大于K的子数组的计数。
- 在[0, N-1]范围内迭代并执行以下步骤。
- 如果arr[i]的值大于K,则将i的值增加1并继续。
- 将变量count初始化为0以存储可能的子数组总数。
- 在范围内迭代直到i小于N且arr[i]小于等于K。
- 将i和count的值增加1。
- 将(count*(count+1))/2的值与ans的值相加。
- 返回ans的值。
- 调用函数totalSubarrays(arr, N, K-1)计算最大不大于K-1的子数组个数,并存入count1。
- 调用函数totalSubarrays(arr, N, K)计算最大不大于K的子数组个数,并存入count2。
- 将(count2 – count1)的值存储在ans的最终值中并打印出来。
下面是上述方法的实现。
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to count the subarrays with maximum
// not greater than K
int totalSubarrays(int arr[], int n, int k)
{
int ans = 0, i = 0;
while (i < n) {
// If arr[i]>k then arr[i] cannot be
// a part of any subarray.
if (arr[i] > k) {
i++;
continue;
}
int count = 0;
// Count the number of elements where
// arr[i] is not greater than k.
while (i < n && arr[i] <= k) {
i++;
count++;
}
// Summation of all possible subarrays
// in the variable ans.
ans += ((count * (count + 1)) / 2);
}
return ans;
}
// Function to count the subarrays with
// maximum value is equal to K
int countSubarrays(int arr[], int n, int k)
{
// Stores count of subarrays with max <= k - 1.
int count1 = totalSubarrays(arr, n, k - 1);
// Stores count of subarrays with max >= k + 1.
int count2 = totalSubarrays(arr, n, k);
// Stores count of subrrays with max = k.
int ans = count2 - count1;
return ans;
}
// Driver Code
int main()
{
// Given Input
int n = 4, k = 3;
int arr[] = { 2, 1, 3, 4 };
// Function Call
cout << countSubarrays(arr, n, k);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to count the subarrays with maximum
// not greater than K
static int totalSubarrays(int arr[], int n, int k)
{
int ans = 0, i = 0;
while (i < n) {
// If arr[i]>k then arr[i] cannot be
// a part of any subarray.
if (arr[i] > k) {
i++;
continue;
}
int count = 0;
// Count the number of elements where
// arr[i] is not greater than k.
while (i < n && arr[i] <= k) {
i++;
count++;
}
// Summation of all possible subarrays
// in the variable ans.
ans += ((count * (count + 1)) / 2);
}
return ans;
}
// Function to count the subarrays with
// maximum value is equal to K
static int countSubarrays(int arr[], int n, int k)
{
// Stores count of subarrays with max <= k - 1.
int count1 = totalSubarrays(arr, n, k - 1);
// Stores count of subarrays with max >= k + 1.
int count2 = totalSubarrays(arr, n, k);
// Stores count of subrrays with max = k.
int ans = count2 - count1;
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int n = 4, k = 3;
int arr[] = { 2, 1, 3, 4 };
// Function Call
System.out.println(countSubarrays(arr, n, k));
// This code is contributed by Potta Lokesh
}
}
Python3
# Python3 implementation of the above approach
# Function to count the subarrays with maximum
# not greater than K
def totalSubarrays(arr, n, k):
ans = 0
i = 0
while (i < n):
# If arr[i]>k then arr[i] cannot be
# a part of any subarray.
if (arr[i] > k):
i += 1
continue
count = 0
# Count the number of elements where
# arr[i] is not greater than k.
while (i < n and arr[i] <= k):
i += 1
count += 1
# Summation of all possible subarrays
# in the variable ans.
ans += ((count * (count + 1)) // 2)
return ans
# Function to count the subarrays with
# maximum value is equal to K
def countSubarrays(arr, n, k):
# Stores count of subarrays with max <= k - 1.
count1 = totalSubarrays(arr, n, k - 1)
# Stores count of subarrays with max >= k + 1.
count2 = totalSubarrays(arr, n, k)
# Stores count of subrrays with max = k.
ans = count2 - count1
return ans
# Driver Code
if __name__ == '__main__':
# Given Input
n = 4
k = 3
arr = [ 2, 1, 3, 4 ]
# Function Call
print(countSubarrays(arr, n, k))
# This code is contributed by ipg2016107
C#
// C# program for the above approach
using System;
class GFG
{
// Function to count the subarrays with maximum
// not greater than K
static int totalSubarrays(int[] arr, int n, int k)
{
int ans = 0, i = 0;
while (i < n)
{
// If arr[i]>k then arr[i] cannot be
// a part of any subarray.
if (arr[i] > k) {
i++;
continue;
}
int count = 0;
// Count the number of elements where
// arr[i] is not greater than k.
while (i < n && arr[i] <= k) {
i++;
count++;
}
// Summation of all possible subarrays
// in the variable ans.
ans += ((count * (count + 1)) / 2);
}
return ans;
}
// Function to count the subarrays with
// maximum value is equal to K
static int countSubarrays(int[] arr, int n, int k)
{
// Stores count of subarrays with max <= k - 1.
int count1 = totalSubarrays(arr, n, k - 1);
// Stores count of subarrays with max >= k + 1.
int count2 = totalSubarrays(arr, n, k);
// Stores count of subrrays with max = k.
int ans = count2 - count1;
return ans;
}
static void Main()
{
// Given Input
int n = 4, k = 3;
int[] arr = { 2, 1, 3, 4 };
// Function Call
Console.WriteLine(countSubarrays(arr, n, k));
}
}
// This code is contributed by abhinavjain194
Javascript
输出
3
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。