Question

How to design an algorithm using recursion to find number of subarray contains all 0 in...

How to design an algorithm using recursion to find number of subarray contains all 0 in an array which only contains 0 or 1? For example, we have array 00, so we return 1, if we have an array 0100, we return 2. Assume the size of array >=1. How to find the recurrence relation of the algorithm? And how to express the recurrence relation as a function of n.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here we need to number of subarrau which contains all 0 in array which contains only 0 or 1.

Basically we need to find the number of subgroups of array which contains zeros

Input: 0100 should return 2

Algorithm:

Input : arr , n // arr - array , n = array size

count = 0

IF arr[0] = 0

then count ++

FOR i=0 ; i<n-1 ;i++

IF arr[i] = 1 and arr[i+1] = 0  

THEN count++

return COUNT

In the above algorithm we are just scanning the array and counting number of occurences of 1 followed by 0 in given array and if arr[0] = 0 we increment count initially to 1. So by counting 10 patterns we get total subgroups.

Below is the recursive logic:

RECURSIVE ALGORITHM:

int count(arr,n):

if ( n == 1 ) return 0;

if( arr[0] == 1 && arr[1] == 0 ) return 1+count(arr+1,n-1);

else return count(arr+1,n-1);

In Main function:

IF (arr[0] ==0) print 1+count(arr,n)

ELSE print count(arr,n)

Here basically we are doing some constant work in each recursive call and calling recursive function with size 1 less than the input .

Let T(n) be time taken for arr of size N

Then recursive relation would be

T(n) = T(n-1) + c [ c = constant amount of work]

Complexity: O(n)

Add a comment
Know the answer?
Add Answer to:
How to design an algorithm using recursion to find number of subarray contains all 0 in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Decrease-by-Half Algorithm We can solve the same problem using a decrease-by-half algorithm. This...

    Convert the pseudocode into a C++ function Decrease-by-Half Algorithm We can solve the same problem using a decrease-by-half algorithm. This algorithm is based on the following ideas: In the base case, n 1 and the only possible solution is b 0, e 1 In the general case, divide V into a left and rnight half; then the maximum subarray can be in one of three places: o entirely in the left half; o entirely in the right half; or o...

  • The task was to find the recurrence relation for this function and then find the complexity...

    The task was to find the recurrence relation for this function and then find the complexity class for it as well. Provided is my work and the function. My question is, I feel like I'm missing some step in the recurrence relation and complexity class. Is this correct? The following code is in JavaScript. function divideAndConquerSum(x){ if(x.length<1){ return 0; } if(x.length == 1){ return x[0]; } var third = Math.floor((x.length-1)/3); var next = (third *2)+1; var y = x.slice(0, third+1);...

  • Weird recursion tree analysis. Suppose we have an algorithm that on problems of size n, recursively...

    Weird recursion tree analysis. Suppose we have an algorithm that on problems of size n, recursively solves two problems of size n/2, with a “local running time” bounded by t(n) for some function t(n). That is, the algorithm’s total running time T(n) satisfies the recurrence relation T(n) ≤ 2T(n/2) + t(n). For simplicity, assume that n is a power of 2. Prove the following using a recursion tree analysis (a) If t(n) = O(n log n), then T(n) = O(n(log...

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • Selection Sort is a common algorithm to sort the elements of an array. First, it scans...

    Selection Sort is a common algorithm to sort the elements of an array. First, it scans the elements of the array to find the smallest value and places it at index 0, thus creating a sorted “subarray” of size 1 that contains the smallest value. Then it scans the remaining unsorted values for the new smallest value and places it at index 1, creating a sorted subarray of size 2 that contains the 2 smallest values. It continues in this...

  • use javascript please Complete the function below using recursion. Assume the parameter is a positive integer....

    use javascript please Complete the function below using recursion. Assume the parameter is a positive integer. The function will return an array that contains all of the numbers from 1 up to the parameter in consecutive order. For example, if the parameter is the value 5 then the return value should be the array [1, 2, 3,4 function makeSequence(value) t\ please use recursion thank von

  • Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array...

    Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array of integers q ≤ r and q,r ∈ N. Postcondition: Returns the smallest element of A[q, ... , r]. 1: function Smallest (A , q , r) 2: if q = r then 3: return A[q] 4: else 5: mid <--- [q+r/2] 6: return min (Smallest(A, q, mid), Smallest (A, mid + 1, r)) 7: end if 8: end function (a) Write a recurrence...

  • Design and implement a Θ(n) algorithm that will simultaneously find the largest and second-largest elements (integers)...

    Design and implement a Θ(n) algorithm that will simultaneously find the largest and second-largest elements (integers) in an array. Note that the second largest element should be distinctly smaller than the largest element. You should also adhere to the following restrictions. (1) You should traverse through the array ONLY ONCE. (2) The array could have both positive and negative elements. So, you should NOT initialize any temporary variables to very small negative values as part of your algorithm. (3) You...

  • 2. Consider the following algorithm. Assume arrays are 0-indexed. Assume list indices are rounded to the...

    2. Consider the following algorithm. Assume arrays are 0-indexed. Assume list indices are rounded to the nearest integer. ܘ̄ ܘ̈ܐ ;ܢ ܞ 1: function ODDSUM(array A) 2: if length(A) == 1: return 0 else: Al + A[0 : length(A)/2] A2 – Aſlength(A)/4 : 3 * length(A)/4] A3 + Aſlength(A)/2 : length(A)] return OddSum(A1) + OddSum(A2) + OddSum(A3) 9: end function F ܞ a Write the running time of this function as a recurrence relation. Assume that the array initializations on...

  • (13 pts) Given an array AlI,2,. .. ,n] integers, design and analyze an efficient Divide-and-Conqu...

    (13 pts) Given an array AlI,2,. .. ,n] integers, design and analyze an efficient Divide-and-Conquer algorithm to find some i and j, where j > 1, such that A[j]-Ali] is maximized. For example, given A 6, 1,3,8,4,5, 12,6], the maximum value of AL] - Ali] for j > i is 12-1 11 where j -7 and i 2. Give the underlying recurrence relation for your algorithm and analyze its running time. You should carefully state all details of your algorithm:...

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
ADVERTISEMENT