Minimize steps to reduce Array by deleting an element and all multiples in a step
Given an array arr[] of size N, the task is to find the minimum number of operations to reduce the array by deleting an element and all its multiples from the array in one operation.
Examples:
Input: N = 5, arr[] = {5, 2, 3, 8, 7}
Output: 4
Explanation: Removing 2, will make it remove 8 (as it is a multiple of 2),
then 3, 5, 7 would be removed in one operation each,
Thus, total count = 1 + 3 = 4 (Required Answer)
Input: N = 7, arr[] = {12, 10, 2, 3, 5, 6, 27}
Output: 3
Explanation: Removing 2 and its multiples 10, 12, 6 in one operation. So count = 1.
Then, remove 3 and 27 (multiple of 3) in one operation. So total count = 1+1 = 2.
Last element left would be 5 which will again take one operation.
Thus, total count = 2 + 1 = 3 (Required Answer)
Approach: The approach to the solution is based on the following idea:
Sort the array and then start iterating from the start.
For each element group all its multiples together if it already is not part of any group.
The total number of group is the required answer.
Follow the steps to solve the problem:
- Sort the array in increasing order.
- Iterate from the i = 0 to N-1 and:
- If this element is part of any group or not.
- If not then group this element and all its multiples together.
- Otherwise, skip this and continue iteration.
- Return the count of the total number of groups.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int n, vector< int >& v)
{
int M = INT_MIN;
for ( int i = 0; i < n; i++)
M = max(M, v[i]);
vector< int > vis(M + 1, 0);
sort(v.begin(), v.end());
for ( int i = 0; i < v.size(); i++) {
if (v[i] == 1) {
cout << 1 << "\n" ;
return ;
}
}
int count = 0;
for ( int i = 0; i < v.size(); i++) {
if (vis[v[i]] == 0) {
vis[v[i]] = 1;
int temp = v[i];
for ( int i = 1; i <= M; i++) {
if (i * temp <= M) {
vis[i * temp] = 1;
}
}
count++;
}
}
cout << count << "\n" ;
}
int main()
{
int N = 5;
vector< int > arr = { 5, 2, 3, 8, 7 };
solve(N, arr);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static void solve( int n, int [] v)
{
int M = Integer.MIN_VALUE;
for ( int i = 0 ; i < n; i++)
M = Math.max(M, v[i]);
int [] vis = new int [M + 1 ];
for ( int i = 0 ; i < M + 1 ; i++) {
vis[i] = 0 ;
}
Arrays.sort(v);
for ( int i = 0 ; i < v.length; i++) {
if (v[i] == 1 ) {
System.out.println( 1 );
return ;
}
}
int count = 0 ;
for ( int i = 0 ; i < v.length; i++) {
if (vis[v[i]] == 0 ) {
vis[v[i]] = 1 ;
int temp = v[i];
for ( int j = 1 ; j <= M; j++) {
if (j * temp <= M) {
vis[j * temp] = 1 ;
}
}
count++;
}
}
System.out.println(count);
}
public static void main(String args[])
{
int N = 5 ;
int [] arr = { 5 , 2 , 3 , 8 , 7 };
solve(N, arr);
}
}
|
Python3
INT_MIN = - 2147483647 - 1
def solve(n, v):
M = INT_MIN
for i in range (n):
M = max (M, v[i])
vis = [ 0 ] * (M + 1 )
v.sort()
for i in range ( len (v)):
if (v[i] = = 1 ):
print ( 1 )
return
count = 0
for i in range ( len (v)):
if (vis[v[i]] = = 0 ):
vis[v[i]] = 1
temp = v[i]
for i in range ( 1 ,M + 1 ):
if (i * temp < = M):
vis[i * temp] = 1
count + = 1
print (count)
N = 5
arr = [ 5 , 2 , 3 , 8 , 7 ]
solve(N, arr)
|
C#
using System;
class GFG {
static void solve( int n, int [] v)
{
int M = Int32.MinValue;
for ( int i = 0; i < n; i++)
M = Math.Max(M, v[i]);
int [] vis = new int [M + 1];
for ( int i = 0; i < M + 1; i++) {
vis[i] = 0;
}
Array.Sort(v);
for ( int i = 0; i < v.Length; i++) {
if (v[i] == 1) {
Console.WriteLine(1);
return ;
}
}
int count = 0;
for ( int i = 0; i < v.Length; i++) {
if (vis[v[i]] == 0) {
vis[v[i]] = 1;
int temp = v[i];
for ( int j = 1; j <= M; j++) {
if (j * temp <= M) {
vis[j * temp] = 1;
}
}
count++;
}
}
Console.WriteLine(count);
}
public static void Main()
{
int N = 5;
int [] arr = { 5, 2, 3, 8, 7 };
solve(N, arr);
}
}
|
Javascript
<script>
const INT_MIN = -2147483647 - 1;
const solve = (n, v) => {
let M = INT_MIN;
for (let i = 0; i < n; i++)
M = Math.max(M, v[i]);
let vis = new Array(M + 1).fill(0);
v.sort();
for (let i = 0; i < v.length; i++) {
if (v[i] == 1) {
document.write( "1<br/>" );
return ;
}
}
let count = 0;
for (let i = 0; i < v.length; i++) {
if (vis[v[i]] == 0) {
vis[v[i]] = 1;
let temp = v[i];
for (let i = 1; i <= M; i++) {
if (i * temp <= M) {
vis[i * temp] = 1;
}
}
count++;
}
}
document.write(`${count}<br/>`);
}
let N = 5;
let arr = [5, 2, 3, 8, 7];
solve(N, arr);
</script>
|
Time Complexity: O(N*M) where M is the maximum element of the array
Auxiliary Space: O(M)
Last Updated :
21 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...