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
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 find the multiple of element in complete vector.
- For each element which are divisible by choose element mark it 0, and decrease the value of N.
- Return the value of N.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int N, vector< int > A)
{
sort(A.begin(), A.end());
int a = 0;
for ( int i = 0; i < A.size() - 1; i++) {
int c = 0;
if (A[i] != 0) {
for ( int j = i + 1; j < A.size(); j++) {
if (A[j] % A[i] == 0 && A[j] != 0
&& A[i] != 0) {
A[j] = 0;
N--;
}
}
}
}
if (N == 0) {
N = 1;
}
return N;
}
int main()
{
vector< int > v = { 4, 6, 2, 8, 7, 21, 24, 49, 44 };
int n = v.size();
cout << solve(n, v);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int solve( int N, int [] A)
{
Arrays.sort(A);
int a = 0 ;
for ( int i = 0 ; i < A.length - 1 ; i++) {
int c = 0 ;
if (A[i] != 0 ) {
for ( int j = i + 1 ; j < A.length; j++) {
if (A[j] % A[i] == 0 && A[j] != 0
&& A[i] != 0 ) {
A[j] = 0 ;
N--;
}
}
}
}
if (N == 0 ) {
N = 1 ;
}
return N;
}
public static void main(String args[])
{
int [] v = { 4 , 6 , 2 , 8 , 7 , 21 , 24 , 49 , 44 };
int n = v.length;
System.out.println(solve(n, v));
}
}
|
Python3
def solve(N,A):
A.sort()
a = 0
for i in range ( len (A) - 1 ):
c = 0
if (A[i] ! = 0 ):
for j in range (i + 1 , len (A)):
if (A[j] % A[i] = = 0 and A[j] ! = 0 and A[i] ! = 0 ):
A[j] = 0
N - = 1
if (N = = 0 ):
N = 1
return N
v = [ 4 , 6 , 2 , 8 , 7 , 21 , 24 , 49 , 44 ]
n = len (v)
print (solve(n, v))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int solve( int N, int [] A)
{
Array.Sort(A);
for ( int i = 0; i < A.Length - 1; i++) {
if (A[i] != 0) {
for ( int j = i + 1; j < A.Length; j++) {
if (A[j] % A[i] == 0 && A[j] != 0
&& A[i] != 0) {
A[j] = 0;
N--;
}
}
}
}
if (N == 0) {
N = 1;
}
return N;
}
public static void Main( string [] args)
{
int [] v = { 4, 6, 2, 8, 7, 21, 24, 49, 44 };
int n = v.Length;
Console.WriteLine(solve(n, v));
}
}
|
Javascript
<script>
let n = -1;
function solve(A)
{
A.sort((a,b)=> a - b);
let a = 0;
for (let i = 0; i < A.length - 1; i++) {
let c = 0;
if (A[i] != 0) {
for (let j = i + 1; j < A.length; j++) {
if (A[j] % A[i] == 0 && A[j] != 0 && A[i] != 0) {
A[j] = 0;
n--;
}
}
}
}
if (n == 0) {
n = 1;
}
return n;
}
let v = [ 4, 6, 2, 8, 7, 21, 24, 49, 44 ];
n = v.length;
document.write(solve(v));
</script>
|
Time Complexity: O(N2 logN), where N is the size of the array
Space Complexity: O(1)
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 implementation of the above approach
C++
#include <bits/stdc++.h>
#define MAX 10000
using namespace std;
int hashTable[MAX];
int minOperations( int arr[], int n)
{
sort(arr, arr + n);
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;
}
int main()
{
int arr[] = { 4, 6, 2, 8, 7, 21, 24, 49, 44 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minOperations(arr, n);
return 0;
}
|
Java
import java.util.*;
class Solution
{
static final int MAX= 10000 ;
static int hashTable[]= new int [MAX];
static int minOperations( int arr[], int n)
{
Arrays.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[] = { 4 , 6 , 2 , 8 , 7 , 21 , 24 , 49 , 44 };
int n = arr.length;
System.out.print( minOperations(arr, n));
}
}
|
Python 3
MAX = 10000
hashTable = [ 0 ] * MAX
def minOperations(arr, n):
arr.sort()
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
if __name__ = = "__main__" :
arr = [ 4 , 6 , 2 , 8 , 7 , 21 , 24 , 49 , 44 ]
n = len (arr)
print (minOperations(arr, n))
|
C#
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));
}
}
|
PHP
<?php
function minOperations(& $arr , $n )
{
$hashTable = array ();
sort( $arr );
for ( $i = 0; $i < $n ; $i ++)
$hashTable [ $arr [ $i ]]++;
$res = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $hashTable [ $arr [ $i ]])
{
for ( $j = $i ; $j < $n ; $j ++)
if ( $arr [ $j ] % $arr [ $i ] == 0)
$hashTable [ $arr [ $j ]] = 0;
$res ++;
}
}
return $res ;
}
$arr = array (4, 6, 2, 8, 7, 21,
24, 49, 44);
$n = sizeof( $arr );
echo minOperations( $arr , $n );
?>
|
Javascript
<script>
var MAX = 10000;
var hashTable = Array(MAX).fill(0);
function minOperations(arr, n)
{
arr.sort((a,b)=>a-b);
for ( var i = 0; i < n; i++)
hashTable[arr[i]]++;
var res = 0;
for ( var i = 0; i < n; i++) {
if (hashTable[arr[i]]) {
for ( var j = i; j < n; j++)
if (arr[j] % arr[i] == 0)
hashTable[arr[j]] = 0;
res++;
}
}
return res;
}
var arr = [ 4, 6, 2, 8, 7, 21, 24, 49, 44 ];
var n = arr.length;
document.write( minOperations(arr, n));
</script>
|
Time Complexity: O(N logN), where N is the size of the array
Space Complexity: O(MAX)
Please Login to comment...