Given an array arr[] of size N, the task is to count the minimum number of elements required to be removed from the given array such that whenever any pair (arr[i], arr[j]) is picked, where i != j and 0 ≤ i < j < N, either arr[i] is multiple of arr[j] or vice versa.
Examples:
Input: N = 5, arr[] = {4, 3, 4, 5, 2}
Output: 2
Explanation: Currently, pair (arr[2], arr[3]) does not satisfy given condition.
Similarly, with i = 4, and i =5 as well.
Remove arr[2]= 3, and arr[4] = 5, to would make array satisfy given conditions.
Therefore minimum removal of elements is 2. And array is {4, 4, 2}.Input: N = 3, arr[] = {2, 2, 4}
Output: 0
Explanation: As, array already satisfies the given condition, there’s no need to remove any element.
Approach: The problem can be solved by using Dynamic Programming and Sieve of Eratosthenes based on the following idea:
Get the frequencies of the array elements. Now find the element having maximum number of multiples present in the array using the frequency table and dynamic programming. All the remaining elements should be deleted to achieve the condition using minimum deletions.
Follow the steps mentioned below to solve the problem:
- Traverse the arr[] and store the maximum element.
- Store the frequency of the array elements.
- Traversing through dp array and store frequency of ith element in dp[i].
- Then use Sieve of Eratosthenes to traverse through multiples of i.
- Update the current dp value with the maximum of dp[i] and current dp value.
- Finding the max element in dp[] array after doing operations
Below is the implementation of the above approach:
// C++ code for the above approach #include <bits/stdc++.h> using namespace std;
// Function to do required operation int solve( int N, vector< int >& 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< int > 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< int > 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< int > arr = { 4, 3, 4, 5, 2 };
// Function call
cout << solve(N, arr);
return 0;
} |
// 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. |
# 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# 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. |
<script> // JavaScript code for the above approach
const INT_MIN = -2147483647 - 1;
const INT_MAX = 2147483647;
// Function to do required operation
const solve = (N, arr) => {
// Initializing maxi to store
// max element in given array
let maxi = INT_MIN;
// Finding out max element in
// given array arr
for (let i = 0; i < N; i++) {
maxi = Math.max(maxi, arr[i]);
}
// Initializing a vector
// to store frequency of array elements
let freq = new Array(maxi + 1).fill(0);
// Traversing from 0 to N
// to store frequency of array elements
for (let i = 0; i < N; i++) {
freq[arr[i]]++;
}
// Initializing dp vector
let dp = new Array(maxi + 1).fill(0);
// Initializing final answer
// to store minimum steps
let answer = 0;
// Traversing through dp array
for (let 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 (let 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
let max_in_dp = Math.max(...dp);
// Printing the final answer
return (N - max_in_dp);
}
// Driver Code
// Taking input
let N = 5;
let arr = [4, 3, 4, 5, 2];
// Function call
document.write(solve(N, arr));
// This code is contributed by rakeshsahni </script> |
2
Time Complexity: O(M*log(M)), where M is maximum element present in given array
Auxiliary Space: O(M)