Shweta is a college girl and she loves Candies. She wants to share some candies with her friend Radhika. She is having N candies such that the ith candy is of A[i] brand where 0 ≤ i ≤ N. Let us call this array as the brand array. She wants to share K candies with Radhika such that the K- sized subsequence of the candies has a minimum number of distinct brands among all possible subsequences of length K.
Formally you will have to help Shweta in determining the subsequence of length K in the given array which has a minimum number of distinct brands among all possible subsequences of length K.
You will have to print the total brands of candies Rahikha received.
1 ≤ K ≤ N ≤ 100000
1 ≤ A[i] ≤ 1000000000
10 6
1 3 2 4 3 4 5 3 4 3
2
One of the possible K-sized subsequences [3 4 3 4 3 4] with 2 distinct brands which is the minimum.
===================================================
Roy is playing with strings and he came up with a very weird question.
He gave you string str of size N having lower case English alphabets and an array of size N which contains N non-negative integer value for every character present in the string.
Your task is to delete characters from the string following the given instruction:
Delete exactly one occurrence of every character present in the string
for e.g. str=aabba and array is [2,3,1,2,3] so frequency of character “a"=3 and frequency of character "b"=2, let's say you deleted 0th index i.e. “a" and 2nd index i.e. "b" which results in frequency of character "a" = 2 and frequency of character "b" = 1 and updated array is [0,3,0,2,3].
Note: Deletion means updating the value of that character in the array as 0
You have to delete the characters in a manner that after all deletions, the sum of the array would be minimum.
Note: The value of every undeleted character will remain unchanged after any deletion, for eg
In the above-mentioned string after deleting 1st and 3rd character the value of 2nd, 4th, and 5th character is still unchanged i.e. 3,2,3
The first line contains a T-denoting number of test cases.
For every test case,
For every test case, output the minimum sum of the array that you get in a new line.
1 ≤ T ≤ 10
0 ≤ N ≤ 1e5
“a” ≤ str[i] ≤ “z” for 0 ≤ i < N
0 ≤ value[i] ≤ 1e9 for 0 ≤ i < N
Note:you have to delete exactly one occurrence of every character in the string.
1
5
abcde
1 2 3 4 5
Question 1
Overview
Approach/Solution
if(length>=k) break;
otherwise increment the length by v[i] and result by 1.Q2) Since we need to just delete one occurence of each character and minimize the remaining sum.
Hence it is better to delete individual characters maximum value position.
For example(Consider 0 based indexing)
Let's suppose 'a' is on 0,1,2 'b' is on 3,4,5 and array a[5, 7,2,4,8,10] And we need to delete exactly one occurence of each character than it is better to delete the position of individual characters with maximum value in array a
Hence for 'a' delete position 1(value=7) and for 'b' delete position 5(value=10) hence our answer is 5+2+0+4+8+0=19