
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Minimum number of elements that should be removed to make the array good using C++.
Problem statement
Given an array “arr”, the task is to find the minimum number of elements to be removed to make the array good.
A sequence a1, a2, a3. . .an is called good if for each element a[i], there exists an element a[j] (i not equals to j) such that a[i] + a[j] is a power of two.
arr1[] = {1, 1, 7, 1, 5}
In above array if we delete element ‘5’ then array becomes good array. After this any pair of arr[i] + arr[j] is power of two −
- arr[0] + arr[1] = (1 + 1) = 2 which power of two
- arr[0] + arr[2] = (1 + 7) = 8 which is power of two
Algorithm
1. We have to delete only such a[i] for which there is no a[j] such that a[i] + a[i] is a power of 2. 2. For each value find the number of its occurrences in the array 3. Check that a[i] doesn’t have a pair a[j]
Example
#include <iostream> #include <map> #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; int minDeleteRequred(int *arr, int n){ map<int, int> frequency; for (int i = 0; i < n; ++i) { frequency[arr[i]]++; } int delCnt = 0; for (int i = 0; i < n; ++i) { bool doNotRemove = false; for (int j = 0; j < 31; ++j) { int pair = (1 << j) - arr[i]; if (frequency.count(pair) && (frequency[pair] > 1 || (frequency[pair] == 1 && pair != arr[i]))) { doNotRemove = true; break; } } if (!doNotRemove) { ++delCnt; } } return delCnt; } int main(){ int arr[] = {1, 1, 7, 1, 5}; cout << "Minimum elements to be deleted = " << minDeleteRequred(arr, SIZE(arr)) << endl; return 0; }
Output
When you compile and execute above program. It generates following output −
Minimum elements to be deleted = 1
- Related Articles
- Minimum number of elements to be removed to make XOR maximum using C++.
- Minimum number of operations on an array to make all elements 0 using C++.
- Minimum number of moves to make all elements equal using C++.
- Minimum number of elements to add to make median equals x using C++.
- C++ Program to count number of characters to be removed to get good string
- Minimum number of operations required to delete all elements of the array using C++.
- Minimum number of points to be removed to get remaining points on one side of axis using C++.
- Number of digits to be removed to make a number divisible by 3 in C++
- Program to find minimum number of intervals to be removed to remove overlaps in C++
- Minimum delete operations to make all elements of array same in C++.
- Minimum operations required to make all the array elements equal in C++
- Program to find number of elements can be removed to make odd and even indexed elements sum equal in Python
- Minimum operation to make all elements equal in array in C++
- Minimum steps to make all the elements of the array divisible by 4 in C++
- Minimum number of items to be delivered using C++.

Advertisements