# 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, 10}
Output: 3
Choosing 2 as the first element will remove 2, 4, 6 and 10 from the array.
Now choose 3 which will result in the removal of 3.
Finally, the only element left to choose is 5.

Input: arr[] = {2, 5, 3, 7, 11}
Output: 5

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: For optimal results, the smallest element from the array should be chosen from the remaining elements one after another until all the elements of the array are deleted.

• Sort the array in ascending order and prepare a hash for occurrences.
• For each unmarked element starting from beginning mark all elements which are divisible by choose element, and increase the result counter.

Below is the implementstion of the above approach

## C++

 `// C++ implementation of the above approach ` ` `  `#include ` `#define MAX 10000 ` ` `  `using` `namespace` `std; ` ` `  `int` `hashTable[MAX]; ` ` `  `// function to find minimum operations ` `int` `minOperations(``int` `arr[], ``int` `n) ` `{ ` `    ``// sort array ` `    ``sort(arr, arr + n); ` ` `  `    ``// prepare hash of array ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``hashTable[arr[i]]++; ` ` `  `    ``int` `res = 0; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``if` `(hashTable[arr[i]]) { ` `            ``for` `(``int` `j = i; j < n; j++) ` `                ``if` `(arr[j] % arr[i] == 0) ` `                    ``hashTable[arr[j]] = 0; ` `            ``res++; ` `        ``} ` `    ``} ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``int` `arr[] = { 4, 6, 2, 8, 7, 21, 24, 49, 44 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``cout << minOperations(arr, n); ` `    ``return` `0; ` `} `

## Java

 `//Java implementation of the above approach  ` `import` `java.util.*; ` `class` `Solution ` `{ ` `static` `final` `int` `MAX=``10000``;  ` ` `  `static` `int` `hashTable[]= ``new` `int``[MAX];  ` ` `  `// function to find minimum operations  ` `static` `int` `minOperations(``int` `arr[], ``int` `n)  ` `{  ` `    ``// sort array  ` `    ``Arrays.sort(arr);  ` ` `  `    ``// prepare hash of array  ` `    ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``hashTable[arr[i]]++;  ` ` `  `    ``int` `res = ``0``;  ` `    ``for` `(``int` `i = ``0``; i < n; i++) {  ` `        ``if` `(hashTable[arr[i]]!=``0``) {  ` `            ``for` `(``int` `j = i; j < n; j++)  ` `                ``if` `(arr[j] % arr[i] == ``0``)  ` `                    ``hashTable[arr[j]] = ``0``;  ` `            ``res++;  ` `        ``}  ` `    ``}  ` ` `  `    ``return` `res;  ` `}  ` ` `  `// Driver program  ` `public` `static` `void` `main(String args[])  ` `{  ` `    ``int` `arr[] = { ``4``, ``6``, ``2``, ``8``, ``7``, ``21``, ``24``, ``49``, ``44` `};  ` `    ``int` `n = arr.length;  ` ` `  `    ``System.out.print( minOperations(arr, n));  ` `  `  `}  ` `} ` `// This code is contributed by Arnab Kundu `

## Python 3

 `# Python 3 implementation of  ` `# the above approach ` `MAX` `=` `10000` ` `  `hashTable ``=` `[``0``] ``*` `MAX` ` `  `# function to find minimum operations ` `def` `minOperations(arr, n): ` `     `  `    ``# sort array ` `    ``arr.sort() ` ` `  `    ``# prepare hash of array ` `    ``for` `i ``in` `range``(n): ` `        ``hashTable[arr[i]] ``+``=` `1` ` `  `    ``res ``=` `0` `    ``for` `i ``in` `range``(n) : ` `        ``if` `(hashTable[arr[i]]) : ` `            ``for` `j ``in` `range``(i, n): ` `                ``if` `(arr[j] ``%` `arr[i] ``=``=` `0``): ` `                    ``hashTable[arr[j]] ``=` `0` `            ``res ``+``=` `1` ` `  `    ``return` `res ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``arr ``=` `[ ``4``, ``6``, ``2``, ``8``, ``7``, ``21``, ``24``, ``49``, ``44` `] ` `    ``n ``=` `len``(arr) ` ` `  `    ``print``(minOperations(arr, n)) ` ` `  `# This code is contributed  ` `# by ChitraNayal `

## C#

 `using` `System; ` ` `  `// C# implementation of the above approach   ` `public` `class` `Solution ` `{ ` `public` `const` `int` `MAX = 10000; ` ` `  `public` `static` `int``[] hashTable = ``new` `int``[MAX]; ` ` `  `// function to find minimum operations   ` `public` `static` `int` `minOperations(``int``[] arr, ``int` `n) ` `{ ` `    ``// sort array   ` `    ``Array.Sort(arr); ` ` `  `    ``// prepare hash of array   ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``hashTable[arr[i]]++; ` `    ``} ` ` `  `    ``int` `res = 0; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``if` `(hashTable[arr[i]] != 0) ` `        ``{ ` `            ``for` `(``int` `j = i; j < n; j++) ` `            ``{ ` `                ``if` `(arr[j] % arr[i] == 0) ` `                ``{ ` `                    ``hashTable[arr[j]] = 0; ` `                ``} ` `            ``} ` `            ``res++; ` `        ``} ` `    ``} ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver program   ` `public` `static` `void` `Main(``string``[] args) ` `{ ` `    ``int``[] arr = ``new` `int``[] {4, 6, 2, 8, 7, 21, 24, 49, 44}; ` `    ``int` `n = arr.Length; ` ` `  `    ``Console.Write(minOperations(arr, n)); ` ` `  `} ` `} ` ` `  `// This code is contributed by Shrikant13 `

## PHP

 ` `

Output:

```2
```

My Personal Notes arrow_drop_up Discovering ways to develop a plane for soaring career goals

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.