📜  数组中的K倒数计数

📅  最后修改于: 2021-05-18 00:36:03             🧑  作者: Mango

给定长度为N且数字为K的数组arr [] ,任务是计算数组中K个倒数的数量。

例子:

方法:遍历给定的数组,每次遇到数字K时,都要检查数组中是否依次存在所有的数字K,K-1,K-2,…(最多1个)。如果是,则将计数加1。如果下一个数字使它不按顺序排列,则将查找下一个出现的K。

下面是上述方法的实现:

C++
// C++ code for the above program.
  
#include 
using namespace std;
  
// Function to to count the
// number of K-countdowns for
// multiple queries
int countKCountdown(int arr[],
                    int N,
                    int K)
{
  
    // flag which stores the
    // current value of value
    // in the countdown
    int flag = -1;
  
    // count of K-countdowns
    int count = 0;
  
    // Loop to iterate over the
    // elements of the array
    for (int i = 0; i < N; i++) {
  
        // condition check if
        // the elements
        // of the array is
        // equal to K
        if (arr[i] == K)
            flag = K;
  
        // condition check if
        // the elements
        // of the array is in
        // continuous order
        if (arr[i] == flag)
            flag--;
  
        // condition check if
        // the elements
        // of the array are not
        // in continuous order
        else
            flag = -1;
  
        // condition check to
        // increment the counter
        // if the there is a
        // K-countdown present
        // in the array
        if (flag == 0)
            count++;
    }
  
    // returning the count of
    // K-countdowns
    return count;
}
  
// Driver Code
int main()
{
    int N = 8;
    int K = 3;
    int arr[N] = { 4, 3, 2, 1,
                   5, 3, 2, 1 };
  
    // Function Call
    cout << countKCountdown(arr, N, K);
}


Java
// Java code for the above program.
class GFG{
      
// Function to to count the 
// number of K-countdowns for 
// multiple queries 
public static int countKCountdown(int arr[], 
                                  int N, int K) 
{ 
      
    // Flag which stores the 
    // current value of value 
    // in the countdown 
    int flag = -1; 
      
    // Count of K-countdowns 
    int count = 0; 
      
    // Loop to iterate over the 
    // elements of the array 
    for(int i = 0; i < N; i++) 
    {
         
       // Condition check if the
       // elements of the array is 
       // equal to K 
       if (arr[i] == K) 
           flag = K; 
             
       // Condition check if the
       // elements of the array is 
       // in continuous order 
       if (arr[i] == flag) 
           flag--; 
         
       // Condition check if the
       // elements of the array are 
       // not in continuous order 
       else
           flag = -1; 
         
       // Condition check to increment 
       // the counter if the there is a 
       // K-countdown present in the array 
       if (flag == 0) 
           count++; 
    } 
      
    // Returning the count of 
    // K-countdowns 
    return count; 
} 
  
// Driver code
public static void main(String[] args) 
{
    int N = 8; 
    int K = 3; 
    int arr[] = { 4, 3, 2, 1, 5, 3, 2, 1 }; 
      
    System.out.print(countKCountdown(arr, N, K));
}
}
  
// This code is contributed by divyeshrabadiya07


Python3
# Python3 code for the above program.
  
# Function to to count the
# number of K-countdowns for
# multiple queries
def countKCountdown(arr, N, K):
  
    # flag which stores the
    # current value of value
    # in the countdown
    flag = -1;
  
    # count of K-countdowns
    count = 0;
  
    # Loop to iterate over the
    # elements of the array
    for i in range(0, N):
  
        # condition check if
        # the elements
        # of the array is
        # equal to K
        if (arr[i] == K):
            flag = K;
  
        # condition check if
        # the elements
        # of the array is in
        # continuous order
        if (arr[i] == flag):
            flag -= 1;
  
        # condition check if
        # the elements
        # of the array are not
        # in continuous order
        else:
            flag = -1;
  
        # condition check to
        # increment the counter
        # if the there is a
        # K-countdown present
        # in the array
        if (flag == 0):
            count += 1;
      
    # returning the count of
    # K-countdowns
    return count;
  
# Driver Code
N = 8;
K = 3;
arr = [ 4, 3, 2, 1,
        5, 3, 2, 1 ];
  
# Function Call
print(countKCountdown(arr, N, K))
  
# This code is contributed by Akanksha_Rai


C#
// C# code for the above program.
using System;
class GFG{
      
// Function to to count the 
// number of K-countdowns for 
// multiple queries 
public static int countKCountdown(int []arr, 
                                  int N, int K) 
{ 
      
    // Flag which stores the 
    // current value of value 
    // in the countdown 
    int flag = -1; 
      
    // Count of K-countdowns 
    int count = 0; 
      
    // Loop to iterate over the 
    // elements of the array 
    for(int i = 0; i < N; i++) 
    {
          
    // Condition check if the
    // elements of the array is 
    // equal to K 
    if (arr[i] == K) 
        flag = K; 
              
    // Condition check if the
    // elements of the array is 
    // in continuous order 
    if (arr[i] == flag) 
        flag--; 
          
    // Condition check if the
    // elements of the array are 
    // not in continuous order 
    else
        flag = -1; 
          
    // Condition check to increment 
    // the counter if the there is a 
    // K-countdown present in the array 
    if (flag == 0) 
        count++; 
    } 
      
    // Returning the count of 
    // K-countdowns 
    return count; 
} 
  
// Driver code
public static void Main() 
{
    int N = 8; 
    int K = 3; 
    int []arr = { 4, 3, 2, 1, 5, 3, 2, 1 }; 
      
    Console.Write(countKCountdown(arr, N, K));
}
}
  
// This code is contributed by Akanksha_Rai


输出:
2


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