📌  相关文章
📜  删除数组元素,将每个数组元素的频率减少到最多 K

📅  最后修改于: 2021-09-05 11:55:04             🧑  作者: Mango

给定一个大小为N的排序数组arr[]和一个整数K ,任务是通过删除最小数量的数组元素来打印数组,以使每个元素的频率最多为K

例子:

朴素的方法:解决这个问题最简单的方法是遍历数组并检查数组元素的频率是否大于K。如果发现为真,则从数组中删除当前元素。否则,增加数组元素的频率并打印数组元素。

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

有效方法:按照以下步骤解决问题:

  • 初始化一个变量,比如j = 0以通过删除数组元素来存储任何数组元素的索引,使得每个不同元素的频率最多为K
  • 遍历数组并检查j < Karr[i] > arr[j – K]是否为真。如果发现为真,则更新arr[j++] = arr[i]
  • 最后,通过删除索引大于或等于j 的所有数组元素来打印数组。

下面是上述方法的实现。

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to remove array elements
// such that frequency of each distinct
// array element is at most K
vector RemoveElemArr(vector& arr,
                        int n, int k)
{
    // Base Case
    if (n == 0 || n == 1)
        return arr;
 
    // Stores index of array element by removing
    // the array element such that the frequency
    // of array elements is at most K
    int j = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // If j < k or arr[i] > arr[j - k]
        if (j < k || arr[i] > arr[j - k]) {
 
            // Update arr[j]
            arr[j++] = arr[i];
        }
    }
 
    // Remove array elements
    while (arr.size() > j) {
        arr.pop_back();
    }
 
    return arr;
}
 
// Function to print the array
void printArray(vector& arr)
{
 
    // Traverse the array
    for (int i = 0; i < arr.size();
        i++) {
        cout << arr[i] << " ";
    }
}
 
// Utility Function to remove array elements
// such that frequency of each distinct
// array element is at most K
void UtilRemov(vector& arr, int n, int k)
{
    arr = RemoveElemArr(arr, n, k);
 
    // Print updated array
    printArray(arr);
}
 
// Driver Code
int main()
{
 
    vector arr
        = { 1, 2, 2, 3, 4, 4, 4, 5, 5 };
 
    int k = 2;
    int n = arr.size();
 
    UtilRemov(arr, n, k);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
// Function to remove array elements
// such that frequency of each distinct
// array element is at most K
static int [] RemoveElemArr(int []arr,
                        int n, int k)
{
   
    // Base Case
    if (n == 0 || n == 1)
        return arr;
 
    // Stores index of array element by removing
    // the array element such that the frequency
    // of array elements is at most K
    int j = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++)
    {
 
        // If j < k or arr[i] > arr[j - k]
        if (j < k || arr[i] > arr[j - k])
        {
 
            // Update arr[j]
            arr[j++] = arr[i];
        }
    }
 
    // Remove array elements
    while (arr.length > j)
    {
        arr = Arrays.copyOfRange(arr, 0, arr.length-1);
    }
 
    return arr;
}
 
// Function to print the array
static void printArray(int [] arr)
{
 
    // Traverse the array
    for (int i = 0; i < arr.length;
        i++)
    {
        System.out.print(arr[i]+ " ");
    }
}
 
// Utility Function to remove array elements
// such that frequency of each distinct
// array element is at most K
static void UtilRemov(int []arr, int n, int k)
{
    arr = RemoveElemArr(arr, n, k);
 
    // Print updated array
    printArray(arr);
}
 
// Driver Code
public static void main(String[] args)
{
 
    int []arr
        = { 1, 2, 2, 3, 4, 4, 4, 5, 5 };
    int k = 2;
    int n = arr.length;
    UtilRemov(arr, n, k);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to remove array elements
# such that frequency of each distinct
# array element is at most K
def RemoveElemArr(arr, n, k):
     
    # Base Case
    if (n == 0 or n == 1):
        return arr
 
    # Stores index of array element by
    # removing the array element such
    # that the frequency of array
    # elements is at most K
    j = 0
 
    # Traverse the array
    for i in range(n):
         
        # If j < k or arr[i] > arr[j - k]
        if (j < k or arr[i] > arr[j - k]):
             
            # Update arr[j]
            arr[j], j = arr[i], j + 1
             
    # Remove array elements
    while (len(arr) > j):
        del arr[-1]
 
    return arr
 
# Function to print the array
def printArray(arr):
     
    # Traverse the array
    for i in arr:
        print(i, end = " ")
         
# Utility Function to remove array elements
# such that frequency of each distinct
# array element is at most K
def UtilRemov(arr, n, k):
     
    arr = RemoveElemArr(arr, n, k)
 
    # Print updated array
    printArray(arr)
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 2, 2, 3, 4, 4, 4, 5, 5 ]
 
    k = 2
    n = len(arr)
 
    UtilRemov(arr, n, k)
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach 
using System;
using System.Linq;
 
class GFG
{
 
  // Function to remove array elements
  // such that frequency of each distinct
  // array element is at most K
  static int [] RemoveElemArr(int []arr,
                              int n, int k)
  {
 
    // Base Case
    if (n == 0 || n == 1)
      return arr;
 
    // Stores index of array element by removing
    // the array element such that the frequency
    // of array elements is at most K
    int j = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++)
    {
 
      // If j < k or arr[i] > arr[j - k]
      if (j < k || arr[i] > arr[j - k])
      {
 
        // Update arr[j]
        arr[j++] = arr[i];
      }
    }
 
    // Remove array elements
    while (arr.Length > j)
    {
      arr = arr.Take(arr.Length - 1).ToArray();
    }
 
    return arr;
  }
 
  // Function to print the array
  static void printArray(int [] arr)
  {
 
    // Traverse the array
    for (int i = 0; i < arr.Length;
         i++)
    {
      Console.Write(arr[i] + " ");
    }
  }
 
  // Utility Function to remove array elements
  // such that frequency of each distinct
  // array element is at most K
  static void UtilRemov(int []arr, int n, int k)
  {
    arr = RemoveElemArr(arr, n, k);
 
    // Print updated array
    printArray(arr);
  }
 
  // Driver code
  public static void Main()
  {
    int []arr
      = { 1, 2, 2, 3, 4, 4, 4, 5, 5 };
    int k = 2;
    int n = arr.Length;
    UtilRemov(arr, n, k);
  }
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
1 2 2 3 4 4 5 5

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

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