Given an array of n integers, find the sum of the frequencies of the highest occurring number and lowest occurring number.
Input: arr = [1, 2, 2, 3, 3, 3]
Output: 4
Explanation: The highest frequency is 3 (element 3), and the lowest frequency is 1 (element 1). Their sum is 3 + 1 = 4.
Input: arr = [4, 4, 5, 5, 6]
Output: 3
Explanation: The highest frequency is 2 (elements 4 and 5), and the lowest frequency is 1 (element 6). Their sum is 2 + 1 = 3.
Input: arr = [10, 9, 7, 7, 8, 8, 8]
The brute force way to solve this problem will be to count the frequency of each element in the array, and once found, this frequency can be compared with the highest and the lowest frequency. Accordingly, the highest and the lowest frequency can be set.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
/* Function to get the sum of highest
and lowest frequency in array */
int sumHighestAndLowestFrequency(vector<int> &nums) {
// Variable to store the size of array
int n = nums.size();
/* Variable to store maximum
and minimum frequency */
int maxFreq = 0;
int minFreq = n;
// Visited array
vector<bool> visited(n, false);
// First loop
for(int i = 0; i < n; i++) {
// Skip second loop if already visited
if(visited[i]) continue;
/* Variable to store frequency
of current element */
int freq = 0;
// Second loop
for(int j = i; j < n; j++) {
if(nums[i] == nums[j]) {
freq++;
visited[j] = true;
}
}
/* Update maximum and
minimum frequencies */
maxFreq = max(maxFreq, freq);
minFreq = min(minFreq, freq);
}
// Return the required sum
return maxFreq+minFreq;
}
};
int main() {
vector<int> nums = {1, 2, 2, 3, 3, 3};
/* Creating an instance of
Solution class */
Solution sol;
/* Function call to get the sum of highest
and lowest frequency in array */
int ans = sol.sumHighestAndLowestFrequency(nums);
cout << "The sum of highest and lowest frequency in the array is: " << ans;
return 0;
}
import java.util.*;
class Solution {
/* Function to get the sum of highest
and lowest frequency in array */
public int sumHighestAndLowestFrequency(int[] nums) {
// Variable to store the size of array
int n = nums.length;
/* Variable to store maximum
and minimum frequency */
int maxFreq = 0;
int minFreq = n;
// Visited array
boolean[] visited = new boolean[n];
// First loop
for (int i = 0; i < n; i++) {
// Skip second loop if already visited
if (visited[i]) continue;
/* Variable to store frequency
of current element */
int freq = 0;
// Second loop
for (int j = i; j < n; j++) {
if (nums[i] == nums[j]) {
freq++;
visited[j] = true;
}
}
/* Update maximum and
minimum frequencies */
maxFreq = Math.max(maxFreq, freq);
minFreq = Math.min(minFreq, freq);
}
// Return the required sum
return maxFreq + minFreq;
}
public static void main(String[] args) {
int[] nums = {1, 2, 2, 3, 3, 3};
/* Creating an instance of
Solution class */
Solution sol = new Solution();
/* Function call to get the sum of highest
and lowest frequency in array */
int ans = sol.sumHighestAndLowestFrequency(nums);
System.out.println("The sum of highest and lowest frequency in the array is: " + ans);
}
}
class Solution:
""" Function to get the sum of highest
and lowest frequency in array """
def sumHighestAndLowestFrequency(self, nums):
# Variable to store the size of array
n = len(nums)
""" Variable to store maximum
and minimum frequency """
max_freq = 0
min_freq = n
# Visited array
visited = [False] * n
# First loop
for i in range(n):
# Skip second loop if already visited
if visited[i]:
continue
""" Variable to store frequency
of current element """
freq = 0
# Second loop
for j in range(i, n):
if nums[i] == nums[j]:
freq += 1
visited[j] = True
""" Update maximum and
minimum frequencies """
max_freq = max(max_freq, freq)
min_freq = min(min_freq, freq)
# Return the required sum
return max_freq + min_freq
# Example usage
nums = [1, 2, 2, 3, 3, 3]
""" Creating an instance of
Solution class """
sol = Solution()
""" Function call to get the sum of highest
and lowest frequency in array """
ans = sol.sumHighestAndLowestFrequency(nums)
print("The sum of highest and lowest frequency in the array is:", ans)
class Solution {
/* Function to get the sum of highest
and lowest frequency in array */
sumHighestAndLowestFrequency(nums) {
// Variable to store the size of array
let n = nums.length;
/* Variable to store maximum
and minimum frequency */
let maxFreq = 0;
let minFreq = n;
// Visited array
let visited = Array(n).fill(false);
// First loop
for (let i = 0; i < n; i++) {
// Skip second loop if already visited
if (visited[i]) continue;
/* Variable to store frequency
of current element */
let freq = 0;
// Second loop
for (let j = i; j < n; j++) {
if (nums[i] === nums[j]) {
freq++;
visited[j] = true;
}
}
/* Update maximum and
minimum frequencies */
maxFreq = Math.max(maxFreq, freq);
minFreq = Math.min(minFreq, freq);
}
// Return the required sum
return maxFreq + minFreq;
}
}
// Example usage
let nums = [1, 2, 2, 3, 3, 3];
/* Creating an instance of
Solution class */
let sol = new Solution();
/* Function call to get the sum of highest
and lowest frequency in array */
let ans = sol.sumHighestAndLowestFrequency(nums);
console.log("The sum of highest and lowest frequency in the array is:", ans);
Time Complexity: O(N2) (where N is the size of the array given) – Using two nested loops.
Space Complexity: O(N) – Using a visited array of size N and a couple of variables.
An optimal approach to solve this question will be to use a Hashmap, a data structure that stores key-value pairs.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
/* Function to get the sum of highest
and lowest frequency in array */
int sumHighestAndLowestFrequency(vector<int> &nums) {
// Variable to store the size of array
int n = nums.size();
/* Variable to store maximum
and minimum frequency */
int maxFreq = 0, minFreq = n;
// HashMap
unordered_map<int, int> mpp;
// Iterating on the array
for (int i = 0; i < n; i++) {
// Updating hashmap
mpp[nums[i]]++;
}
// Iterate on the map
for(auto it : mpp) {
int freq = it.second;
/* Update maximum and
minimum frequencies */
maxFreq = max(maxFreq, freq);
minFreq = min(minFreq, freq);
}
// Return the required sum
return maxFreq + minFreq;
}
};
int main() {
vector<int> nums = {1, 2, 2, 3, 3, 3};
/* Creating an instance of
Solution class */
Solution sol;
/* Function call to get the sum of highest
and lowest frequency in array */
int ans = sol.sumHighestAndLowestFrequency(nums);
cout << "The sum of highest and lowest frequency in the array is: " << ans;
return 0;
}
import java.util.HashMap;
class Solution {
/* Function to get the sum of highest
and lowest frequency in array */
public int sumHighestAndLowestFrequency(int[] nums) {
// Variable to store the size of array
int n = nums.length;
/* Variable to store maximum
and minimum frequency */
int maxFreq = 0, minFreq = n;
// HashMap
HashMap<Integer, Integer> mpp = new HashMap<>();
// Iterating on the array
for (int i = 0; i < n; i++) {
// Updating hashmap
mpp.put(nums[i], mpp.getOrDefault(nums[i], 0) + 1);
}
// Iterate on the map
for (int freq : mpp.values()) {
/* Update maximum and
minimum frequencies */
maxFreq = Math.max(maxFreq, freq);
minFreq = Math.min(minFreq, freq);
}
// Return the required sum
return maxFreq + minFreq;
}
public static void main(String[] args) {
int[] nums = {1, 2, 2, 3, 3, 3};
/* Creating an instance of
Solution class */
Solution sol = new Solution();
/* Function call to get the sum of highest
and lowest frequency in array */
int ans = sol.sumHighestAndLowestFrequency(nums);
System.out.println("The sum of highest and lowest frequency in the array is: " + ans);
}
}
class Solution:
# Function to get the sum of highest
# and lowest frequency in array
def sumHighestAndLowestFrequency(self, nums):
# Variable to store the size of array
n = len(nums)
# Variable to store maximum
# and minimum frequency
maxFreq = 0
minFreq = n
# HashMap
mpp = {}
# Iterating on the array
for num in nums:
# Updating hashmap
if num in mpp:
mpp[num] += 1
else:
mpp[num] = 1
# Iterate on the map
for freq in mpp.values():
# Update maximum and
# minimum frequencies
maxFreq = max(maxFreq, freq)
minFreq = min(minFreq, freq)
# Return the required sum
return maxFreq + minFreq
# Test the solution
if __name__ == "__main__":
nums = [1, 2, 2, 3, 3, 3]
# Creating an instance of
# Solution class
sol = Solution()
# Function call to get the sum of highest
# and lowest frequency in array
ans = sol.sumHighestAndLowestFrequency(nums)
print(f"The sum of highest and lowest frequency in the array is: {ans}")
class Solution {
/* Function to get the sum of highest
and lowest frequency in array */
sumHighestAndLowestFrequency(nums) {
// Variable to store the size of array
const n = nums.length;
/* Variable to store maximum
and minimum frequency */
let maxFreq = 0, minFreq = n;
// HashMap
const mpp = new Map();
// Iterating on the array
nums.forEach(num => {
// Updating hashmap
if (mpp.has(num)) {
mpp.set(num, mpp.get(num) + 1);
} else {
mpp.set(num, 1);
}
});
// Iterate on the map
for (let freq of mpp.values()) {
/* Update maximum and
minimum frequencies */
maxFreq = Math.max(maxFreq, freq);
minFreq = Math.min(minFreq, freq);
}
// Return the required sum
return maxFreq + minFreq;
}
}
// Test the solution
const nums = [1, 2, 2, 3, 3, 3];
// Creating an instance of
// Solution class
const sol = new Solution();
// Function call to get the sum of highest
// and lowest frequency in array
const ans = sol.sumHighestAndLowestFrequency(nums);
console.log(`The sum of highest and lowest frequency in the array is: ${ans}`);
Time Complexity: O(N) (where N is the size of the array given) –
Space Complexity: O(N) – In the worst-case scenario, the HashMap will store all the elements in the array when array elements are unique.