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

📅  最后修改于: 2021-09-04 08:22:13             🧑  作者: Mango

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

例子:

朴素的方法:最简单的方法是找到数组中当前的最大元素并将其与之前的最大元素进行比较。如果它们相等,则前一个元素和当前元素的秩必须相等。否则,将当前最大元素的秩指定为比先前获得的最大值数大 1 的等级。重复此操作,直到给定数组变空或秩大于K ,以较早者为准。遍历后,打印被删除元素的总数。

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

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

  • 按升序对给定的arr[]进行排序。
  • 1初始化变量rankposition ,分别存储当前数组元素的rank和位置。
  • 将排序后的数组从i = (N – 1)遍历到0并执行以下操作:
    • 当相邻数组元素不相等或i等于(N – 1)时,用位置更新排名
    • 返回位置 –如果 rank 大于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


Javascript


输出
5

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live