Question

Even Subarray A subarray is a contiguous portion of an array.Given an array of integers,...

Even Subarray A subarray is a contiguous portion of an array. Given an array of integers, determine the number of distinct subarrays that can be formed having at most a given number of odd elements Two subarrays are distinct if they differ at even one position their contents. For example, if numbers [1, 2, 3, 4] and the maximum number of odd elements allowed, k 1, the following is a list of the 8 distinct valid subarrays: [[1], [21, [3], [4], [1,2], [2, 31, [3, 4], [2, :

11 1
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1
ON 1. def countSubarrays(ar, ment): 2. count=0 3. n=len(a) for m in range(mcnt+1): 5. prefix = [0] *n 6. odd = 0 7. # travers
def countSubarrays(ar,mcnt): 
    count=0
    n=len(a)
    for m in range(mcnt+1):
        prefix = [0] * n 
        odd = 0
        # traverse in the array 
        for i in range(n): 
            prefix[odd] += 1
            # if array element is odd 
            if (a[i] & 1): 
                odd += 1
            # when number of odd elements>=M 
            if (odd >=m): 
                count += prefix[odd - m] 
    return count 
        
# Driver Code 
a = [1,2,3,4] 
n = len(a) 
m = 1

print(countSubarrays(a,m)) 

Add a comment
Know the answer?
Add Answer to:
Even Subarray A subarray is a contiguous portion of an array.Given an array of integers,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT