Merge Unsorted Temperature Readings ā Problem Statement & Solution Guide
Problem Description
Merge unsorted temperature readings from two arrays while preserving duplicates.
Examples
Input
[1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 9, 10, 10]
Output
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 9, 10, 10]
Explanation: Step-by-step: First, we merge the two input arrays into one. Then, we sort the merged array in ascending order. Finally, we remove any duplicate elements from the sorted array to get the final output.
Input
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 10, 9, 9, 8, 8, 7, 7, 6, 6]
Output
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 10, 9, 9, 8, 8, 7, 7, 6, 6]
Explanation: Step-by-step: First, we merge the two input arrays into one. Then, we sort the merged array in descending order. Finally, we remove any duplicate elements from the sorted array to get the final output.
Constraints
- The input arrays can have duplicate temperature readings.
- The total number of elements in both input arrays is between 2 and 1000.
Optimal Approach & Strategy
The optimal approach involves using the quick sort algorithm to sort each input array in O(n log n) time complexity, followed by a two-pointer technique to merge the sorted arrays in O(n) time complexity. The overall time complexity of this approach is O(n log n).
Brute Force Approach
A naive approach would involve using nested loops to compare and swap elements in the input arrays, resulting in a time complexity of O(n²). This approach would be inefficient for large input arrays.
Verified Code Solutions
function mergeUnsortedTemperatureReadings(temperatures1, temperatures2) {
const mergedArray = [...temperatures1, ...temperatures2];
mergedArray.sort((a, b) => a - b);
const result = [mergedArray[0]];
for (let i = 1; i < mergedArray.length; i++) {
if (mergedArray[i] !== mergedArray[i - 1]) {
result.push(mergedArray[i]);
}
}
return result;
}class Solution {
public int[] mergeUnsortedTemperatureReadings(int[] arr1, int[] arr2) {
// Merge the two input arrays into one
int[] mergedArray = new int[arr1.length + arr2.length];
System.arraycopy(arr1, 0, mergedArray, 0, arr1.length);
System.arraycopy(arr2, 0, mergedArray, arr1.length, arr2.length);
// Sort the merged array in ascending order
Arrays.sort(mergedArray);
// Remove any duplicate elements from the sorted array
int[] finalArray = new int[mergedArray.length];
int j = 0;
for (int i = 0; i < mergedArray.length; i++) {
if (i == 0 || mergedArray[i] != mergedArray[i - 1]) {
finalArray[j++] = mergedArray[i];
}
}
return Arrays.copyOf(finalArray, j);
}
}def merge_unsorted_temperature_readings(arr1, arr2):
# Merge the two input arrays into one
merged_array = arr1 + arr2
# Sort the merged array in ascending order
merged_array.sort()
# Remove any duplicate elements from the sorted array
final_array = []
for num in merged_array:
if num not in final_array:
final_array.append(num)
return final_array
function mergeUnsortedTemperatureReadings(temperatures1, temperatures2) {
const mergedArray = [...temperatures1, ...temperatures2];
mergedArray.sort((a, b) => a - b);
const result = [mergedArray[0]];
for (let i = 1; i < mergedArray.length; i++) {
if (mergedArray[i] !== mergedArray[i - 1]) {
result.push(mergedArray[i]);
}
}
return result;
}Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.