Given an array A[] of size N, the task is to maximize the frequency of any element by adding any positive integer X to all elements of any subsequence of that array. Print the maximum frequency that can be achieved and the value of X. If X has more than one value return any of it.
Example:
Input: A[] = {2, 1, 3, 3, 4, 5} Output: X = 2, Maximum Frequency Possible = 3 Explanation: Convert 1 to 3 to get A[] = {3, 1, 3, 3, 4, 5}, Here maximum frequency = 3, and we added 2 to each element of the subsequence {1}.
Input: A[] = {5, 6, 4, 5, 5, 4, 7} Output: X = 1, Maximum Frequency Possible = 5
Approach:
The idea behind this problem is to take the subsequence from array containing only the second most frequent element in the array and convert all the elements to the most frequent element in the array.
Follow the steps to solve this problem:
- Find and store the frequency of each element in the array using an unordered_map.
- From the frequency map, find the elements with the highest and second highest frequency in the array.
- X = absolute difference between the most frequent and second most frequent element.
- Maximum Frequency Possible would be the sum of the highest and second highest frequency.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
void find_most_duplicates(vector< int >& A)
{
int N = A.size();
vector<pair< int , int > > freq;
unordered_map< int , int > mp;
for ( int i = 0; i < A.size(); i++) {
mp[A[i]]++;
}
for ( auto i : mp) {
freq.push_back({ i.second, i.first });
}
int max_freq = 0, X = 0, m = freq.size();
if (m == 1) {
max_freq = freq.back().first;
X = 0;
}
else if (m == 2) {
max_freq = freq[0].first + freq[1].first;
X = abs (freq[0].second - freq[1].second);
}
else {
int first_greatest = 0, second_greatest = 0;
int first_greatest_index = -1,
second_greatest_index = -1;
for ( int i = 0; i < m; i++) {
if (freq[i].first > first_greatest) {
swap(first_greatest, second_greatest);
swap(first_greatest_index,
second_greatest_index);
first_greatest = freq[i].first;
first_greatest_index = i;
}
else if (freq[i].first > second_greatest) {
second_greatest = freq[i].first;
second_greatest_index = i;
}
}
max_freq = first_greatest + second_greatest;
X = abs (freq[first_greatest_index].second
- freq[second_greatest_index].second);
}
cout << "X = " << X
<< ", Maximum Frequency Possible = " << max_freq
<< endl;
}
int main()
{
vector< int > A{ 2, 1, 3, 3, 4, 5 };
find_most_duplicates(A);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class pair {
int first, second;
pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
class GFG {
static void find_most_duplicates( int [] A)
{
int N = A.length;
List<pair> freq
= new ArrayList<>();
HashMap<Integer, Integer> mp
= new HashMap<>();
for ( int i = 0 ; i < N; i++) {
mp.put(A[i], mp.getOrDefault(A[i], 0 ) + 1 );
}
for (Map.Entry<Integer, Integer> i :
mp.entrySet()) {
freq.add( new pair(i.getValue(), i.getKey()));
}
int max_freq = 0 , X = 0 , m = freq.size();
if (m == 1 )
{
pair temp = freq.get(freq.size() - 1 );
max_freq = temp.first;
X = 0 ;
}
else if (m == 2 ) {
pair temp0 = freq.get( 0 );
pair temp1 = freq.get( 1 );
max_freq = temp0.first + temp1.first;
X = Math.abs(temp0.second - temp1.second);
}
else {
int first_greatest = 0 , second_greatest = 0 ;
int first_greatest_index = - 1 ,
second_greatest_index = - 1 ;
for ( int i = 0 ; i < m; i++) {
pair temp = freq.get(i);
if (temp.first > first_greatest)
{
int temp_int = first_greatest;
first_greatest = second_greatest;
second_greatest = temp_int;
temp_int = first_greatest_index;
first_greatest_index
= second_greatest_index;
second_greatest_index = temp_int;
first_greatest = temp.first;
first_greatest_index = i;
}
else if (temp.first > second_greatest) {
second_greatest = temp.first;
second_greatest_index = i;
}
}
max_freq = first_greatest + second_greatest;
pair temp1 = freq.get(first_greatest_index);
pair temp2 = freq.get(second_greatest_index);
X = Math.abs(temp1.second - temp2.second);
}
System.out.println(
"X = " + X + ", Maximum Frequency Possible = "
+ max_freq);
}
public static void main(String[] args)
{
int [] A = { 2 , 1 , 3 , 3 , 4 , 5 };
find_most_duplicates(A);
}
}
|
Python3
def find_most_duplicates(A):
N = len (A)
freq = []
mp = {}
for i in range (N):
if A[i] not in mp:
mp[A[i]] = 1
else :
mp[A[i]] + = 1
for i in mp:
freq.append([mp[i], i])
max_freq = 0
X = 0
m = len (freq)
if m = = 1 :
max_freq = freq[m - 1 ][ 0 ]
X = 0
elif m = = 2 :
max_freq = freq[ 0 ][ 0 ] + freq[ 1 ][ 0 ]
X = abs (frq[ 0 ][ 1 ] - freq[ 1 ][ 1 ])
else :
first_greatest = 0
second_greatest = 0
first_greatest_index = - 1
second_greatest_index = - 1
for i in range (m):
if freq[i][ 0 ] > first_greatest:
first_greatest, second_greatest = second_greatest, first_greatest
first_greatest_index, second_greatest_index = second_greatest_index, first_greatest_index
first_greatest = freq[i][ 0 ]
first_greatest_index = i
elif freq[i][ 0 ] > second_greatest:
second_greatest = freq[i][ 0 ]
second_greatest_index = i
max_freq = first_greatest + second_greatest
X = abs (freq[first_greatest][ 1 ] - freq[second_greatest][ 1 ])
print ( "X = " , X, ", Maximum Frequency Possible=" , max_freq)
if __name__ = = '__main__' :
A = [ 2 , 1 , 3 , 3 , 4 , 5 ]
find_most_duplicates(A)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class Pair<T, U> {
public Pair() {}
public Pair(T first, U second)
{
this .First = first;
this .Second = second;
}
public T First
{
get ;
set ;
}
public U Second
{
get ;
set ;
}
};
public class GFG {
static void swap( ref int x, ref int y)
{
int tempswap = x;
x = y;
y = tempswap;
}
public static void find_most_duplicates(List< int > A)
{
int N = A.Count;
List<Pair< int , int > > freq
= new List<Pair< int , int > >();
Dictionary< int , int > mp
= new Dictionary< int ,
int >();
for ( int i = 0; i < 20; i++) {
mp.Add(i, 0);
}
for ( int i = 0; i < A.Count; i++) {
mp[A[i]] = mp[A[i]] + 1;
}
foreach (KeyValuePair< int , int > entry in mp)
{
Pair< int , int > pair = new Pair< int , int >(
entry.Value, entry.Key);
freq.Add(pair);
}
int max_freq = 0, X = 0, m = freq.Count;
if (m == 1) {
max_freq = freq.Last().First;
X = 0;
}
else if (m == 2) {
max_freq = freq[0].First + freq[1].First;
X = Math.Abs(freq[0].Second - freq[1].Second);
}
else {
int first_greatest = 0, second_greatest = 0;
int first_greatest_index = -1,
second_greatest_index = -1;
for ( int i = 0; i < m; i++) {
if (freq[i].First > first_greatest) {
swap( ref first_greatest,
ref second_greatest);
swap( ref first_greatest_index,
ref second_greatest_index);
first_greatest = freq[i].First;
first_greatest_index = i;
}
else if (freq[i].First > second_greatest) {
second_greatest = freq[i].First;
second_greatest_index = i;
}
}
max_freq = first_greatest + second_greatest;
X = Math.Abs(
freq[first_greatest_index].Second
- freq[second_greatest_index].Second);
}
Console.Write( "X = " );
Console.Write(X);
Console.Write( ", Maximum Frequency Possible = " );
Console.Write(max_freq);
Console.WriteLine( "" );
}
static public void Main()
{
List< int > A = new List< int >();
A.Add(2);
A.Add(1);
A.Add(3);
A.Add(3);
A.Add(4);
A.Add(5);
find_most_duplicates(A);
}
}
|
Javascript
const find_most_duplicates = (A) => {
let N = A.length;
let freq = [];
let mp = {};
for (let i = 0; i < A.length; i++) {
mp[A[i]] = A[i] in mp ? mp[A[i]] + 1 : 1;
}
for (let i in mp) {
freq.push([mp[i], i]);
}
let max_freq = 0, X = 0, m = freq.length;
if (m == 1) {
max_freq = freq[freq.length - 1][0];
X = 0;
}
else if (m == 2) {
max_freq = freq[0][0] + freq[1][0];
X = Math.abs(freq[0][1] - freq[1][1]);
}
else {
let first_greatest = 0, second_greatest = 0;
let first_greatest_index = -1,
second_greatest_index = -1;
for (let i = 0; i < m; i++) {
if (freq[i][0] > first_greatest) {
let temp = first_greatest;
first_greatest = second_greatest;
second_greatest = temp;
temp = first_greatest_index;
first_greatest_index = second_greatest_index;
second_greatest_index = temp;
first_greatest = freq[i][0];
first_greatest_index = i;
}
else if (freq[i][0] > second_greatest) {
second_greatest = freq[i][0];
second_greatest_index = i;
}
}
max_freq = first_greatest + second_greatest;
X = Math.abs(freq[first_greatest_index][1]
- freq[second_greatest_index][1]);
}
console.log(`X = ${X}, Maximum Frequency Possible = ${max_freq}<br/>`);
}
let A = [2, 1, 3, 3, 4, 5];
find_most_duplicates(A);
|
Output
X = 2, Maximum Frequency Possible = 3
Time Complexity: O(N) Auxiliary Space: O(N)
|