Skip to main content

Maximum Value of Concatenated Binary Segments - Video Solutions

HardArrayGreedySorting

LeetCode BiWeekly Contest 180 - Q4 - Maximum Value of Concatenated Binary Segments(3897) Explained

Kumar K [Amazon]
28:16715 views
4 video solutions available

Maximum Value of Concatenated Binary Segments - Video Solution

Watch 4 video solutions for Maximum Value of Concatenated Binary Segments, a hard level problem involving Array, Greedy, Sorting. This walkthrough by Kumar K [Amazon] has 715 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

You are given two integer arrays nums1 and nums0, each of size n.

  • nums1[i] represents the number of '1's in the ith segment.
  • nums0[i] represents the number of '0's in the ith segment.

For each index i, construct a binary segment consisting of:

  • nums1[i] occurrences of '1' followed by
  • nums0[i] occurrences of '0'.

You may rearrange the order of these segments in any way. After rearranging, concatenate all segments to form a single binary string.

Return the maximum possible integer value of the concatenated binary string.

Since the result can be very large, return the answer modulo 109 + 7.

 

Example 1:

Input: nums1 = [1,2], nums0 = [1,0]

Output: 14

Explanation:

  • At index 0, nums1[0] = 1 and nums0[0] = 1, so the segment formed is "10".
  • At index 1, nums1[1] = 2 and nums0[1] = 0, so the segment formed is "11".
  • Reordering the segments as "11" followed by "10" produces the binary string "1110".
  • The binary number "1110" has value 14 which is the maximum possible value.

Example 2:

Input: nums1 = [3,1], nums0 = [0,3]

Output: 120

Explanation:

  • At index 0, nums1[0] = 3 and nums0[0] = 0, so the segment formed is "111".
  • At index 1, nums1[1] = 1 and nums0[1] = 3, so the segment formed is "1000".
  • Reordering the segments as "111" followed by "1000" produces the binary string "1111000".
  • The binary number "1111000" has value 120 which is the maximum possible value.

 

Constraints:

  • 1 <= n == nums1.length == nums0.length <= 105
  • 0 <= nums1[i], nums0[i] <= 104
  • nums1[i] + nums0[i] > 0
  • The total sum of all elements in nums1 and nums0 does not exceed 2 * 105.
Read full problem with examples

Approach Overview

Problem Overview: You are given multiple binary segments and must concatenate them in some order to produce the largest possible binary value. The task is essentially choosing the best ordering of segments so the final concatenated binary string represents the maximum decimal value.

Approach 1: Brute Force Permutations (O(n! * k) time, O(n) space)

Generate every possible ordering of the binary segments using permutations. For each permutation, concatenate the segments and compute the resulting value (or compare the binary strings directly). Track the maximum result seen so far. This approach works for very small n but quickly becomes infeasible because the number of permutations grows factorially. It is mainly useful for understanding the problem and validating test cases.

Approach 2: Greedy Sorting by Concatenation Order (O(n log n * k) time, O(n) space)

The key insight: for two segments a and b, the better order is whichever produces a larger binary string between a + b and b + a. If a + b is larger, place a before b; otherwise place b first. Sorting all segments using this custom comparator produces the optimal global order. The comparison works because maximizing the most significant bits of the final concatenation dominates the value. Implement the comparator with string concatenation and lexicographic comparison. This pattern is similar to the classic “largest number by concatenation” problem and is naturally implemented with sorting and greedy reasoning.

Approach 3: Bit-Length Aware Comparison (O(n log n) comparisons, O(n) space)

Instead of building large intermediate strings repeatedly, treat each segment as a binary number with a known bit length. When comparing a before b versus b before a, compute the shifted values: (a << len(b)) | b and (b << len(a)) | a. Compare these two numbers to determine order. This reduces temporary string allocations and highlights the bit-level behavior of the concatenation. The technique relies on concepts from bit manipulation and works well when segment lengths are manageable.

Recommended for interviews: The greedy sorting comparator is what interviewers typically expect. Starting with the brute force permutation shows you understand the search space, but recognizing the a+b vs b+a ordering rule demonstrates the key greedy insight and reduces the complexity to O(n log n), which is scalable for large inputs.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force PermutationsO(n! * k)O(n)Very small inputs or verifying correctness of other approaches
Greedy Sort by Concatenation (a+b vs b+a)O(n log n * k)O(n)General case; standard optimal solution used in interviews
Bit-Length Aware ComparatorO(n log n)O(n)When avoiding large string concatenations and using numeric bit operations