Maximize frequency sum of K chosen characters from given string
Last Updated :
07 Oct, 2022
Given a positive integer K and a string S consisting of N characters, the task is to maximize the sum of the frequencies of exactly K chosen characters from the given string.
Examples:
Input: K – 3, S = ”GEEKSFORGEEKS”
Output: 12
Explanation:
Choose the character E from the given string S, the sum of frequency of 3 E’s is 3*4 = 12, which is the maximum.
Input: K = 10, S = ”GEEKSFORGEEKS”
Output: 28
Approach: The given problem can be solved by using the Greedy Approach by choosing those K characters having the higher frequencies. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximumSum(string S, int N, int K)
{
int sum = 0;
int freq[256] = { 0 };
for ( int i = 0; i < N; i++) {
freq[ int (S[i])]++;
}
sort(freq, freq + 256, greater< int >());
for ( int i = 0; i < 256; i++) {
if (K > freq[i]) {
sum += freq[i] * freq[i];
K -= freq[i];
}
else {
sum += freq[i] * K;
break ;
}
}
return sum;
}
int main()
{
string S = "GEEKSFORGEEKS" ;
int K = 10;
int N = S.length();
cout << maximumSum(S, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int maximumSum(String S, int N, int K)
{
int sum = 0 ;
Integer []freq = new Integer[ 256 ];
Arrays.fill(freq, 0 );
for ( int i = 0 ; i < N; i++)
{
freq[( int )S.charAt(i)] += 1 ;
}
Arrays.sort(freq, Collections.reverseOrder());
for ( int i = 0 ; i < 256 ; i++)
{
if (K > freq[i])
{
sum += freq[i] * freq[i];
K -= freq[i];
}
else
{
sum += freq[i] * K;
break ;
}
}
return sum;
}
public static void main(String args[])
{
String S = "GEEKSFORGEEKS" ;
int K = 10 ;
int N = S.length();
System.out.print(maximumSum(S, N, K));
}
}
|
Python3
def maximumSum(S, N, K):
sum = 0
freq = [ 0 ] * 256
for i in range (N):
freq[ ord (S[i])] + = 1
freq = sorted (freq)[:: - 1 ]
for i in range ( 256 ):
if (K > freq[i]):
sum + = freq[i] * freq[i]
K - = freq[i]
else :
sum + = freq[i] * K
break
return sum
if __name__ = = '__main__' :
S = "GEEKSFORGEEKS"
K = 10
N = len (S)
print (maximumSum(S, N, K))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maximumSum( string S, int N, int K)
{
int sum = 0;
int []freq = new int [256];
Array.Clear(freq, 0, 256);
for ( int i = 0; i < N; i++)
{
freq[( int )S[i]]++;
}
Array.Sort(freq);
Array.Reverse(freq);
for ( int i = 0; i < 256; i++)
{
if (K > freq[i])
{
sum += freq[i] * freq[i];
K -= freq[i];
}
else
{
sum += freq[i] * K;
break ;
}
}
return sum;
}
public static void Main()
{
string S = "GEEKSFORGEEKS" ;
int K = 10;
int N = S.Length;
Console.Write(maximumSum(S, N, K));
}
}
|
Javascript
<script>
function maximumSum(S, N, K)
{
let sum = 0;
let freq = Array.from({length: 256}, (_, i) => 0);
for (let i = 0; i < N; i++)
{
freq[S[i].charCodeAt()]++;
}
freq.sort((a, b) => b - a);
for (let i = 0; i < 256; i++)
{
if (K > freq[i])
{
sum += freq[i] * freq[i];
K -= freq[i];
}
else
{
sum += freq[i] * K;
break ;
}
}
return sum;
}
let S = "GEEKSFORGEEKS" ;
let K = 10;
let N = S.length;
document.write(maximumSum(S.split( '' ), N, K))
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(256)
Share your thoughts in the comments
Please Login to comment...