

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimum number of operations required to delete all elements of the array using C++.
Problem statement
Given an integer array arr, the task is to print the minimum number of operations required to delete all elements of the array. While deleting element following restriction is imposed −
Any element from the array can be chosen at random and every element divisible by it can be removed from the array
If arr[] = {2, 4, 15, 10, 8, 5, 3} then 3 operation are required to delete all elements −
- If we choose 2 then it will delete {2, 4, 10, 8}
- If we choose 5 then it will remove {5, 15}
- If we choose 3 then it will remove {3}
Algorithm
1. Sort the array in ascending order and count number occurrence of each element 2. For each unmarked element starting from beginning mark all elements which are divisible by choose element, and increase the result counter
Example
#include <iostream> #include <algorithm> #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) #define MAX 100 using namespace std; int getMinOperations(int *arr, int n){ int map[MAX] = {0}; sort(arr, arr + n); for (int i = 0; i < n; ++i) { map[arr[i]]++; } int cnt = 0; for (int i = 0; i < n; ++i) { if (map[arr[i]]) { for (int j = i; j < n; ++j) { if (arr[j] % arr[i] == 0) { map[arr[j]] = 0; } } ++cnt; } } return cnt; } int main(){ int arr[] = {2, 4, 15, 10, 8, 5, 3}; cout << "Minimum required operations = " << getMinOperations(arr, SIZE(arr)) << endl; return 0; }
Output
When you compile and execute the above program. It generates the following output −
Minimum required operations = 3
- Related Questions & Answers
- Minimum delete operations to make all elements of array same in C++.
- Number of operations required to make all array elements Equal in Python
- Minimum operations required to make all the array elements equal in C++
- Find the number of operations required to make all array elements Equal in C++
- Minimum number of operations on an array to make all elements 0 using C++.
- Minimum operations required to set all elements of binary matrix in C++
- Minimum number of operations required to sum to binary string S using C++.
- Minimum number of mails required to distribute all the questions using C++.
- Minimum number of given operations required to make two strings equal using C++.
- Minimum operations required to remove an array in C++
- C++ Program to find out the minimum number of operations required to defeat an enemy
- Finding minimum number of required operations to reach n from m in JavaScript
- Minimum number of moves to make all elements equal using C++.
- How to find the minimum number of jumps required to reach the end of the array using C#?
- Program to find minimum number of operations required to make one number to another in Python
Advertisements