Question

In: Computer Science

Suppose that all the numbers in an array are located in an interval [0,12], and we...

Suppose that all the numbers in an array are located in an interval [0,12], and we need to find the largest element with accuracy ε = 0.8. How many iterations will you need if we use the quantum optimization algorithm? How many times do we need to apply Grover's algorithm? Trace the quantum optimization algorithm for the case when the actual largest element is a5 = 3.14

Solutions

Expert Solution

#include<iostream> 
using namespace std; 

// A structure to store an element and its current count 
struct eleCount 
{ 
        int e; // Element 
        int c; // Count 
}; 


void moreThanNdK(int arr[], int n, int k) 
{ 
        // k must be greater than 1 to get some output 
        if (k < 2) 
        return; 

        
        struct eleCount temp[k-1]; 
        for (int i=0; i<k-1; i++) 
                temp[i].c = 0; 

        
        for (int i = 0; i < n; i++) 
        { 
                int j; 

                /* If arr[i] is already present in 
                the element count array, then increment its count */
                for (j=0; j<k-1; j++) 
                { 
                        if (temp[j].e == arr[i]) 
                        { 
                                temp[j].c += 1; 
                                break; 
                        } 
                } 

                /* If arr[i] is not present in temp[] */
                if (j == k-1) 
                { 
                        int l; 
                        
                        /* If there is position available in temp[], then place 
                        arr[i] in the first available position and set count as 1*/
                        for (l=0; l<k-1; l++) 
                        { 
                                if (temp[l].c == 0) 
                                { 
                                        temp[l].e = arr[i]; 
                                        temp[l].c = 1; 
                                        break; 
                                } 
                        } 

                        /* If all the position in the temp[] are filled, then 
                        decrease count of every element by 1 */
                        if (l == k-1) 
                                for (l=0; l<k; l++) 
                                        temp[l].c -= 1; 
                } 
        } 

        
        for (int i=0; i<k-1; i++) 
        { 
                // Calculate actual count of elements 
                int ac = 0; // actual count 
                for (int j=0; j<n; j++) 
                        if (arr[j] == temp[i].e) 
                                ac++; 

                // If actual count is more than n/k, then print it 
                if (ac > n/k) 
                cout << "Number:" << temp[i].e 
                                << " Count:" << ac << endl; 
        } 
} 

int main() 
{ 
        cout << "First Test\n"; 
        int arr1[] = {4, 5, 6, 7, 8, 4, 4}; 
        int size = sizeof(arr1)/sizeof(arr1[0]); 
        int k = 3; 
        moreThanNdK(arr1, size, k); 

        cout << "\nSecond Test\n"; 
        int arr2[] = {4, 2, 2, 7}; 
        size = sizeof(arr2)/sizeof(arr2[0]); 
        k = 3; 
        moreThanNdK(arr2, size, k); 

        cout << "\nThird Test\n"; 
        int arr3[] = {2, 7, 2}; 
        size = sizeof(arr3)/sizeof(arr3[0]); 
        k = 2; 
        moreThanNdK(arr3, size, k); 

        

        return 0; 
} 

Related Solutions

Find the median (middle value) of an array of numbers, which we will assume are all...
Find the median (middle value) of an array of numbers, which we will assume are all distinct. For example, if there are 27 elements, then you must return the 14th smallest (which is also the 14th largest). The strategy used to solve this problem is somewhat like the quicksort algorithm, but has some important differences. Consider choosing a pivot element and partitioning the input so that the elements less than the pivot are to its left, and those larger than...
Suppose we have an array A that contains a prime numbers from 2 to 200 in...
Suppose we have an array A that contains a prime numbers from 2 to 200 in sorted order. How many items of the array A would the binary search algorithm have to examine before concluding that 60 is not in the array A? 30 200 100 6 2- Suppose we have an array that contains 182 village name. These names are sorted alphabetically. How many names would binary search algorithm examine to locate a particular name in the array, in...
Given an array A of n distinct real numbers, we say that a pair of numbers...
Given an array A of n distinct real numbers, we say that a pair of numbers i, j ∈ {0, . . . , n−1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Answer the following: (a) How small can the number of inversions be? Give an example of an array of length n with the smallest possible number of inversions. (b)...
Given an array A of n distinct numbers, we say that a pair of numbers i,...
Given an array A of n distinct numbers, we say that a pair of numbers i, j ∈ {0, . . . , n − 1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Define the Inversion problem as follows: • Input: an array A consisting of distinct numbers. • Output: the number of inversions of A, i.e. |inv(A)|. Answer the following:...
Project: Given an array numbers. We define a running sum of an array as runningSum[i] =...
Project: Given an array numbers. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of numbers. Example: Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]. You need to do: Create a class called RunningSumArray to perform the main method. Create a class called ArrayOperationto hold the actions for array. In this project, you will need 4methods: 1. Public static Int[] getArray() --- inside ArrayOperationclass,...
We have an array of numbers, and we start at index 0. At every point, we're...
We have an array of numbers, and we start at index 0. At every point, we're allowed to jump from index i to i+3, i+4, or stop where we are. We'd like to find the maximum possible sum of the numbers we visit. For example, for the array [14, 28, 79, -87, 29, 34, -7, 65, -11, 91, 32, 27, -5], the answer is 140. (starting at the 14, we jump 4 spots to 29, then 3 spots to 65,...
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending...
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. I need this in java language.
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in...
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. (Write a C# program)
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending...
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. I need this in java language. Radix Sort assignment (CISP430 ignore this part) This is only for those who are using Java How many classes do I need? A: Node, Queue, Radix, Driver...
Given an array A[1..n] of integers - all of whose numbers are in the range [0,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0, n^3 − 1] give an algorithm which sorts them in O(n) time.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT