📌  相关文章
📜  最小化从 Array 中的删除,以便在任何一对中,一个元素是其他元素的倍数

📅  最后修改于: 2022-05-13 01:56:04.516000             🧑  作者: Mango

最小化从 Array 中的删除,以便在任何一对中,一个元素是其他元素的倍数

给定尺寸n的数组arr [] ,任务是计算从给定阵列中删除所需的最小元素数,以便在挑选任何对(ARR [I],ARR [J]) ,在哪里! = j0 ≤ i < j < N ,要么 arr[i] 是 arr[j] 的倍数,反之亦然。

例子:

方法:该问题可以通过基于以下思想的动态规划和埃拉托色尼筛法来解决:

请按照以下步骤解决问题:

  • 遍历arr[]并存储最大元素。
  • 存储数组元素的频率。
  • 遍历dp数组并将第 i 个元素的频率存储在dp[i]中。
    • 然后使用 Eratosthenes 的 Sieve 遍历 i 的倍数。
    • 用 dp[i] 和当前 dp 值的最大值更新当前 dp 值。
  • 执行操作后在dp[]数组中查找最大元素

下面是上述方法的实现:

C++
// C++ code for the above approach
 
#include 
using namespace std;
 
// Function to do required operation
int solve(int N, vector& arr)
{
    // Initializing maxi to store
    // max element in given array
    int maxi = INT_MIN;
 
    // Finding out max element in
    // given array arr
    for (int i = 0; i < N; i++) {
        maxi = max(maxi, arr[i]);
    }
 
    // Initializing a vector
    // to store frequency of array elements
    vector freq(maxi + 1);
 
    // Traversing from 0 to N
    // to store frequency of array elements
    for (int i = 0; i < N; i++) {
        freq[arr[i]]++;
    }
 
    // Initializing dp vector
    vector dp(maxi + 1);
 
    // Initializing final answer
    // to store minimum steps
    int answer = 0;
 
    // Traversing through dp array
    for (int i = 1; i <= maxi; i++) {
 
        // Storing frequency of i'th
        // element in dp[i]
        dp[i] += freq[i];
 
        // Using sieve of Eratosthenes to
        // traverse through multiples
        // of i
        for (int j = 2 * i; j <= maxi;
             j += i) {
 
            // Updating dp[j] as discussed
            // in approach with max of dp[j]
            // and occurrence of i
            dp[j] = max(dp[j], dp[i]);
        }
    }
 
    // Finding the max element in
    // dp vector after doing operations
    int max_in_dp
        = *max_element(dp.begin(), dp.end());
 
    // Printing the final answer
    return (N - max_in_dp);
}
 
// Driver Code
int main()
{
    // Taking input
    int N = 5;
    vector arr = { 4, 3, 4, 5, 2 };
 
    // Function call
    cout << solve(N, arr);
    return 0;
}


Java
// Java code for the above approach
import java.util.*;
 
class GFG {
 
  // Function to do required operation
  static int solve(int N, int[] arr)
  {
     
    // Initializing maxi to store
    // max element in given array
    int maxi = Integer.MIN_VALUE;
 
    // Finding out max element in
    // given array arr
    for (int i = 0; i < N; i++) {
      maxi = Math.max(maxi, arr[i]);
    }
 
    // Initializing a vector
    // to store frequency of array elements
    int[] freq = new int[maxi + 1];
 
    // Traversing from 0 to N
    // to store frequency of array elements
    for (int i = 0; i < N; i++) {
      freq[arr[i]]++;
    }
 
    // Initializing dp vector
    int[] dp = new int[maxi + 1];
 
    // Initializing final answer
    // to store minimum steps
    int answer = 0;
 
    // Traversing through dp array
    for (int i = 1; i <= maxi; i++) {
 
      // Storing frequency of i'th
      // element in dp[i]
      dp[i] += freq[i];
 
      // Using sieve of Eratosthenes to
      // traverse through multiples
      // of i
      for (int j = 2 * i; j <= maxi;
           j += i) {
 
        // Updating dp[j] as discussed
        // in approach with max of dp[j]
        // and occurrence of i
        dp[j] = Math.max(dp[j], dp[i]);
      }
    }
 
    // Finding the max element in
    // dp vector after doing operations
    int max_in_dp
      = Arrays.stream(dp).max().getAsInt();
 
    // Printing the final answer
    return (N - max_in_dp);
  }
 
  // Driver Code
  public static void main (String[] args)
  {
     
    // Taking input
    int N = 5;
    int arr[] = { 4, 3, 4, 5, 2 };
 
    // Function call
    System.out.print(solve(N, arr));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python code for the above approach
 
# Function to do required operation
def solve(N, arr):
 
    # Initializing maxi to store
    # max element in given array
    maxi = -9999999;
 
    # Finding out max element in
    # given array arr
    for i in range(N):
        maxi = max(maxi, arr[i]);
     
    # Initializing a vector
    # to store frequency of array elements
    freq = [0]*(maxi+1);
 
    # Traversing from 0 to N
    # to store frequency of array elements
    for i in range(N):
        freq[arr[i]] = freq[arr[i]] + 1;
     
    # Initializing final answer
    # to store minimum steps
    answer = 0;
    dp = [0]*(maxi + 1);
     
    # Traversing through dp array
    for i in range(1, maxi):
 
        # Storing frequency of i'th
        # element in dp[i]
        dp[i] = dp[i]+  freq[i];
 
        # Using sieve of Eratosthenes to
        # traverse through multiples
        # of i
        for j in range(2 * i,maxi, i):
             
            # Updating dp[j] as discussed
            # in approach with max of dp[j]
            # and occurrence of i
            dp[j] = max(dp[j], dp[i]);
         
    # Finding the max element in
    # dp vector after doing operations
    max_in_dp = max(dp);
 
    # Printing the final answer
    return (N - max_in_dp);
 
# Driver Code
 
    # Taking input
N = 5;
arr = [4, 3, 4, 5, 2];
 
    # Function call
print(solve(N, arr));
   
# This code is contributed by Potta Lokesh


C#
// C# implementation of above approach
using System;
using System.Collections.Generic;
using System.Linq;
 
public class GFG{
 
  // Function to do required operation
  static int solve(int N, int[] arr)
  {
 
    // Initializing maxi to store
    // max element in given array
    int maxi = Int32.MinValue;
 
    // Finding out max element in
    // given array arr
    for (int i = 0; i < N; i++) {
      maxi = Math.Max(maxi, arr[i]);
    }
 
    // Initializing a vector
    // to store frequency of array elements
    int[] freq = new int[maxi + 1];
 
    // Traversing from 0 to N
    // to store frequency of array elements
    for (int i = 0; i < N; i++) {
      freq[arr[i]]++;
    }
 
    // Initializing dp vector
    int[] dp = new int[maxi + 1];
 
    // Initializing final answer
    // to store minimum steps
    int answer = 0;
 
    // Traversing through dp array
    for (int i = 1; i <= maxi; i++) {
 
      // Storing frequency of i'th
      // element in dp[i]
      dp[i] += freq[i];
 
      // Using sieve of Eratosthenes to
      // traverse through multiples
      // of i
      for (int j = 2 * i; j <= maxi;
           j += i) {
 
        // Updating dp[j] as discussed
        // in approach with max of dp[j]
        // and occurrence of i
        dp[j] = Math.Max(dp[j], dp[i]);
      }
    }
 
    // Finding the max element in
    // dp vector after doing operations
    int max_in_dp
      = dp.Max();
 
    // Printing the final answer
    return (N - max_in_dp);
  }
 
  // Driver Code
  static public void Main (){
 
    // Taking input
    int N = 5;
    int[] arr = { 4, 3, 4, 5, 2 };
 
    // Function call
    Console.Write(solve(N, arr));
  }
}
 
// This code is contributed by code_hunt.


Javascript


输出
2

时间复杂度: O(M*log(M)),其中 M 是给定数组中存在的最大元素
辅助空间: O(M)