Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Frequency Counting algorithm but will also help you build up problem-solving skills.

POTD 1 November: Frequencies of Limited Range Array Elements:
Given an array arr[] of N positive integers which can contain integers from 1 to P where elements can be repeated or can be absent from the array. Your task is to count the frequency of all numbers from 1 to N. Make in-place changes in arr[], such that arr[i] = frequency(i). Assume 1-based indexing.
Note: The elements greater than N in the array can be ignored for counting and modify the array in-place.
Example:
Input:N = 5, arr[] = {2, 3, 2, 3, 5}, P = 5
Output: 0 2 2 0 1
Explanation: Counting frequencies of each array element. We have: 1 occurring 0 times, 2 occurring 2 times, 3 occurring 2 times, 4 occurring 0 times, 5 occurring 1 time.Input:N = 4, arr[] = {3,3,3,3}, P = 3
Output: 0 0 4 0
Explanation: Counting frequencies of each array element. We have: 1 occurring 0 times, 2 occurring 0 times, 3 occurring 4 times, 4 occurring 0 times.
Frequencies of Limited Range Array Elements by Storing the Frequency as Negative Values:
The idea is to traverse the given array, use elements as an index and store their counts at the index. Since the elements are positive so we will store the frequency as negative values. For a element at ith index, the frequency should be updated at index idx, where idx=arr[i]-1. If arr[idx] is less than 0 then it will decrement arr[idx] else, we will replace the element arr[i] with arr[idx] and then set arr[idx] to -1 so that element at arr[idx] is not lost.
Follow the steps to solve the above problem:
- Traverse the array from start to end.
- For each element check if the element is less than or equal to zero or not. If negative skip the element as it is the frequency. If the element is greater than N then also we will skip the element because no need to calculate its frequency.
- Let idx=arr[i]-1. Then we have 2 cases:
- If arr[idx] is negative, then it is not the first occurrence, we simply update arr[idx] to arr[idx]-1.
- If arr[idx] is positive, we will replace the element arr[i] with arr[idx] and then set arr[idx] to -1 so that element at arr[idx] is not lost.
- Traverse then array again. If the element at ith index is negative it means it represents a frequency so set negative elements to their absolute values else if the element is positive then it means the element i has not occurred, so set arr[i] to 0.
Below is the implementation of above approach:
class Solution {
public:
// Function to count the frequency of all elements from
// 1 to N in the array.
void frequencyCount(vector<int>& arr, int N, int P)
{
int i = 0;
while (i < N) {
// Calculate the index for the current element
int idx = arr[i] - 1;
// Check if the index is out of bounds
if (idx >= N || idx < 0) {
i++;
continue;
}
// Check if the element at the calculated index
// is negative (indicating a frequency)
if (arr[idx] <= 0) {
// Update frequency
arr[idx]--;
// Move to the next element
i++;
}
else {
// Store the original value at idx
int st = arr[idx];
// set the frequency
arr[idx] = -1;
// If the calculated index (idx) is greater
// than the current index (i), it means we
// haven't visited this element yet.
if (idx > i) {
// Replace the current element with the
// stored value
arr[i] = st;
}
// Move to the next element
else {
i++;
}
}
}
// Traverse the array again to clean up and set
// positive elements to 0, and negative elements to
// their absolute values.
for (int i = 0; i < N; i++) {
if (arr[i] < 0) {
// Set negative elements to their absolute
// values (frequencies)
arr[i] = -arr[i];
}
else {
// Set positive elements to 0 (elements not
// present in the array)
arr[i] = 0;
}
}
}
};
class Solution {
// Function to count the frequency of all elements from
// 1 to N in the array.
public static void frequencyCount(int arr[], int N,
int P)
{
int i = 0;
while (i < N) {
// Calculate the index for the current element
int idx = arr[i] - 1;
// Check if the index is out of bounds
if (idx >= N || idx < 0) {
i++;
continue;
}
// Check if the element at the calculated index
// is negative (indicating a frequency)
if (arr[idx] <= 0) {
// Update frequency
arr[idx]--;
// Move to the next element
i++;
}
else {
// Store the original value at idx
int st = arr[idx];
// Set the frequency
arr[idx] = -1;
// If the calculated index (idx) is greater
// than the current index (i), it means we
// haven't visited this element yet.
if (idx > i) {
// Replace the current element with the
// stored value
arr[i] = st;
}
else {
i++;
}
}
}
// Traverse the array again to clean up and set
// positive elements to 0, and negative elements to
// their absolute values.
for (i = 0; i < N; i++) {
if (arr[i] < 0) {
// Set negative elements to their absolute
// values (frequencies)
arr[i] = -arr[i];
}
else {
// Set positive elements to 0 (elements not
// present in the array)
arr[i] = 0;
}
}
}
}
class Solution:
# Function to count the frequency of all elements
# from 1 to N in the array.
def frequencyCount(self, arr, N, P):
i = 0
while i < N:
# Calculate the index for the current element
idx = arr[i] - 1
# Check if the index is out of bounds
if idx >= N or idx < 0:
i += 1
continue
# Check if the element at the calculated index
# is negative (indicating a frequency)
if arr[idx] <= 0:
# Update frequency
arr[idx] -= 1
# Move to the next element
i += 1
else:
# Store the original value at idx
st = arr[idx]
# Set the frequency
arr[idx] = -1
# If the calculated index (idx) is greater
# than the current index (i),
# it means we haven't visited this element yet.
if idx > i:
# Replace the current element
# with the stored value
arr[i] = st
# Move to the next element
else:
i += 1
# Traverse the array again to clean up and
# set positive elements to 0, and negative elements
# to their absolute values.
for i in range(N):
if arr[i] < 0:
# Set negative elements to their absolute values (frequencies)
arr[i] = -arr[i]
else:
# Set positive elements to 0 (elements not present in the array)
arr[i] = 0
Time Complexity: O(N)
Auxilary Space: O(1)