Minimum number of given operations required to reduce the array to 0 element
Last Updated :
13 Jun, 2022
Given an array arr[] of N integers. The task is to find the minimum number of given operations required to reduce the array to 0 elements. In a single operation, any element can be chosen from the array and all of its multiples get removed including itself.
Examples:
Input: arr[] = {2, 4, 6, 3, 4, 6, 8}
Output: 2
Operation 1: Choose 2 and delete all the multiples, arr[] = {3}
Operation 3: Choose 3 and the array gets reduced to 0 element.
Input: arr[] = {2, 4, 2, 4, 4, 4}
Output: 1
Naive approach: Find minimum from the array at each step and traverse the entire array to find multiples of these elements and delete them.
Efficient approach:
- Create a count array that stores the count of each number in the array.
- Since we know that for a number x the elements which satisfy the condition (A % x == 0) are actually the multiples of x and hence we need to find the multiples for every number and set their frequencies to 0 including the chosen element itself.
- Now for every number, we traverse its multiples once and subtract the value of the count of that number from all of its multiples.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the minimum
// operations required
int minOperations(int* arr, int n)
{
int maxi, result = 0;
// Count the frequency of each element
vector<int> freq(1000001, 0);
for (int i = 0; i < n; i++) {
int x = arr[i];
freq[x]++;
}
// Maximum element from the array
maxi = *(max_element(arr, arr + n));
for (int i = 1; i <= maxi; i++) {
if (freq[i] != 0) {
// Find all the multiples of i
for (int j = i * 2; j <= maxi; j = j + i) {
// Delete the multiples
freq[j] = 0;
}
// Increment the operations
result++;
}
}
return result;
}
// Driver code
int main()
{
int arr[] = { 2, 4, 2, 4, 4, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << minOperations(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.Arrays;
class GFG
{
// Function to return the minimum
// operations required
static int minOperations(int[] arr, int n)
{
int maxi, result = 0;
// Count the frequency of each element
int[] freq = new int[1000001];
for (int i = 0; i < n; i++)
{
int x = arr[i];
freq[x]++;
}
// Maximum element from the array
maxi = Arrays.stream(arr).max().getAsInt();
for (int i = 1; i <= maxi; i++)
{
if (freq[i] != 0)
{
// Find all the multiples of i
for (int j = i * 2; j <= maxi; j = j + i)
{
// Delete the multiples
freq[j] = 0;
}
// Increment the operations
result++;
}
}
return result;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {2, 4, 2, 4, 4, 4};
int n = arr.length;
System.out.println(minOperations(arr, n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
# Function to return the minimum
# operations required
def minOperations(arr, n):
result = 0
# Count the frequency of each element
freq = [0] * 1000001
for i in range(0, n):
freq[arr[i]] += 1
# Maximum element from the array
maxi = max(arr)
for i in range(1, maxi+1):
if freq[i] != 0:
# Find all the multiples of i
for j in range(i * 2, maxi+1, i):
# Delete the multiples
freq[j] = 0
# Increment the operations
result += 1
return result
# Driver code
if __name__ == "__main__":
arr = [2, 4, 2, 4, 4, 4]
n = len(arr)
print(minOperations(arr, n))
# This code is contributed by Rituraj Jain
C#
// C# implementation of above approach
using System;
using System.Linq;
class GFG
{
// Function to return the minimum
// operations required
static int minOperations(int[] arr, int n)
{
int maxi, result = 0;
// Count the frequency of each element
int[] freq = new int[1000001];
for (int i = 0; i < n; i++)
{
int x = arr[i];
freq[x]++;
}
// Maximum element from the array
maxi = arr.Max();
for (int i = 1; i <= maxi; i++)
{
if (freq[i] != 0)
{
// Find all the multiples of i
for (int j = i * 2; j <= maxi; j = j + i)
{
// Delete the multiples
freq[j] = 0;
}
// Increment the operations
result++;
}
}
return result;
}
// Driver code
public static void Main(String[] args)
{
int []arr = {2, 4, 2, 4, 4, 4};
int n = arr.Length;
Console.WriteLine(minOperations(arr, n));
}
}
// This code is contributed by Rajput-Ji
PHP
<?php
// PHP implementation of the approach
// Function to return the minimum
// operations required
function minOperations($arr, $n)
{
$result = 0;
$freq = array();
// Count the frequency of each element
for($i = 0; $i < $n; $i++)
{
$freq[$arr[$i]] = 0;
}
for ($i = 0; $i < $n; $i++)
{
$x = $arr[$i];
$freq[$x]++;
}
// Maximum element from the array
$maxi = max($arr);
for ($i = 1; $i <= $maxi; $i++)
{
if ($freq[$i] != 0)
{
// Find all the multiples of i
for ($j = $i * 2;
$j <= $maxi; $j = $j + $i)
{
// Delete the multiples
$freq[$j] = 0;
}
// Increment the operations
$result++;
}
}
return $result;
}
// Driver code
$arr = array( 2, 4, 2, 4, 4, 4 );
$n = count($arr);
echo minOperations($arr, $n);
// This code is contributed by AnkitRai01
?>
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the minimum
// operations required
function minOperations(arr, n)
{
let maxi, result = 0;
// Count the frequency of each element
let freq = new Array(1000001).fill(0);
for (let i = 0; i < n; i++) {
let x = arr[i];
freq[x]++;
}
// Maximum element from the array
maxi = Math.max(...arr);
for (let i = 1; i <= maxi; i++) {
if (freq[i] != 0) {
// Find all the multiples of i
for (let j = i * 2; j <= maxi; j = j + i) {
// Delete the multiples
freq[j] = 0;
}
// Increment the operations
result++;
}
}
return result;
}
// Driver code
let arr = [ 2, 4, 2, 4, 4, 4 ];
let n = arr.length;
document.write(minOperations(arr, n));
</script>
Time Complexity: O(n + m?m), where n represents the size of the given array and m represents the maximum element in the array.
Auxiliary Space: O(1000001), no extra space is required, so it is a constant.
Similar Reads
Minimum number of operations required to delete all elements of the array Given an integer array arr, the task is to print the minimum number of operations required to delete all elements of the array. In an operation, any element from the array can be chosen at random and every element divisible by it can be removed from the array. Examples: Input: arr[] = {2, 4, 6, 3, 5
9 min read
Minimum number of steps required to obtain the given Array by the given operations Given an array arr[] of N positive integers, the task is to find the minimum number of operations required of the following types to obtain the array arr[] from an array of zeroes only. Select any index i and increment all the elements at the indices [i, N - 1] by 1.Select any index i and decrease a
12 min read
Minimum no. of operations required to make all Array Elements Zero Given an array of N elements and each element is either 1 or 0. You need to make all the elements of the array equal to 0 by performing the below operations: If an element is 1, You can change it's value equal to 0 then, if the next consecutive element is 1, it will automatically get converted to 0.
12 min read
Minimum operations of given type required to empty given array Given an array arr[] of size N, the task is to find the total count of operations required to remove all the array elements such that if the first element of the array is the smallest element, then remove that element, otherwise move the first element to the end of the array. Examples: Input: A[] =
14 min read
Minimum operations required to make all elements of Array less than equal to 0 Given an array arr[] consisting of N positive numbers, the task is to find the minimum number of operations required to make all elements of the array less than or equal to 0. In each operation, one has to pick the minimum positive element from the array and subtract all the elements of the array fr
5 min read