Minimize steps to reduce Array by deleting an element and all multiples in a step
Last Updated :
21 Nov, 2022
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++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to do required operation
void solve(int n, vector<int>& v)
{
int M = INT_MIN;
for (int i = 0; i < n; i++)
M = max(M, v[i]);
// Initializing a visited array
// of max size of N and marking
// each element as 0(false)
vector<int> vis(M + 1, 0);
// Sorting the given array
// such that each element
// appear in increasing order
sort(v.begin(), v.end());
// Special case for 1
for (int i = 0; i < v.size(); i++) {
if (v[i] == 1) {
cout << 1 << "\n";
return;
}
}
// Initializing final count answer
int count = 0;
// Traverse in the given array
for (int i = 0; i < v.size(); i++) {
// Check if element is
// not visited yet
if (vis[v[i]] == 0) {
// Mark it as visited
vis[v[i]] = 1;
// Store that element for
// further traversing through
// its multiples
int temp = v[i];
// Then, traverse from N
// min to max range and
// mark all the multiples of
// stored element as visited
for (int i = 1; i <= M; i++) {
if (i * temp <= M) {
vis[i * temp] = 1;
}
}
// Increase count by 1 for
// each iteration
count++;
}
}
// Print the final count answer
cout << count << "\n";
}
// Driver Code
int main()
{
// Taking input
int N = 5;
vector<int> arr = { 5, 2, 3, 8, 7 };
// Function call
solve(N, arr);
return 0;
}
Java
// Java code for the above approach
import java.util.*;
public class GFG {
// Function to do required operation
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]);
// Initializing a visited array
// of max size of N and marking
// each element as 0(false)
int[] vis = new int[M + 1];
for (int i = 0; i < M + 1; i++) {
vis[i] = 0;
}
// Sorting the given array
// such that each element
// appear in increasing order
Arrays.sort(v);
// Special case for 1
for (int i = 0; i < v.length; i++) {
if (v[i] == 1) {
System.out.println(1);
return;
}
}
// Initializing final count answer
int count = 0;
// Traverse in the given array
for (int i = 0; i < v.length; i++) {
// Check if element is
// not visited yet
if (vis[v[i]] == 0) {
// Mark it as visited
vis[v[i]] = 1;
// Store that element for
// further traversing through
// its multiples
int temp = v[i];
// Then, traverse from N
// min to max range and
// mark all the multiples of
// stored element as visited
for (int j = 1; j <= M; j++) {
if (j * temp <= M) {
vis[j * temp] = 1;
}
}
// Increase count by 1 for
// each iteration
count++;
}
}
// Print the final count answer
System.out.println(count);
}
// Driver Code
public static void main(String args[])
{
// Taking input
int N = 5;
int[] arr = { 5, 2, 3, 8, 7 };
// Function call
solve(N, arr);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python3 code for the above approach
INT_MIN = -2147483647 - 1
# Function to do required operation
def solve(n, v):
M = INT_MIN
for i in range(n):
M = max(M, v[i])
# Initializing a visited array
# of max size of N and marking
# each element as 0(false)
vis = [0]*(M+1)
# Sorting the given array
# such that each element
# appear in increasing order
v.sort()
# Special case for 1
for i in range(len(v)):
if (v[i] == 1):
print(1)
return
# Initializing final count answer
count = 0
# Traverse in the given array
for i in range(len(v)):
# Check if element is
# not visited yet
if (vis[v[i]] == 0):
# Mark it as visited
vis[v[i]] = 1
# Store that element for
# further traversing through
# its multiples
temp = v[i]
# Then, traverse from N
# min to max range and
# mark all the multiples of
# stored element as visited
for i in range(1,M+1):
if (i * temp <= M):
vis[i * temp] = 1
# Increase count by 1 for
# each iteration
count += 1
# Print the final count answer
print(count)
# Driver Code
# Taking input
N = 5
arr = [5, 2, 3, 8, 7]
# Function call
solve(N, arr)
# This code is contributed by shinjanpatra
C#
// C# code for the above approach
using System;
class GFG {
// Function to do required operation
static void solve(int n, int[] v)
{
int M = Int32.MinValue;
for (int i = 0; i < n; i++)
M = Math.Max(M, v[i]);
// Initializing a visited array
// of max size of N and marking
// each element as 0(false)
int[] vis = new int[M + 1];
for (int i = 0; i < M + 1; i++) {
vis[i] = 0;
}
// Sorting the given array
// such that each element
// appear in increasing order
Array.Sort(v);
// Special case for 1
for (int i = 0; i < v.Length; i++) {
if (v[i] == 1) {
Console.WriteLine(1);
return;
}
}
// Initializing final count answer
int count = 0;
// Traverse in the given array
for (int i = 0; i < v.Length; i++) {
// Check if element is
// not visited yet
if (vis[v[i]] == 0) {
// Mark it as visited
vis[v[i]] = 1;
// Store that element for
// further traversing through
// its multiples
int temp = v[i];
// Then, traverse from N
// min to max range and
// mark all the multiples of
// stored element as visited
for (int j = 1; j <= M; j++) {
if (j * temp <= M) {
vis[j * temp] = 1;
}
}
// Increase count by 1 for
// each iteration
count++;
}
}
// Print the final count answer
Console.WriteLine(count);
}
// Driver Code
public static void Main()
{
// Taking input
int N = 5;
int[] arr = { 5, 2, 3, 8, 7 };
// Function call
solve(N, arr);
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
<script>
// JavaScript code for the above approach
const INT_MIN = -2147483647 - 1;
// Function to do required operation
const solve = (n, v) => {
let M = INT_MIN;
for (let i = 0; i < n; i++)
M = Math.max(M, v[i]);
// Initializing a visited array
// of max size of N and marking
// each element as 0(false)
let vis = new Array(M + 1).fill(0);
// Sorting the given array
// such that each element
// appear in increasing order
v.sort();
// Special case for 1
for (let i = 0; i < v.length; i++) {
if (v[i] == 1) {
document.write("1<br/>");
return;
}
}
// Initializing final count answer
let count = 0;
// Traverse in the given array
for (let i = 0; i < v.length; i++) {
// Check if element is
// not visited yet
if (vis[v[i]] == 0) {
// Mark it as visited
vis[v[i]] = 1;
// Store that element for
// further traversing through
// its multiples
let temp = v[i];
// Then, traverse from N
// min to max range and
// mark all the multiples of
// stored element as visited
for (let i = 1; i <= M; i++) {
if (i * temp <= M) {
vis[i * temp] = 1;
}
}
// Increase count by 1 for
// each iteration
count++;
}
}
// Print the final count answer
document.write(`${count}<br/>`);
}
// Driver Code
// Taking input
let N = 5;
let arr = [5, 2, 3, 8, 7];
// Function call
solve(N, arr);
// This code is contributed by rakeshsahni
</script>
Time Complexity: O(N*M) where M is the maximum element of the array
Auxiliary Space: O(M)
Similar Reads
Minimum steps required to reduce all array elements to 1 based on given steps Given an array arr[] of size N. The task is to find the minimum steps required to reduce all array elements to 1. In each step, perform the following given operation: Choose any starting index, say i, and jump to the (arr[i] + i)th index, reducing ith as well as (arr[i] + i)th index by 1, follow thi
8 min read
Minimize steps to make Array elements equal by deleting adjacent pair and inserting bitwise OR Given an array arr[], consisting of N integers, the task is to find the minimum number of moves required to make all numbers in the resulting array equal. In one move, Take two adjacent numbers arr[i] and arr[i+1], and delete them. Insert the bitwise OR of arr[i] and arr[i+1] at the deleted position
6 min read
Minimize deletions in Array by deleting all occurrences of any number such that array size is reduced to at least half Given an array arr[] of positive integers, the task is to select an element from the array and delete all its occurrences, such that the number of elements selected are minimum and the array size becomes atleast half of its original size.Note: Size of the given array is always even. Example: Input:
10 min read
Minimize moves required to make array elements equal by incrementing and decrementing pairs | Set 2 Given an array arr[] of size N, the task is to print the minimum number of moves required to make all array elements equal by selecting any pair of distinct indices and then increment the element at the first index and decrement the element at the other index by 1 each time. If it is impossible to m
7 min read
Minimize subarray increments/decrements required to reduce all array elements to 0 Given an array arr[], select any subarray and apply any one of the below operations on each element of the subarray: Increment by oneDecrement by one The task is to print the minimum number of above-mentioned increment/decrement operations required to reduce all array elements to 0. Examples: Input:
5 min read