Find the length of the longest subarray having sum greater than k
We can improve your code and speed it up from $O(n^2)$ to $O(n \cdot \log n)$ with one change.
First precompute all prefix sum. Let $P_t$ = the sum of the first $t$ elements. Notice that $P_t$ is defined from $0 \leq t \leq n$ and in particular $P_0=0$. This step is $O(n)$ so it is ok.
Now you need to find two index $0 \leq j &lt; i \leq n$ such that $P_i - P_j &gt; k$ and $i - j$ is maximum.
The next step in your solution is fixing the right most index ($i$), so we need to figure out a way to find fast the leftmost $j$ such that $P_i - P_j &gt; k$. For a fixed $i$ the condition can be converted to:
What is the smallest $j$ such that $P_i - k &gt; P_j$?
We might be tempted to do binary search here, but it is not correct since $P_j$ is not sorted... but wait, we can just get rid of &quot;not sorted elements&quot; easily. Let's say that if there exist two index $a &lt; b$ such that $P_a \leq P_b$, then we are not interested at all in the element at position $b$, since it will never be the leftmost index of an optimal interval, given that we can extend that interval up to position $a$.
With this in mind we can define an index $j$ important, if there is no index $k &lt; j$ such that $P_k \leq P_j$, i.e. $j$ is the smallest element in the prefix ending at $j$. One nice property of important elements, is that they form a strictly decreasing subsequence of array $P$, and for any fixed $i$, the optimal election of $j$ is an important index.
Since the important index array forms a strictly decreasing subsequence we can perform binary search without any issue. To build such array, we inserts elements one by one, if the new element is less than the minimum element in the array then it is important and should be inserted, otherwise, we don't add this element. The minimum element of the important index array will always be the last one.
Pseudocode:
def largest_subarray(array, k):
    n := length of array
    P := prefix sum of array
    # Remember that P[0] = 0, P[1] = array[1], ...

    # List of important index in increasing order.
    # Notice the values such indexes are pointing too are decreasing.
    # That is P[J[0]], P[J[1]], ... is strictly decreasing
    # Initially there is only index 0
    J := [0]

    # Answer of this function.
    longest_length := -1

    for i in 1..n:
        # Check if there is at least one solution ending
        # at index i. i.e. If P[i] - min(P[j &lt; i]) &gt; k
        
        # Index of the smallest element
        last_j := J[-1]
        if P[i] - P[J[-1]] &gt; k:
            # There is at least one solution, so we can run binary search
            # to find the optimal solution. i.e the smallest index j
            # such that P[i] - P[j] &gt; k
            # Since j belongs to J, we are finding the smallest index
            # p such that P[i] - P[J[p]] &gt; k
            p := bin_search()
            longest_length := max(longest_length, i - J[p])

        # Check if i is important
        if P[i] &lt; P[last_j]:
            J.push(i)
    
    return longest_length