Question

Suppose you have an array S indexed from 1 to n which contains n numbers, not in any particular order, and you wish to count

int CountOccur (int i,j) { int mid, count; if j <i return 0 else mid Floor (j)/2) if S[mid] x Count 1 CountOccur(i,mid-1) Cou

Suppose you have an array S indexed from 1 to n which contains n numbers, not in any particular order, and you wish to count how many times a given number x occurs in S. Consider the recursive algorithm below for this which finds the number of occurrences of x in the index range i...j in S. Of course, solving the problem would involve an initial call to the algorithm for the range 1.n:
int CountOccur (int i,j) { int mid, count; if j
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

int countOccur(int S[], int x, int n){
   int count = 0;
   for(int i = 0;i<n;i++){
       if(S[i] == x)
           count++;
   }
   return count;
  
}

In the above code, S is the array which contains all the digits, x is the one with which we want to match, n is the number of items inside the array S.

2. [2, 3, 6 , 2, 7, 8, 2, 9] x = 2

So here n = 8

So we need to check for each value of ranging from 0 to 7

i = 0, match, count = 1

i = 1, not match

i = 2, not match

i = 3, match, count = 2

i = 4, not match

i = 5 not match

i = 6 match, count = 3

i = 7 not match

return count as 3, 3 is the answer from the illustration.

3. For the following problem, time complexity would be O(n), where n is the number of elements in the array.


Friend: If you have any doubts regarding the solution to the problem, do let me know in the comment section.
Thanks

Add a comment
Know the answer?
Add Answer to:
Suppose you have an array S indexed from 1 to n which contains n numbers, not...
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
  • 4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up t...

    4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...

  • This is an assignment for my algorithm class which I have written the code partially and...

    This is an assignment for my algorithm class which I have written the code partially and only need to complete it by adding a time function that calculates the average running time. We could use any programming language we want so I am using C++. I am including the instruction and my partial code below. Thank you! Implement linearSearch(a,key) and binarySearch( a,key)functions. Part A.In this part we will calculate theaverage-case running time of each function.1.Request the user to enter a...

  • I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is...

    I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is not limited to finding only the median, but can generally find the ith element with 0≤i <n. Implement this generic version of the median algorithm by creating a class selector in the ads.set2.select package and implementing the following method: /** * Returns the...

  • Write a program that can: 1. Insert twenty random numbers into a linked list. The numbers...

    Write a program that can: 1. Insert twenty random numbers into a linked list. The numbers should be within a range (E.g., 1 to 7). The user should be prompted to enter the minimum number and maximum number of the range. Each number should be inserted at the end of the list. Section 7.8 of the textbook covers the random number generator. Examples of how to use the random number generator are in Fig 7.6 and 7.7. Here is a...

  • 1) can any one please givem the code for this a) If n is a power...

    1) can any one please givem the code for this a) If n is a power of 2, as it is in Figure 9-3, you would merge pairs of individual entries, starting at the beginning of the array. Then you would return to the beginning of the array and merge pairs of twoentry subarrays. Finally, you would merge one pair of four-entry subarrays. Notice that the subarrays in each pair of subarrays contain the same number of entries. In general,...

  • Let A[1..n] be an array with n elements. Consider the Count-Occurrence algorithm with the pseudocode below....

    Let A[1..n] be an array with n elements. Consider the Count-Occurrence algorithm with the pseudocode below. Count-Occurrence(A, n, k). count 0 for j = 1 to n if A[j] ==k count = count+1 print count Which of the following is the correct loop invariant for the for loop? At the start of each iteration jof the for loop, count represents the number of times that k occurs in the subarray A[1.j]. At the start of each iteration of the for...

  • C++, THIS IS FOR AN UNSORTED ARRAY. I'm trying to implement an "if" statement that will...

    C++, THIS IS FOR AN UNSORTED ARRAY. I'm trying to implement an "if" statement that will check if there are 2 or more modes in a data set. I tried checking "if (count == total)" then it would increment "pos" by 1 and put the other mode in the list (m[pos-1] = values[index]), but it kept giving me the same number multiple times. For instance, if the data set was {1,5,10,4,7,3,8,3,9,10} (the 3 and 10 occur twice) then i would...

  • you will analyse two algorithms for finding the median of an array of integers. You will...

    you will analyse two algorithms for finding the median of an array of integers. You will compare both algorithms in terms of timing, and hopefully design a hybrid algorithm that uses both, depending on input size. You will write a report describing your experiments and results. the following Java program implements two algorithms for finding the median of an array of integers. The first uses merge sort, and the other implements the recursive linear time selection algorithm, the task is...

  • Suppose you have a sorted array of positive and negative integers and would like to determine...

    Suppose you have a sorted array of positive and negative integers and would like to determine if there exist some value of x such that both x and -x are in the array. Consider the following three algorithms: Algorithm #1: For each element in the array, do a sequential search to see if its negative is also in the array. Algorithm #2:For each element in the array, do a binary search to see if its negative is also in the...

  • Add reverse() method which reverses the content of array without using additional array. rotate(k) method which...

    Add reverse() method which reverses the content of array without using additional array. rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete is successful returns 1, otherwise 0; * for successful deletion:...

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