Minimize removal from Array so that in any pair one element is multiple of other
Last Updated :
07 Apr, 2022
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++
// 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
// 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
<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>
Time Complexity: O(M*log(M)), where M is maximum element present in given array
Auxiliary Space: O(M)
Similar Reads
Closest pair in an Array such that one number is multiple of the other Given an array arr[] of integers of size N, the task is to find the closest pair in the given array such that one element is the multiple of the other. If no such pair exists then print -1. Note: Closest pair means the difference between the index of any two elements must be minimum. Examples: Input
6 min read
Minimize Array partitions such that an element is repeated atmost once in each partition Given an array arr[] consisting of positive integers. The task is to minimize the contiguous groups formed if each element from arr[] can be a part of atmost one group, and a number can be repeated only once. Examples: Input: arr[] = {1, 2, 1, 1, 1, 1, 1, 2, 3}Output: { {1, 2, 1}, {1, 1}, {1, 1, 2,
6 min read
Minimize steps to make Array elements 0 by reducing same A[i] - X from Subarray Given an array A[] of size N, the task is to find the minimum number of operations required to make all the elements of the array zero. In one step, the following operations are done on a subarray of the given array: Any integer X is chosenIf an element is greater than X (A[i] > X), the array ele
6 min read
Pairs such that one is a power multiple of other You are given an array A[] of n-elements and a positive integer k(other than 1). Now you have find the number of pairs Ai, Aj such that Ai = Aj*(kx) where x is an integer. Given that (k?1). Note: (Ai, Aj) and (Aj, Ai) must be count once. Examples : Input : A[] = {3, 6, 4, 2}, k = 2 Output : 2 Explan
6 min read
Minimize cost of increments or decrements such that same indexed elements become multiple of each other Given two arrays A[] and B[] consisting of N integers, the task is to minimize the total cost of incrementing or decrementing array elements by 1 such that for every ith element, either A[i] is a multiple of B[i] or vice-versa. Examples: Input: A[] = {3, 6, 3}, B[] = {4, 8, 13}Output: 4Explanation:I
6 min read
Minimize the sum of MEX by removing all elements of array Given an array of integers arr[] of size N. You can perform the following operation N times: Pick any index i, and remove arr[i] from the array and add MEX(arr[]) i.e., Minimum Excluded of the array arr[] to your total score. Your task is to minimize the total score. Examples: Input: N = 8, arr[] =
7 min read