Minimum number of operations required to delete all elements of the array | C # implementation of the above approach ; function to find minimum operations ; sort array ; prepare hash of array ; Driver program
using System ; public class Solution { public const int MAX = 10000 ; public static int [ ] hashTable = new int [ MAX ] ; public static int minOperations ( int [ ] arr , int n ) { Array . Sort ( arr ) ; 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 ; } 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 ) ) ; } }