Maximum number of distinct points on a 2D plane
Last Updated :
23 Feb, 2023
Given an array X[] of length N where X[i] ? 2 (1 ? i ? N). Divide each element of X[] into two integers such that the sum of those two integers is equal to the divided element of X[], Formally X[i] = (a + b) for all (1 ? i ? N), Where (a, b > 0), a and b can be duplicate as well. Mark (a, b) as a point in the 2D plane, the task is to find the maximum number of distinct points on a 2D plane that can be created.
Examples:
Input: N = 4, X[] = {2, 3, 4, 5}
Output: 4
Explanation: Optimal Division of elements takes place as:
- X[1] = 2, divided into (1, 1)
- X[2] = 3, divided into (1, 2)
- X[3] = 4, divided into (1, 3)
- X[4] = 5, divided into (1, 4)
Total 4 distinct points (1, 1), (1, 2), (1, 3) and (1, 4) are on a 2D plane. Which is the maximum possible for this case.
Input: N = 6, X[] = {2, 2, 4, 4, 2, 6}
Output: 4
Explanation: Optimal Division of elements takes place as:
- X[1] = 2, divided into (1, 1)
- X[2] = 2, divided into (1, 1)
- X[3] = 4, divided into (3, 1)
- X[4] = 4, divided into (1, 3)
- X[5] = 2, divided into (1, 1)
- X[6] = 6, divided into (3, 2)
Total 4 distinct points (1, 1), (1, 3), (3, 1), and (3, 2) are on a 2D plane. Which is the maximum possible for this case.
Approach: Implement the idea below to solve the problem:
The problem is observation based and can be solved by using HashMap and counting the frequency of elements present in X[]. The concept is first to initialize HashMap with the frequency of elements present in X[]. Then make a Count variable and initialize it to 0, Traverse over HashMap and then increment the count by min(element – 1, frequency) in each iteration.
Steps were taken to solve the problem:
- Declare the Hashmap of <Integer, Integer> type.
- Initialize the map with the frequency of elements in X[].
- Create a Count variable and initialize it to 0.
- Traverse on HashMap using loop and increment count by min(element-1, frequency) in each operation.
Below is the code to implement the approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long Max_Points( int n, int x[])
{
map < int , int > mp;
for ( int i = 0; i < n; i++) {
mp[x[i]]++;
}
long long count = 0;
for ( auto i : mp){
int element = i.first;
int value = i.second;
count += min(element - 1, value);
}
return count;
}
int main() {
int N = 6;
int X[] = { 2, 2, 4, 4, 2, 6 };
cout << Max_Points(N, X) << endl;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
throws java.lang.Exception
{
int N = 6 ;
int [] X = { 2 , 2 , 4 , 4 , 2 , 6 };
System.out.println(Max_Points(N, X));
}
static long Max_Points( int n, int [] X)
{
HashMap<Integer, Integer> map = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
int d = X[i];
map.put(d, map.get(d) == null ? 1
: map.get(d) + 1 );
}
long count = 0 ;
for (Map.Entry<Integer, Integer> set :
map.entrySet()) {
count += Math.min(set.getKey() - 1 ,
set.getValue());
}
return count;
}
}
|
C#
using System;
using System.Collections.Generic;
class GFG {
static public void Main ()
{
int N = 6;
int [] X = { 2, 2, 4, 4, 2, 6 };
Console.WriteLine(Max_Points(N, X));
}
static long Max_Points( int n, int [] X)
{
Dictionary< int , int > map= new Dictionary< int , int >();
for ( int i = 0; i < n; i++) {
int d = X[i];
if (!map.ContainsKey(d))
{
map.Add(d,1);
}
else
map[d]+=1;
}
long count = 0;
foreach (KeyValuePair< int , int > set in map)
{
count += Math.Min( set .Key - 1, set .Value);
}
return count;
}
}
|
Python
def Max_Points(n, x):
mp = {}
for i in range (n):
if x[i] in mp:
mp[x[i]] + = 1
else :
mp[x[i]] = 1
count = 0
for element, value in mp.items():
count + = min (element - 1 , value)
return count
N = 6
X = [ 2 , 2 , 4 , 4 , 2 , 6 ]
print (Max_Points(N,X))
|
Javascript
function Max_Points(n, x)
{
let mp = new Map();
for (let i=0;i<n;i++) {
if (mp.has(x[i])) {
mp.set(x[i], mp.get(x[i]) + 1);
}
else {
mp.set(x[i], 1);
}
}
let count = 0;
for (let [key, value] of mp.entries())
{
count += Math.min(key - 1, value);
}
return count;
}
let N = 6;
let X = [ 2, 2, 4, 4, 2, 6 ];
document.write(Max_Points(N, X));
|
Time Complexity: O(N)
Auxiliary Space: O(N), as HashMap is used for counting frequencies.
Related Articles:
Share your thoughts in the comments
Please Login to comment...