Problem solution · Python

Maximum Sized Array

Maximum Sized Array: a Python solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Sliding window or two pointers
Source
walkccc LeetCode Solutions
Length
39 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sliding window or two pointers

For Maximum Sized Array, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 39 lines of Python from the credited upstream file 3344.py.
  • The implementation keeps its working state in language-native values and containers.
  • No explicit loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMaximum Sized Array · PythonPython
Use this to learn the idea, then write your own version.
class Solution:  def maxSizedArray(self, s: int) -> int:    def getNumbersWithBitSet(n: int, i: int) -> int:      """      Returns the number of integers in [0, n - 1] with the i-th bit set.       For the i-th bit, numbers in the range [0, n - 1] can be divided into      groups of 2^(i + 1) numbers. In each group, exactly half of the numbers      have the i-th bit set.      """      groupSize = 1 << (i + 1)      halfGroupSize = 1 << i      fullGroups = n // groupSize      remaining = max(0, (n % groupSize) - halfGroupSize)      return fullGroups * halfGroupSize + remaining     def getArraySum(n: int) -> int:      """      Returns the sum of all i * (j OR k) values in 3D arrays of size n^3.         sum(i * (j OR k)), where 0 <= i, j, k < n      = 0 * (j OR k) + 1 * (j OR k) + ... + (n - 1) * (j OR k)      = (0 + 1 + ... + n - 1) * sum(j OR k)      = (n * (n - 1) / 2) * sum(j OR k)      """      arithmeticSum = n * (n - 1) // 2  # 0 + 1 + ... + n - 1      orSum = 0  # the sum of (j OR k) values in 2D arrays of size n^2      for i in range(n.bit_length()):        numbersWithoutBit = n - getNumbersWithBitSet(n, i)        pairsWithBit = n**2 - numbersWithoutBit**2        orSum += pairsWithBit * (1 << i)  # Add contribution of this bit.      return arithmeticSum * orSum     if s == 0:      return 1    l = 0    r = 1196  # when s = 10^15, n = 1196    return bisect.bisect_right(range(l, r + 1), s, key=getArraySum) - 1 + l 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗