Wednesday, 1 Dec 2021
Category: frequency-counting

Given a string S of size N, the task is to find the maximum sum of values assigned to all the alphabets of the string S. The value assigned to all the characters are over the range [1, 26], and the values assigned to the same lowercase and uppercase character is the same.Examples:Attention reader! Don’t […]

Given a string S of N characters, the task is to calculate the total number of non-empty substrings such that at most one character occurs an odd number of times.Example: Input: S = “aba”Output: 4Explanation: The valid substrings are “a”, “b”, “a”, and “aba”. Therefore, the total number of required substrings are 4.Input: “aabb”Output: 9Explanation: The […]

Given an array arr[] consisting of N integers, the task is to find the maximum element with the minimum frequency.Examples:Input: arr[] = {2, 2, 5, 50, 1}Output: 50Explanation:The element with minimum frequency is {1, 5, 50}. The maximum element among these element is 50.Input: arr[] = {3, 2, 5, 6, 1}Output: 6Approach: The given problem […]

Given an array A[]of N strings of lowercase characters, the task is to find maximum number of strings, such that one character has majority i.e. the occurrence of one character in all of the strings is greater than all the other characters combined.Examples:Input: A[] = {“aba”, “abcde”, “aba”}Output: 2Explanation: On choosing {“aba”, “aba”}, the occurrence […]

#include using namespace std;  int minimumPalindromicStrings(string S){        int N = S.length();                      vector freq(26);    for (int i = 0; i < N; i++) {        freq[S[i] - 'a']++;    }          vector oddFreqCharacters;                                for (int i = 0; i < 26; i++) {          if (freq[i] & 1) {              oddFreqCharacters.push_back(i);            freq[i]--;        }          freq[i] /= 2;    }          vector ans;              if (oddFreqCharacters.empty()) {                          string left = "";          for (int i = 0; i < 26; i++) {            for (int j = […]

Given an array arr[] consisting of N positive integers, the task is to count the number of pairs in the array, say (a, b) such that sum of a with its sum of digits is equal to sum of b with its sum of digits.Examples:Input: arr[] = {1, 1, 2, 2}Output: 2Explanation:Following are the pairs […]

function maxAmount(n, k, arr)        {                                             let A = new Array(1000001).fill(0);            for (let i = 0; i < n; i++)            {                A[arr[i]]++;            }            let j = 0;                                                 for (let i = 0; i < 1000001; i++) {                while (A[i] != 0) {                    arr[j++] = i;                    A[i]--;                }            }                        let ans = 0;            let mod = 1e9 + 7;            let i = n - 1;            j = n - 2;                                    while (k > 0 && […]

Given a 2D array arr[][] consisting of N*N positive integers, the task is to generate an N-length array such that Greatest Common Divisors(GCD) of all possible pairs of that array is present in the array arr[][].Examples:Input: N = 4, arr[] = {2, 1, 2, 3, 4, 3, 2, 6, 1, 1, 2, 2, 1, 2, […]

#include using namespace std;  void findIfPossible(int N, string str){    int countG = 0, countF = 0;    for (int i = 0; i < N; i++) {          if (str[i] == 'G')            countG++;        else            countF++;    }      if (2 * countF != countG) {        cout

public class GFG {                  static String checkIfPossible(        int N, String[] arr, String T)    {                        int[] freqS = new int;                          int[] freqT = new int;                          for (char ch : T.toCharArray()) {            freqT[ch – ‘a’]++;        }                  for (int i = 0; i < N; i++) {                                      for (char ch : arr[i].toCharArray()) {                freqS[ch - 'a']++;            }        }          for (int i = 0; i < 256; i++) {                                      if (freqT[i] == 0                && freqS[i] […]