BackmediumArraysPhonePe

Weight Category Partition Solution

Problem Statement

Partition the list of weights into two categories: weights less than 850 and weights greater than or equal to 850, while maintaining the relative order within each category.

Example 1
Input
[300, 500, 900, 1200, 1000]
Output
[300, 500, 900]

Explanation: Step-by-step: Given the input [300, 500, 900, 1200, 1000], we first initialize two empty lists, one for weights less than 850 and one for weights greater than or equal to 850. Then, we iterate over the input list. If a weight is less than 850, we append it to the first list. If a weight is greater than or equal to 850, we append it to the second list. Finally, we return the two lists. The output is [300, 500, 900].

Example 2
Input
[100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200]
Output
[100, 200, 300, 400, 500, 600, 700, 800]

Explanation: Step-by-step: Given the input [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200], we first initialize two empty lists, one for weights less than 850 and one for weights greater than or equal to 850. Then, we iterate over the input list. If a weight is less than 850, we append it to the first list. If a weight is greater than or equal to 850, we append it to the second list. Finally, we return the two lists. The output is [100, 200, 300, 400, 500, 600, 700, 800].

Constraints

  • 1 <= cargoWeights.length <= 10^5
  • 1 <= cargoWeights[i] <= 10^6
  • 1 <= targetWeight <= 10^6
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

Weight Category Partition — Problem Statement & Solution Guide

ArraysMediumTwo Pointers
TimeO(n)
|
SpaceO(n)

Problem Description

Partition the list of weights into two categories: weights less than 850 and weights greater than or equal to 850, while maintaining the relative order within each category.

Examples

Example 1

Input

[300, 500, 900, 1200, 1000]

Output

[300, 500, 900]

Explanation: Step-by-step: Given the input [300, 500, 900, 1200, 1000], we first initialize two empty lists, one for weights less than 850 and one for weights greater than or equal to 850. Then, we iterate over the input list. If a weight is less than 850, we append it to the first list. If a weight is greater than or equal to 850, we append it to the second list. Finally, we return the two lists. The output is [300, 500, 900].

Example 2

Input

[100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200]

Output

[100, 200, 300, 400, 500, 600, 700, 800]

Explanation: Step-by-step: Given the input [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200], we first initialize two empty lists, one for weights less than 850 and one for weights greater than or equal to 850. Then, we iterate over the input list. If a weight is less than 850, we append it to the first list. If a weight is greater than or equal to 850, we append it to the second list. Finally, we return the two lists. The output is [100, 200, 300, 400, 500, 600, 700, 800].

Constraints

  • 1 <= cargoWeights.length <= 10^5
  • 1 <= cargoWeights[i] <= 10^6
  • 1 <= targetWeight <= 10^6

Optimal Approach & Strategy

The optimal approach involves utilizing the Two Pointers technique to iterate through the cargoWeights array and partition it into two subarrays based on the target weight, preserving the original order. This can be achieved with a single pass through the array.

Brute Force Approach

A naive approach would involve using nested loops to compare each container's weight with the target weight, resulting in a time complexity of O(n²). This method is inefficient for large inputs. The brute-force approach would not maintain the original order.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function weightCategoryPartition(weights) { let lessThan850 = [], greaterThan850 = []; for (let weight of weights) { if (weight < 850) lessThan850.push(weight); else greaterThan850.push(weight); } return lessThan850.concat(greaterThan850); }

Asked in Top Tech Interviews

PhonePe

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.