GFG App
Open App
Browser
Continue

# Maximize frequency of an element by adding X to a Subsequence

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++

```// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;

void find_most_duplicates(vector<int>& A)
{
int N = A.size();
vector<pair<int, int> > freq; // { freq, element }

// we will use this map to find the frequency of
// elements.
unordered_map<int, int> mp; // { element, freq }

// mapping elements
for (int i = 0; i < A.size(); i++) {
mp[A[i]]++;
}

// we need to convert the pair from map to pair we need
// in frequency array, to do this we reverse the pair.
for (auto i : mp) {
freq.push_back({ i.second, i.first });
}
int max_freq = 0, X = 0, m = freq.size();
if (m == 1) {
// this case is edge case when all are already same.
max_freq = freq.back().first;
X = 0;
}
else if (m == 2) {
max_freq = freq[0].first + freq[1].first;

// we are taking absolute because we don't know the
// maximum frequency element is actually greater in
// magnitude
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);
}

// print the result.
cout << "X = " << X
<< ", Maximum Frequency Possible =  " << max_freq
<< endl;
}

// Driver Code
int main()
{
// Starting array
vector<int> A{ 2, 1, 3, 3, 4, 5 };
find_most_duplicates(A);
return 0;
}```

## Java

```// Java code to implement the above approach
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<>(); // { freq, element }

// we will use this map to find the frequency of
// elements.
HashMap<Integer, Integer> mp
= new HashMap<>(); // { element, freq }

// mapping elements
for (int i = 0; i < N; i++) {
mp.put(A[i], mp.getOrDefault(A[i], 0) + 1);
}

// we need to convert the pair from map to pair we
// need in frequency array, to do this we reverse
// the pair.
for (Map.Entry<Integer, Integer> i :
mp.entrySet()) {
}
int max_freq = 0, X = 0, m = freq.size();
if (m == 1)
{

// this case is edge case when all are already
// same.
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;

// we are taking absolute because we don't know
// the
// maximum frequency element is actually greater
// in magnitude
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)
{

// swap(first_greatest, second_greatest)
int temp_int = first_greatest;
first_greatest = second_greatest;
second_greatest = temp_int;

// swap(first_greatest_index,
// second_greatest_index)
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);
}

// print the result.
System.out.println(
"X = " + X + ", Maximum Frequency Possible =  "
+ max_freq);
}

public static void main(String[] args)
{
// Starting array
int[] A = { 2, 1, 3, 3, 4, 5 };
find_most_duplicates(A);
}
}

// This code is contributed by lokesh```

## Python3

```# Python code to implement the above approach
def find_most_duplicates(A):
N = len(A)
freq = []  # { freq, element }

# we will use this map to find the frequency of
# elements.
mp = {}  # { element, freq }

# mapping elements
for i in range(N):
if A[i] not in mp:
mp[A[i]] = 1
else:
mp[A[i]] += 1

# we need to convert the pair from
# map to pair we need in frequency array,
# to do this we reverse the pair.
for i in mp:
freq.append([mp[i], i])

max_freq = 0
X = 0
m = len(freq)

if m == 1:
# this case is edge case when all are already same.
max_freq = freq[m-1][0]
X = 0
elif m == 2:
max_freq = freq[0][0] + freq[1][0]

# we are taking absolute because
# we don't know the maximum frequency
# element is actually greater in magnitude.
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 the result.
print("X = ", X, ", Maximum Frequency Possible=", max_freq)

# Driver Code
if __name__ == '__main__':

# Starting array
A = [2, 1, 3, 3, 4, 5]
find_most_duplicates(A)

# This code is contributed by Tapesh(tapeshdua420)```

## 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> >(); // { freq,
// element }

// we will use this map to find the frequency of
// elements.
Dictionary<int, int> mp
= new Dictionary<int,
int>(); // { element, freq }

for (int i = 0; i < 20; i++) {
}

// mapping elements
for (int i = 0; i < A.Count; i++) {
mp[A[i]] = mp[A[i]] + 1;
}

// we need to convert the pair from map to pair we
// need in frequency array, to do this we reverse
// the pair.
foreach(KeyValuePair<int, int> entry in mp)
{
Pair<int, int> pair = new Pair<int, int>(
entry.Value, entry.Key);
}
int max_freq = 0, X = 0, m = freq.Count;
if (m == 1) {
// this case is edge case when all are already
// same.
max_freq = freq.Last().First;
X = 0;
}
else if (m == 2) {
max_freq = freq[0].First + freq[1].First;

// we are taking absolute because we don't know
// the maximum frequency element is actually
// greater in magnitude
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);
}

// print the result.
Console.Write("X = ");
Console.Write(X);
Console.Write(", Maximum Frequency Possible =  ");
Console.Write(max_freq);
Console.WriteLine("");
}

// Driver Code
static public void Main()
{
// Starting array
List<int> A = new List<int>();
find_most_duplicates(A);
}
}

// This code is contributed by akashish__```

## Javascript

```        // JavaScript code to implement the above approach

const find_most_duplicates = (A) => {
let N = A.length;
let freq = []; // { freq, element }

// we will use this map to find the frequency of
// elements.
let mp = {}; // { element, freq }

// mapping elements
for (let i = 0; i < A.length; i++) {
mp[A[i]] = A[i] in mp ? mp[A[i]] + 1 : 1;
}

// we need to convert the pair from map to pair we need
// in frequency array, to do this we reverse the pair.
for (let i in mp) {
freq.push([mp[i], i]);
}
let max_freq = 0, X = 0, m = freq.length;
if (m == 1) {
// this case is edge case when all are already same.
max_freq = freq[freq.length - 1][0];
X = 0;
}
else if (m == 2) {
max_freq = freq[0][0] + freq[1][0];

// we are taking absolute because we don't know the
// maximum frequency element is actually greater in
// magnitude
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]);
}

// print the result.
console.log(`X =  \${X}, Maximum Frequency Possible = \${max_freq}<br/>`);
}

// Driver Code

// Starting array
let A = [2, 1, 3, 3, 4, 5];
find_most_duplicates(A);

// This code is contributed by rakeshsahni.```
Output

`X = 2, Maximum Frequency Possible =  3`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up