📌  相关文章
📜  减少数组,使每个元素最多出现 K 次

📅  最后修改于: 2021-09-03 03:15:53             🧑  作者: Mango

给定一个大小为N的排序数组arr ,任务是减少数组,使得每个元素最多可以出现 K 次。
例子:

方法:

  1. 遍历给定数组arr
  2. 遍历时维护数组中每个唯一元素的计数,使用指针i
  3. 如果 arr[i] 直到索引 i 的当前频率小于或等于 K,则将元素 arr[i] 添加到新的缩减数组并将频率增加 1。
  4. 如果 arr[i] 直到索引 i 的当前频率大于 K,则跳过直到找到下一个唯一元素。
  5. 遍历结束后,打印缩减后的数组。

下面是上述方法的实现:

C++
// C++ program to reduce the array
// such that each element appears
// at most K times
 
#include 
using namespace std;
 
// Function to reduce the array
void reduceArray(int arr[], int n, int K)
{
    // Vector to store the reduced array
    vector vec;
    int size = 0;
    int curr_ele = arr[0], curr_freq = 1;
 
    // Iterate over the array
    for (int i = 0; i < n; i++) {
 
        if (curr_ele == arr[i]
            && curr_freq <= K) {
            vec.push_back(arr[i]);
            size++;
        }
        else if (curr_ele != arr[i]) {
            curr_ele = arr[i];
            vec.push_back(arr[i]);
            size++;
            curr_freq = 1;
        }
        curr_freq++;
    }
 
    // Print the reduced array
    cout << "{";
    for (int i = 0; i < size; i++) {
        cout << vec[i] << ", ";
    }
    cout << "}";
}
 
// Driver code
int main()
{
    int arr[]
        = { 1, 1, 1, 2,
            2, 2, 3, 3,
            3, 3, 3, 3,
            4, 5 };
 
    int n = sizeof(arr)
            / sizeof(arr[0]);
    int K = 2;
 
    // Function call
    reduceArray(arr, n, K);
 
    return 0;
}


Java
// Java program to reduce the array
// such that each element appears
// at most K times
import java.util.*;
 
class GFG{
  
// Function to reduce the array
static void reduceArray(int arr[], int n, int K)
{
    // Vector to store the reduced array
    Vector vec = new Vector();
    int size = 0;
    int curr_ele = arr[0], curr_freq = 1;
  
    // Iterate over the array
    for (int i = 0; i < n; i++) {
  
        if (curr_ele == arr[i]
            && curr_freq <= K) {
            vec.add(arr[i]);
            size++;
        }
        else if (curr_ele != arr[i]) {
            curr_ele = arr[i];
            vec.add(arr[i]);
            size++;
            curr_freq = 1;
        }
        curr_freq++;
    }
  
    // Print the reduced array
    System.out.print("{");
    for (int i = 0; i < size; i++) {
        System.out.print(vec.get(i)+ ", ");
    }
    System.out.print("}");
}
  
// Driver code
public static void main(String[] args)
{
    int arr[]
        = { 1, 1, 1, 2,
            2, 2, 3, 3,
            3, 3, 3, 3,
            4, 5 };
  
    int n = arr.length;
    int K = 2;
  
    // Function call
    reduceArray(arr, n, K);
  
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to reduce the array
# such that each element appears
# at most K times
 
# Function to reduce the array
def reduceArray(arr, n, K) :
 
    # List to store the reduced array
    vec = [];
    size = 0;
    curr_ele = arr[0]; curr_freq = 1;
 
    # Iterate over the array
    for i in range(n) :
 
        if (curr_ele == arr[i]
            and curr_freq <= K) :
            vec.append(arr[i]);
            size += 1;
 
        elif (curr_ele != arr[i]) :
            curr_ele = arr[i];
            vec.append(arr[i]);
            size += 1;
            curr_freq = 1;
         
        curr_freq += 1;
 
    # Print the reduced array
    print("{",end= "");
    for i in range(size) :
        print(vec[i] ,end= ", ");
     
    print("}",end="");
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 1, 1, 1, 2,
           2, 2, 3, 3,
            3, 3, 3, 3,
            4, 5 ];
 
    n = len(arr)
    K = 2;
 
    # Function call
    reduceArray(arr, n, K);
 
# This code is contributed by AnkitRai01


C#
// C# program to reduce the array
// such that each element appears
// at most K times
using System;
using System.Collections.Generic;
 
class GFG{
   
// Function to reduce the array
static void reduceArray(int []arr, int n, int K)
{
    // List to store the reduced array
    List vec = new List();
    int size = 0;
    int curr_ele = arr[0], curr_freq = 1;
   
    // Iterate over the array
    for (int i = 0; i < n; i++) {
   
        if (curr_ele == arr[i]
            && curr_freq <= K) {
            vec.Add(arr[i]);
            size++;
        }
        else if (curr_ele != arr[i]) {
            curr_ele = arr[i];
            vec.Add(arr[i]);
            size++;
            curr_freq = 1;
        }
        curr_freq++;
    }
   
    // Print the reduced array
    Console.Write("{");
    for (int i = 0; i < size; i++) {
        Console.Write(vec[i]+ ", ");
    }
    Console.Write("}");
}
   
// Driver code
public static void Main(String[] args)
{
    int []arr
        = { 1, 1, 1, 2,
            2, 2, 3, 3,
            3, 3, 3, 3,
            4, 5 };
   
    int n = arr.Length;
    int K = 2;
   
    // Function call
    reduceArray(arr, n, K);
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
{1, 1, 2, 2, 3, 3, 4, 5, }

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

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