<p>You have a bomb to defuse, and your time is running out! Your informer will provide you with a <strong>circular</strong> array <code>code</code>&nbsp;of length of <code>n</code>&nbsp;and a key <code>k</code>.</p>

<p>To decrypt the code, you must replace every number. All the numbers are replaced <strong>simultaneously</strong>.</p>

<ul>
	<li>If <code>k &gt; 0</code>, replace the <code>i<sup>th</sup></code> number with the sum of the <strong>next</strong> <code>k</code> numbers.</li>
	<li>If <code>k &lt; 0</code>, replace the <code>i<sup>th</sup></code> number with the sum of the <strong>previous</strong> <code>k</code> numbers.</li>
	<li>If <code>k == 0</code>, replace the <code>i<sup>th</sup></code> number with <code>0</code>.</li>
</ul>

<p>As <code>code</code> is circular, the next element of <code>code[n-1]</code> is <code>code[0]</code>, and the previous element of <code>code[0]</code> is <code>code[n-1]</code>.</p>

<p>Given the <strong>circular</strong> array <code>code</code> and an integer key <code>k</code>, return <em>the decrypted code to defuse the bomb</em>!</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> code = [5,7,1,4], k = 3
<strong>Output:</strong> [12,10,16,13]
<strong>Explanation:</strong> Each number is replaced by the sum of the next 3 numbers. The decrypted code is [7+1+4, 1+4+5, 4+5+7, 5+7+1]. Notice that the numbers wrap around.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> code = [1,2,3,4], k = 0
<strong>Output:</strong> [0,0,0,0]
<strong>Explanation:</strong> When k is zero, the numbers are replaced by 0. 
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> code = [2,4,9,3], k = -2
<strong>Output:</strong> [12,5,6,13]
<strong>Explanation:</strong> The decrypted code is [3+9, 2+3, 4+2, 9+4]. Notice that the numbers wrap around again. If k is negative, the sum is of the <strong>previous</strong> numbers.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == code.length</code></li>
	<li><code>1 &lt;= n&nbsp;&lt;= 100</code></li>
	<li><code>1 &lt;= code[i] &lt;= 100</code></li>
	<li><code>-(n - 1) &lt;= k &lt;= n - 1</code></li>
</ul>

class Solution:
    def decrypt(self, code: List[int], k: int) -> List[int]:
        prefSum = [0]
        f = k > 0
        k = abs(k)
        for i in code:
            prefSum.append(prefSum[-1]+i)
        def prevKSum(i):
            l = i-1
            r = i-k
            if r >= 0:
                return prefSum[l+1] - prefSum[r]
            else:
                # Sum[0:end] = prefSum[r]
                # inclusive end-l+1
                # Sum[end:end-l] = prefSun[r] - prefSum[end-l-1]
                # end-1,end-2..end-nt
                p = prefSum[l+1]
                nt = k - (l+1)
                r = len(prefSum)-1
                q = prefSum[r] - prefSum[len(code)-nt]
                return p+q

        def nextKSum(i):
            # Sum(code[i+1:i+k+1])
            # i included
            # Sum[0:i] = prefSum[i+1]
            # Sum[0:i+k] = prefSum[i+k+1]
            # Sum[0:end] = prefSum[-1]
            l = i+1
            r = l+k
            if r < len(prefSum):
                return prefSum[r] - prefSum[l]
            else:
                p = prefSum[-1] - prefSum[l]
                nt = len(code) - l + 1
                q = prefSum[k-nt+1]
                return p+q
        l = []
        for j in range(len(code)):
            i = code[j]
            if f:
                l.append(nextKSum(j))
            elif k == 0:
                l.append(0)
            else:
                l.append(prevKSum(j))
        return l

16600