> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Array End Python Solution with Tests

> Tested Python solution for LeetCode 3133 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 3133, Medium. Topics: Bit Manipulation. [View on LeetCode](https://leetcode.com/problems/minimum-array-end/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 3133   # by problem number
lcpy gen -s minimum_array_end   # by problem name
```

## Problem

You are given two integers `n` and `x`. You have to construct an array of **positive** integers `nums` of size `n` where for every `0 <= i < n - 1`, `nums[i + 1]` is **greater than** `nums[i]`, and the result of the bitwise `AND` operation between all elements of `nums` is `x`.

Return the **minimum** possible value of `nums[n - 1]`.

### Examples

```
Input: n = 3, x = 4
Output: 6
Explanation: nums can be [4, 5, 6] and its last element is 6.
```

```
Input: n = 2, x = 7
Output: 15
Explanation: nums can be [7, 15] and its last element is 15.
```

### Constraints

* `1 <= n, x <= 10^8`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_array_end/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_array_end/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(1)  # bounded by 64 iterations
    # Space: O(1)
    def min_end(self, n: int, x: int) -> int:
        result = x
        increment = n - 1
        inc_bit = 0
        for bit in range(64):
            if (x >> bit) & 1 == 0:
                if (increment >> inc_bit) & 1:
                    result |= 1 << bit
                inc_bit += 1
        return result
```

## Complexity

| Time                             | Space |
| -------------------------------- | ----- |
| O(1)  # bounded by 64 iterations | O(1)  |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
