Problem solution · C++

Maximum Sized Array

Maximum Sized Array: a C++ 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
57 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

  • 57 lines of C++ from the credited upstream file 3344.cpp.
  • The implementation keeps its working state in language-native values and containers.
  • 2 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 · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  int maxSizedArray(long long s) {    if (s == 0)      return 1;     int l = 0;    int r = 1196;  // when s = 10^15, n = 1196     while (l < r) {      const int m = (l + r + 1) / 2;      if (getArraySum(m) <= s)        l = m;      else        r = m - 1;    }     return l;  }  private:  // 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.  int getNumbersWithBitSet(int n, int i) {    const int groupSize = 1 << (i + 1);    const int halfGroupSize = 1 << i;    const int fullGroups = n / groupSize;    const int remaining = max(0, (n % groupSize) - halfGroupSize);    return fullGroups * halfGroupSize + remaining;  }   // 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)  long getArraySum(int n) {    const int arithmeticSum = n * (n - 1) / 2;    long orSum = 0;    for (int i = 0; i < bitLength(n); ++i) {      const int numbersWituoutBit = n - getNumbersWithBitSet(n, i);      const int pairsWithBit =          (n * n) - (numbersWituoutBit * numbersWituoutBit);      orSum += pairsWithBit * (1L << i);    }    return arithmeticSum * orSum;  }   int bitLength(int n) {    return 32 - __builtin_clz(n);  }}; 

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 ↗