📌  相关文章
📜  计算等级不超过K的数组元素

📅  最后修改于: 2021-05-17 16:59:37             🧑  作者: Mango

给定一个由N个整数和一个整数K组成的数组arr [] ,任务是找到秩最多为K的数组元素的数量。

例子:

天真的方法:最简单的方法是在数组中找到当前的最大元素,并将其与先前的最大元素进行比较。如果它们相等,则前一个元素和当前元素的等级必须相等。否则,将当前最大元素的等级分配为比先前获得的最大数量大一的等级。重复此过程,直到给定数组为空或秩大于K ,以较早者为准。遍历后,打印已删除元素的总数。

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

高效方法:想法是使用排序算法。在以给定数组以非递增顺序排序之后,并且对于每个元素,如果当前元素和前一个元素不相等,则当前元素的等级必须比前一个元素大1。否则,它与上一个相同。请按照以下步骤解决问题:

  • 以给定的arr []升序排序。
  • 1初始化变量rankposition ,以分别存储当前数组元素的rank和位置。
  • 将排序后的数组从i =(N – 1)遍历到0,然后执行以下操作:
    • 当相邻数组元素不相等或i等于(N – 1)时,使用位置更新等级
    • 返回位置–如果等级大于K则返回1
    • 每次遍历的增量位置
  • 如果所有数组元素的秩均不超过K ,则打印N。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find count of array
// elements with rank less than or
// equal to k
int rankLessThanK(int* arr, int k, int n)
{
     
    // Initialize rank and position
    int rank = 1;
    int position = 1;
 
    // Sort the given array
    sort(arr, arr + n);
 
    // Traverse array from right to left
    for(int i = n - 1; i >= 0; i--)
    {
         
        // Update rank with position, if
        // adjacent elements are unequal
        if (i == n - 1 || arr[i] != arr[i + 1])
        {
            rank = position;
 
            // Return position - 1, if
            // rank greater than k
            if (rank > k)
                return position - 1;
        }
 
        // Increase position
        position++;
    }
    return n;
}
 
// Driver Code
int main()
{
     
    // Given array
    int arr[5] = { 2, 2, 3, 4, 5 };
 
    int N = 5;
 
    // Given K
    int K = 4;
 
    // Function Call
    cout << rankLessThanK(arr, K, N);
     
    return 0;
}
 
// This code is contributed by hemanth gadarla


Java
// Java program for the above approach
 
import java.util.*;
import java.lang.*;
class GFG {
 
    // Function to find count of array
    // elements with rank less than or
    // equal to k
    static int rankLessThanK(int[] arr,
                             int k, int n)
    {
        // Initialize rank and position
        int rank = 1;
        int position = 1;
 
        // Sort the given array
        Arrays.sort(arr);
 
        // Traverse array from right to left
        for (int i = n - 1; i >= 0; i--) {
 
            // Update rank with position, if
            // adjacent elements are unequal
            if (i == n - 1
                || arr[i] != arr[i + 1]) {
 
                rank = position;
 
                // Return position - 1, if
                // rank greater than k
                if (rank > k)
                    return position - 1;
            }
 
            // Increase position
            position++;
        }
 
        return n;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given array
        int arr[] = { 2, 2, 3, 4, 5 };
 
        int N = arr.length;
 
        // Given K
        int K = 4;
 
        // Function Call
        System.out.println(
            rankLessThanK(arr, K, N));
    }
}


Python3
# Python3 program for the
# above approach
 
# Function to find count of
# array elements with rank
# less than or equal to k
def rankLessThanK(arr, k, n):
   
    # Initialize rank and
    # position
    rank = 1;
    position = 1;
 
    # Sort the given array
    arr = sorted(arr)
 
    # Traverse array from
    # right to left
    for i in range(n - 1,
                   -1, -1):
 
        # Update rank with position,
        # if adjacent elements are
        # unequal
        if (i == n - 1 or
            arr[i] != arr[i + 1]):
            rank = position;
 
            # Return position - 1, if
            # rank greater than k
            if (rank > k):
                return position - 1;
 
        # Increase position
        position += 1;
 
    return n;
 
# Driver Code
if __name__ == '__main__':
   
    # Given array
    arr = [2, 2, 3, 4, 5];
 
    N = len(arr);
 
    # Given K
    K = 4;
 
    # Function Call
    print(rankLessThanK(arr, K, N));
 
# This code is contributed by gauravrajput1


C#
// C# program for the above approach
using System;
  
class GFG{
      
// Function to find count of array
// elements with rank less than or
// equal to k
static int rankLessThanK(int[] arr,
                         int k, int n)
{
     
    // Initialize rank and position
    int rank = 1;
    int position = 1;
 
    // Sort the given array
    Array.Sort(arr);
     
    // Traverse array from right to left
    for(int i = n - 1; i >= 0; i--)
    {
         
        // Update rank with position, if
        // adjacent elements are unequal
        if (i == n - 1 || arr[i] != arr[i + 1])
        {
            rank = position;
 
            // Return position - 1, if
            // rank greater than k
            if (rank > k)
                return position - 1;
        }
 
        // Increase position
        position++;
    }
    return n;
}      
  
// Driver Code
public static void Main()
{
     
    // Given array
    int[] arr = { 2, 2, 3, 4, 5 };
  
    int N = arr.Length;
  
    // Given K
    int K = 4;
  
    // Function Call
    Console.WriteLine(rankLessThanK(
        arr, K, N));
}
}
 
// This code is contributed by sanjoy_62


输出
5

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