Question

In: Other

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, :

Solutions

Expert Solution

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)) 


Related Solutions

Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
%) Even integers with non repeating digits. Even integers are integers ending in 0, 2, 4,...
%) Even integers with non repeating digits. Even integers are integers ending in 0, 2, 4, 6, 8. It is easy to see that for 2 digits integers, the number of 2 digits even integers is 45 (i.e. from 10 to 99, 10, 12, 14, …, 96, 98 are the 2 digits even integers); for 3 digits integers, the number of 3 digits even integers is 450 (from 100 to 999, 100, 102, 104, .. , till 994, 996, 998),...
1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
Write array methods that carry out the following tasks for an array of integers by creating...
Write array methods that carry out the following tasks for an array of integers by creating and completing the “ArrayMethods” class below. Add documentation comments for each method. Provide a test program called ‘Lab5_yourID.java” that test methods of ArrayMethods class. In your test program, use random class to generate array values. public class ArrayMethods { private int[ ] values; //declare instant variables public ArrayMethods (int[ ] initialValues) {values = initialValues;} //constructor public void shiftRight( ) { … } public Boolean...
Problem Definition: Problem: Given an array of integers print all pairs of integers a and b...
Problem Definition: Problem: Given an array of integers print all pairs of integers a and b where a + b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a + b = 16 A= [ 10, 4, 6, 15, 3, 5, 1, 13] The following are pairs that sum to 16: 13, 3 6, 10 15, 1 Your program should print these...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT