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++ code to implement the approach #include <bits/stdc++.h> using namespace std;
// Function for returning maximum number
// of distinct points
long long Max_Points( int n, int x[])
{
// Map declared for counting frequency
map < int , int > mp;
// Loop for initializing Map with frequency
for ( int i = 0; i < n; i++) {
mp[x[i]]++;
}
// Variable to store maximum number
// of distinct points
long long count = 0;
// Loop for traversing over Map
for ( auto i : mp){
int element = i.first;
int value = i.second;
count += min(element - 1, value);
}
// Returning the value of count
return count;
}
int main() {
// Input value of N
int N = 6;
int X[] = { 2, 2, 4, 4, 2, 6 };
// Function call
cout << Max_Points(N, X) << endl;
return 0;
} |
// Java code to implement the approach import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Driver function
public static void main(String[] args)
throws java.lang.Exception
{
// Input value of N
int N = 6 ;
int [] X = { 2 , 2 , 4 , 4 , 2 , 6 };
// Function call
System.out.println(Max_Points(N, X));
}
// Function for returning maximum number
// of distinct points
static long Max_Points( int n, int [] X)
{
// HashMap declared for counting
// frequency
HashMap<Integer, Integer> map = new HashMap<>();
// Loop for initializing HashMap with
// frequency
for ( int i = 0 ; i < n; i++) {
int d = X[i];
map.put(d, map.get(d) == null ? 1
: map.get(d) + 1 );
}
// Variable to store maximum number
// of distinct points
long count = 0 ;
// Loop for traversing over HashMap
for (Map.Entry<Integer, Integer> set :
map.entrySet()) {
count += Math.min(set.getKey() - 1 ,
set.getValue());
}
// Returning the value of count
return count;
}
} |
// C# code to implement the approach using System;
using System.Collections.Generic;
class GFG {
// Driver function
static public void Main ()
{
// Input value of N
int N = 6;
int [] X = { 2, 2, 4, 4, 2, 6 };
// Function call
Console.WriteLine(Max_Points(N, X));
}
// Function for returning maximum number
// of distinct points
static long Max_Points( int n, int [] X)
{
// HashMap declared for counting
// frequency
Dictionary< int , int > map= new Dictionary< int , int >();
// Loop for initializing HashMap with
// frequency
for ( int i = 0; i < n; i++) {
int d = X[i];
if (!map.ContainsKey(d))
{
map.Add(d,1);
}
else
map[d]+=1;
}
// Variable to store maximum number
// of distinct points
long count = 0;
// Loop for traversing over HashMap
foreach (KeyValuePair< int , int > set in map)
{
count += Math.Min( set .Key - 1, set .Value);
}
// Returning the value of count
return count;
}
} // This code is contributed by Pushpesh Raj. |
def Max_Points(n, x):
# Map declared for counting frequency
mp = {}
# Loop for initializing Map with frequency
for i in range (n):
if x[i] in mp:
mp[x[i]] + = 1
else :
mp[x[i]] = 1
# Variable to store maximum number
# of distinct points
count = 0
# Loop for traversing over Map
for element, value in mp.items():
count + = min (element - 1 , value)
# Returning the value of count
return count
N = 6
X = [ 2 , 2 , 4 , 4 , 2 , 6 ]
print (Max_Points(N,X))
|
// JS code to implement the approach // Function for returning maximum number
// of distinct points
function Max_Points(n, x)
{
// Map declared for counting frequency
let mp = new Map();
// Loop for initializing Map with frequency
for (let i=0;i<n;i++) {
if (mp.has(x[i])) {
// If number is present in mp,
// incrementing it's count by 1
mp.set(x[i], mp.get(x[i]) + 1);
}
else {
// If integer is not present in mp,
// putting this integer to mp with 1 as it's value
mp.set(x[i], 1);
}
}
// Variable to store maximum number
// of distinct points
let count = 0;
// Loop for traversing over Map
for (let [key, value] of mp.entries())
{
count += Math.min(key - 1, value);
}
// Returning the value of count
return count;
} // Input value of N let N = 6; let X = [ 2, 2, 4, 4, 2, 6 ]; // Function call document.write(Max_Points(N, X)); |
4
Time Complexity: O(N)
Auxiliary Space: O(N), as HashMap is used for counting frequencies.
Related Articles: