BackmediumArrays Paytm

Galactic Cargo Sorting Solution

Problem Statement

Sort the cargo containers in the spaceship's cargo bay based on their weight. The containers must be separated into two categories: those weighing less than 850 kilograms and those weighing 850 kilograms or more. The order of containers within each category must be maintained as they were initially loaded.

Example 1
Input
[100, 200, 300, 700, 900, 950]
Output
{"lessThanTarget":[100,200,300,700,900],"greaterThanOrEqualToTarget":[950]}

Explanation: Step-by-step: We first initialize two empty lists, lessThanTarget and greaterThanOrEqualToTarget. Then we iterate over the input array. If a container's weight is less than 850 kilograms, we append it to lessThanTarget. If a container's weight is 850 kilograms or more, we append it to greaterThanOrEqualToTarget. Finally, we return the dictionary containing both lists.

Example 2
Input
[50, 75, 25, 100, 150, 200]
Output
{"lessThanTarget":[50,75,25,100,150,200],"greaterThanOrEqualToTarget":[]}

Explanation: Step-by-step: We first initialize two empty lists, lessThanTarget and greaterThanOrEqualToTarget. Then we iterate over the input array. If a container's weight is less than 850 kilograms, we append it to lessThanTarget. If a container's weight is 850 kilograms or more, we append it to greaterThanOrEqualToTarget. Finally, we return the dictionary containing both lists.

Constraints

  • 1 <= cargoWeights.length <= 10^5
  • 1 <= cargoWeights[i] <= 10^6
  • 1 <= targetWeight <= 10^6
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free