Minimize removal from Array so that in any pair one element is multiple of other
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++
#include <bits/stdc++.h>
using namespace std;
int solve( int N, vector< int >& arr)
{
int maxi = INT_MIN;
for ( int i = 0; i < N; i++) {
maxi = max(maxi, arr[i]);
}
vector< int > freq(maxi + 1);
for ( int i = 0; i < N; i++) {
freq[arr[i]]++;
}
vector< int > dp(maxi + 1);
int answer = 0;
for ( int i = 1; i <= maxi; i++) {
dp[i] += freq[i];
for ( int j = 2 * i; j <= maxi;
j += i) {
dp[j] = max(dp[j], dp[i]);
}
}
int max_in_dp
= *max_element(dp.begin(), dp.end());
return (N - max_in_dp);
}
int main()
{
int N = 5;
vector< int > arr = { 4, 3, 4, 5, 2 };
cout << solve(N, arr);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int solve( int N, int [] arr)
{
int maxi = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++) {
maxi = Math.max(maxi, arr[i]);
}
int [] freq = new int [maxi + 1 ];
for ( int i = 0 ; i < N; i++) {
freq[arr[i]]++;
}
int [] dp = new int [maxi + 1 ];
int answer = 0 ;
for ( int i = 1 ; i <= maxi; i++) {
dp[i] += freq[i];
for ( int j = 2 * i; j <= maxi;
j += i) {
dp[j] = Math.max(dp[j], dp[i]);
}
}
int max_in_dp
= Arrays.stream(dp).max().getAsInt();
return (N - max_in_dp);
}
public static void main (String[] args)
{
int N = 5 ;
int arr[] = { 4 , 3 , 4 , 5 , 2 };
System.out.print(solve(N, arr));
}
}
|
Python3
def solve(N, arr):
maxi = - 9999999 ;
for i in range (N):
maxi = max (maxi, arr[i]);
freq = [ 0 ] * (maxi + 1 );
for i in range (N):
freq[arr[i]] = freq[arr[i]] + 1 ;
answer = 0 ;
dp = [ 0 ] * (maxi + 1 );
for i in range ( 1 , maxi):
dp[i] = dp[i] + freq[i];
for j in range ( 2 * i,maxi, i):
dp[j] = max (dp[j], dp[i]);
max_in_dp = max (dp);
return (N - max_in_dp);
N = 5 ;
arr = [ 4 , 3 , 4 , 5 , 2 ];
print (solve(N, arr));
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG{
static int solve( int N, int [] arr)
{
int maxi = Int32.MinValue;
for ( int i = 0; i < N; i++) {
maxi = Math.Max(maxi, arr[i]);
}
int [] freq = new int [maxi + 1];
for ( int i = 0; i < N; i++) {
freq[arr[i]]++;
}
int [] dp = new int [maxi + 1];
int answer = 0;
for ( int i = 1; i <= maxi; i++) {
dp[i] += freq[i];
for ( int j = 2 * i; j <= maxi;
j += i) {
dp[j] = Math.Max(dp[j], dp[i]);
}
}
int max_in_dp
= dp.Max();
return (N - max_in_dp);
}
static public void Main (){
int N = 5;
int [] arr = { 4, 3, 4, 5, 2 };
Console.Write(solve(N, arr));
}
}
|
Javascript
<script>
const INT_MIN = -2147483647 - 1;
const INT_MAX = 2147483647;
const solve = (N, arr) => {
let maxi = INT_MIN;
for (let i = 0; i < N; i++) {
maxi = Math.max(maxi, arr[i]);
}
let freq = new Array(maxi + 1).fill(0);
for (let i = 0; i < N; i++) {
freq[arr[i]]++;
}
let dp = new Array(maxi + 1).fill(0);
let answer = 0;
for (let i = 1; i <= maxi; i++) {
dp[i] += freq[i];
for (let j = 2 * i; j <= maxi;
j += i) {
dp[j] = Math.max(dp[j], dp[i]);
}
}
let max_in_dp = Math.max(...dp);
return (N - max_in_dp);
}
let N = 5;
let arr = [4, 3, 4, 5, 2];
document.write(solve(N, arr));
</script>
|
Time Complexity: O(M*log(M)), where M is maximum element present in given array
Auxiliary Space: O(M)
Last Updated :
07 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...