Python

Room rules: sopython.com/chatroom Code formatting guide: tinyu...
Jul 21, 2020 13:18
def f(A, k): # number of subarrays with at most k odd numbers
    res = i = j = countOdd = 0
    while j < len(A):
        countOdd += A[j] % 2
        while countOdd > k:  # shrink window
            countOdd -= A[i] % 2
            i += 1
        res += j - i + 1
        j += 1
    return res

foo = [1,1,2,1,1]

print(f(foo, 3) - f(foo, 2))
Jul 21, 2020 13:15
Not mine, a solution I came across. Trying to understand the theory behind it
Jul 21, 2020 13:14
That code, when called on an array using f(k) - f(k-1), will return the correct subarray count. I can't make sense of why res += j - i + 1 gives the correct value for configurations of a subarray, because it seems to be the length of the sliding window?
Jul 21, 2020 13:12
Some code given to solve the problem looks like this:

    def f(k): # number of subarrays with at most k odd numbers
        res = i = j = countOdd = 0
        while j < len(A):
            countOdd += A[j] % 2
            while countOdd > k:  # shrink window
                countOdd -= A[i] % 2
                i += 1
            res += j - i + 1
            j += 1
        return res
Jul 21, 2020 13:12
I've committed the XY problem -_-
Jul 21, 2020 13:09
An example is, given [1,1,2,1,1] return the number of subarrays with exactly 3 odd elements. The output should be 2, because the only possible combinations are [1,1,2,1] and [1,2,1,1].
Jul 21, 2020 13:03
Thanks for pointing that out. It seems I'm looking for permutations then. The question I'm trying to solve is: "given an array, return the number of possible subarrays with exactly k odd numbers". The answer to that question uses a sliding window and calculates the number of subarrays by adding the length of the sliding window to the result. So I concluded maybe the size of the array gives the number of permutations?
Jul 21, 2020 12:55
A bit of a theoretical question here. I'm writing a sliding window algorithm and I want to use it to figure out the possible configurations of a subarray. Is it always true that the length of an array is equal to the number of possible combinations? So if I have [1,2,3], it has length 3, and there are 3 possible combinations ([1,2,1], [2,1,1], [1,1,2]). Does this hold true for all arrays with size n?
 

SO Close Vote Reviewers

This room is for support and discussion about reviewing and co...
Jul 14, 2020 14:27
I'm out of flags unfortunately
Jul 14, 2020 14:25
Can't really think of a better room to post this in, I came across a strange post. A question was asked a few days ago, and then edited to ask something completely different a few minutes ago. A minute after that edit, someone adds a bounty. Is it justified to be suspicious about this? stackoverflow.com/questions/62853449/…
 

2020 Moderator Election Chat

Open discussion for the Stack Overflow election stackoverflow....
Jul 13, 2020 20:35
Is that because less applied?
Jul 13, 2020 20:27
As someone fairly new to all this, I'm trying to understand what the role of a moderator actually is. Are they able to influence the direction the company goes in by stating user demands, or are they simply there to enforce the rules?
 

Bad Stack Overflow Reviews

Room to discuss and report incorrect Stack Overflow reviews. A...
Jul 10, 2020 17:06
stackoverflow.com/review/triage/26642691
Jul 10, 2020 17:06
Hi, I've misclicked and selected a "Requires Edit". Instead, this needs moderator attention because it contains personal information, and I've used all my flags so I cannot go back and flag correctly. Forgive my mistake, the link is here: