BackmediumArraysSwiggyInfosys

Merge Unsorted Temperature Readings Solution

Problem Statement

Merge unsorted temperature readings from two arrays while preserving duplicates.

Example 1
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.

Example 2
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.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Merge Unsorted Temperature Readings — Problem Statement & Solution Guide

ArraysMediumMerge Sort / Quick Sort
TimeO(n log n)
|
SpaceO(n)

Problem Description

Merge unsorted temperature readings from two arrays while preserving duplicates.

Examples

Example 1

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.

Example 2

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

JavaScript Solution
Time: O(n log n)
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

SwiggyInfosys

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.