给定一个数组arr[] 大小为N和整数K ,任务是计算由降序排列的前K 个自然数组成的子数组的数量。
例子:
Input: arr[] = {1, 2, 3, 7, 9, 3, 2, 1, 8, 3, 2, 1}, K = 3
Output: 2
Explanation: The subarray {3, 2, 1} occurs twice in the array.
Input: arr = {100, 7, 6, 5, 4, 3, 2, 1, 100}, K = 6
Output: 1
方法:这个想法是遍历数组并检查是否存在从当前索引开始所需的递减序列。请按照以下步骤解决问题:
- 初始化两个变量, temp到K ,检查模式,并用0计数,以存储匹配的总子数组的计数。
- 使用变量i遍历数组arr[]并执行以下操作:
- 如果arr[i]等于temp并且temp 的值为1 ,则将计数增加1并将temp更新为K 。否则将temp减少1 。
- 否则,更新温度作为温度= K和如果ARR [i]是等于K,减1岛
- 完成上述步骤后,打印 作为结果的计数值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count subarray having
// the decreasing sequence K to 1
int CountSubarray(int arr[], int n,
int k)
{
int temp = k, count = 0;
// Traverse the array
for (int i = 0; i < n; i++) {
// Check if required sequence
// is present or not
if (arr[i] == temp) {
if (temp == 1) {
count++;
temp = k;
}
else
temp--;
}
// Reset temp to k
else {
temp = k;
if (arr[i] == k)
i--;
}
}
// Return the count
return count;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 7, 9, 3,
2, 1, 8, 3, 2, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 3;
// Function Call
cout << CountSubarray(arr, N, K);
return 0;
}
// This code is contributed by Dharanendra L V
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to count subarray having
// the decreasing sequence K to 1
static int CountSubarray(int arr[], int n,
int k)
{
int temp = k, count = 0;
// Traverse the array
for (int i = 0; i < n; i++) {
// Check if required sequence
// is present or not
if (arr[i] == temp) {
if (temp == 1) {
count++;
temp = k;
}
else
temp--;
}
// Reset temp to k
else {
temp = k;
if (arr[i] == k)
i--;
}
}
// Return the count
return count;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 7, 9, 3,
2, 1, 8, 3, 2, 1 };
int N = arr.length;
int K = 3;
// Function Call
System.out.println(CountSubarray(arr, N, K));
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 program for the above approach
# Function to count subarray having
# the decreasing sequence K to 1
def CountSubarray(arr, n, k):
temp = k
count = 0
# Traverse the array
for i in range(n):
# Check if required sequence
# is present or not
if (arr[i] == temp):
if (temp == 1):
count += 1
temp = k
else:
temp -= 1
# Reset temp to k
else:
temp = k
if (arr[i] == k):
i -= 1
# Return the count
return count
# Driver Code
if __name__ == "__main__":
arr = [ 1, 2, 3, 7, 9, 3,
2, 1, 8, 3, 2, 1 ]
N = len(arr)
K = 3
# Function Call
print(CountSubarray(arr, N, K))
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
class GFG{
// Function to count subarray having
// the decreasing sequence K to 1
static int CountSubarray(int[] arr,
int n, int k)
{
int temp = k, count = 0;
// Traverse the array
for(int i = 0; i < n; i++)
{
// Check if required sequence
// is present or not
if (arr[i] == temp)
{
if (temp == 1)
{
count++;
temp = k;
}
else
temp--;
}
// Reset temp to k
else
{
temp = k;
if (arr[i] == k)
i--;
}
}
// Return the count
return count;
}
// Driver code
static public void Main()
{
int[] arr = { 1, 2, 3, 7, 9, 3,
2, 1, 8, 3, 2, 1 };
int N = arr.Length;
int K = 3;
// Function Call
Console.Write(CountSubarray(arr, N, K));
}
}
// This code is contributed by Dharanendra L V
Javascript
输出:
2
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live