📌  相关文章
📜  按降序计算由前K个自然数组成的子数组

📅  最后修改于: 2021-04-24 03:59:39             🧑  作者: Mango

给定一个数组arr [] 大小为N和整数K ,任务是计算子数组的数量,该子数组由降序排列的前K个自然数组成。

例子:

方法:想法是遍历数组,并检查是否从当前索引开始是否存在所需的递减序列。请按照以下步骤解决问题:

  • 初始化两个变量tempK ,以检查模式,并使用0计数以存储匹配的子数组总数。
  • 使用变量i遍历数组arr []并执行以下操作:
    • 如果arr [i]等于temp并且temp的值为1 ,则将计数增加1并将temp更新为K。否则将温度递减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)