

- 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 points to be removed to get remaining points on one side of axis using C++.
Problem statement
We are given N points in a Cartesian plane. Our task is to find the minimum number of points that should be removed in order to get the remaining points on one side of any axis.
If given input is {(10, 5), (-2, -5), (13, 8), (-14, 7)} then if we remove (-2, -5) then all remaining points are above X-axis.
Hence answer is 1.
Algorithm
1. Finds the number of points on all sides of the X-axis and Y-axis 2. Return minimum from both of them
Example
#include <iostream> #include <algorithm> #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; struct point{ int x, y; }; int minPointsToBeRemoved(point arr[], int n){ int a = 0, b = 0, c = 0, d = 0; for (int i = 0; i < n; i++){ if (arr[i].x >= 0) b++; else if (arr[i].x <= 0) a++; if (arr[i].y <= 0) d++; else if (arr[i].y >= 0) c++; } return min({a, d, c, b}); } int main(){ point arr[] = {{10, 5}, {-2, -5}, {13, 8}, {-14, 7}}; cout << "Minimum points to be removed = " << minPointsToBeRemoved(arr, SIZE(arr)) << endl; return 0; }
Output
When you compile and execute the above program. It generates the following output −
Minimum points to be removed = 1
- Related Questions & Answers
- Minimum number of elements to be removed to make XOR maximum using C++.
- Number of Integral Points between Two Points in C++
- Minimum number of elements that should be removed to make the array good using C++.
- Program to find minimum number of intervals to be removed to remove overlaps in C++
- C++ Program to count number of characters to be removed to get good string
- Minimum number of items to be delivered using C++.
- Find number of endless points in C++
- Find the number of boxes to be removed in C++
- Plot scatter points on polar axis in Matplotlib
- Minimum Initial Points to Reach Destination
- How to get the center of a set of points using Python?
- Find the Number of Triangles Formed from a Set of Points on Three Lines using C++\n
- Minimum Time Visiting All Points in C++
- C++ Program to Find Number of Articulation points in a Graph
- Program to count number of points that lie on a line in Python
Advertisements