Given a string str consisting of N lowercase characters and an array Q[][] with each row of the form {l, r} representing a query. For each query, the task is to find the difference between the maximum frequency and minimum frequency of the characters in the substring {str[l], …. str[r]}.
Note: Consider 1 – based indexing.
Examples:
Input: N = 7, S = “abaabac”, Q[][] = {{ 2, 6 }, { 1, 7 }}
Output: 1 3
Explanation:
Query 1: ‘a’ occurs maximum number of times in the given range i.e., 3 and ‘b’ occurs minimum number of times in the given range i.e. 2. Therefore, output = 3 – 2 = 1.
Query 2: ‘a’ occurs maximum number of times in the given range i.e. 4 and ‘c’ occurs minimum number of times in the given range i.e. 1. Therefore, output = 4 – 1 = 3.Input: N = 6, S = “aabbcc”, Q[][] = {{1, 3}, {1, 6}}
Output: 0 0
Explanation:
Query 1: ‘a’ and ‘b’ both occurs same number of times in the given range. Therefore, the output is 0.
Query 2: All ‘a’, ‘b’ and ‘c’ occurs same number of times in the given range. Therefore, the output is 0.
Naive Approach: For each query, find the frequencies of all characters in the given range, and take the difference between the maximum and minimum frequencies.
Time Complexity: O((N + 26)* |Q|)
Auxiliary Space: O(26)
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Funciton to find difference between maximum and // minimum frequency of a character in given range void maxDiffFreq(vector<pair<int, int> > queries, string S) { // Stores length of string int N = S.size(); // Stores count of queries int Q = queries.size(); // Iterate over the characters // of the string for (int i = 0; i < Q; ++i) { // Stores l-value of a query int l = queries[i].first - 1; // Stores r-value of a query int r = queries[i].second - 1; int freq[26] = { 0 }; // Store count of every character // laying in range [l, r] for (int j = l; j <= r; j++) { // Update frequency of // current character freq[S[j] - 'a']++; } // Stores maximum frequency // of characters in given range int mx = 0; // Stores minimum frequency // of characters in given range int mn = 99999999; // Iterate over all possible characters // of the given string for (int j = 0; j < 26; j++) { // Update mx mx = max(mx, freq[j]); // If (j + 'a') is present if (freq[j]) mn = min(mn, freq[j]); } // difference between max and min cout << mx - mn << endl; } } // Driver Code int main() { // Given string string S = "abaabac"; // Given queries vector<pair<int, int> > queries{ { 2, 6 }, { 1, 7 } }; // Function Call maxDiffFreq(queries, S); } |
1 3
Efficient Approach: The idea is to use a 2D-Fenwick tree to store the frequency of each character. Follow the steps below to solve the problem:
- Create a two-dimensional Fenwick tree that stores the information about each character from ‘a’ to ‘z’.
- Then for each query, count the frequencies of each character in the given range using the Fenwick tree.
- From the frequencies found above, get the maximum and the minimum frequency.
- Print the difference between the maximum and minimum frequency as the answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to update frequency of // a character in Fenwick tree void update(int BIT[26][10005], int idx, int i, int val) { while (i < 10005) { // Update frequency of (idx + 'a') BIT[idx][i] += val; // Update i i = i + (i & (-i)); } } // Function to find the frequency of // a character (idx + 'a') in range [1, i] int query(int BIT[26][10005], int idx, int i) { // Stores frequency of character, (idx + 'a') // in range [1, i] int ans = 0; while (i > 0) { // Update ans ans += BIT[idx][i]; // Update i i = i - (i & (-i)); } return ans; } // Function to find difference between maximum and // minimum frequency of a character in given range void maxDiffFreq(string s, vector<pair<int, int> > queries) { // BIT[i][j]: Stores frequency of (i + 'a') // If j is a power of 2, then it stores // the frequency (i + 'a') of from [1, j] int BIT[26][10005]; // Stores length of string int n = s.size(); // Iterate over the characters // of the string for (int i = 0; i < n; i++) { // Update the frequency of // s[i] in fenwick tree update(BIT, s[i] - 'a', i + 1, 1); } // Stores count of queries int Q = queries.size(); // Iterate over all the queries for (int i = 0; i < Q; ++i) { // Stores maximum frequency of // a character in range [l, r] int mx = 0; // Stores minimum frequency of // a character in range [l, r] int mn = INT_MAX; int l = queries[i].first; int r = queries[i].second; // Iterate over all possible characters for (int j = 0; j < 26; j++) { // Stores frequency of (j + 'a') // in range [1, r] int p = query(BIT, j, r); // Stores frequency of (j + 'a') // in range [1, l - 1] int q = query(BIT, j, l - 1); // Update mx mx = max(mx, p - q); // If a character (i + 'a') present // in range [l, r] if (p > 0) { // Update mn mn = min(mn, p - q); } } // Print the difference between // max and min freq cout << mx - mn << endl; } } // Driver Code int main() { // Given string string S = "abaabac"; // Given queries vector<pair<int, int> > queries = { { 2, 6 }, { 1, 7 } }; // Function Call maxDiffFreq(S, queries); } |
1 3
Time Complexity: O(|Q| * log(N) * 26)
Auxiliary Space: O(N * 26)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

