Question

In: Computer Science

Given a positive integer k and an array A[1..n] that contains the quiz scores of n...

Given a positive integer k and an array A[1..n] that contains the quiz scores of n students in ascending order, design a divide and conquer algorithm to efficiently count the number of students that have quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. Let group i be the set of students with quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. The counting result should be stored in G[1..k], i.e. G[i] stores the number of students in group i. The elements in G is initialized to 0. You have the following constraints:

(i) You cannot directly access A[j] for 1 ≤ j ≤ n and G[i] for 1 ≤ i ≤ k.

(ii) A function Compare(s, t) is given for returning a boolean value in O(1) time. It returns true if A[s] and A[t] are in the same group, it returns false otherwise. Each index can only be used in Compare(s, t) for at most once, e.g. if Compare(1, 4) is called, then Compare(1, 3), Compare(3, 1), Compare(3, 4) and Compare(4, 3) cannot be called.

(iii) A procedure Increase(j, val) is given for updating an element of G in O(1) time. It computes the group that the student with score A[j] belongs to, and it increases the corresponding element of G by val. E.g. if A[j] in (100(i − 1)/k, 100i/k], then G[i] is increased by val. Similar to (ii), each index j can only be used in Increase(j, val) for at most once. Note that Increase(j, val) has no return value.

Violating any one of the above constraints will cause the algorithm fail.

Your algorithm should run in O(k log n) time. You can assume the maximum score is 100 and no student get zero mark.

Writing down algorithms in pseudocode or C++.

Solutions

Expert Solution

Look at the examples, we can see that the rotation value is equal to the index of the lower objects. A simple line solution is to find a small object and retrieve its index.

implementation of the C ++ concept.

/ Returns the calculation of the exchange of the same members

// is sorted first by ascending, then rotated

int countRotations (int arr [], int n)

{

// Basically we get a minimum indication

// feature

int min = arr [0], min_index;

because (int i = 0; i <n; i ++)

{

uma (min> arr [i])

{

minute = editing [i];

min_index = i;

}

}

replace min_index;

}

Here, again we find an index of low-level items, but we use Binary Search. The concept is based on the following facts:

The bottom line is that the only thing that has passed is the greatest. If nothing is over, then there is no rotation (first thing minimum). We assess this situation with a medium object by comparing it with (mid-1) ‘th and (mid + 1)’ th element.
If the smallest element is not in the middle (or middle or middle + 1), then the least of the objects lie in the left or right side.
If the middle object is smaller than the last item, then the lower object lies on the left side
The other little thing lies in the right half.
// Returns the calculation of the exchange of the same members

// is sorted first by ascending, then rotated

int countRotations (int arr [], int low, int high)

{

// This condition is required to handle this case

// when the list can be rotated at all

if (high <low)

return 0;

// With only one item left

if (high == low)

back down;

// Find in the middle

int mid = low + (high - low) / 2; / * (bottom + top) / 2; * /

// Check if the element (center + 1) is a small object.

// Think of cases like {3, 4, 5, 1, 2}

if (middle <up && arr [middle + 1] <arr [mid])

return (center +1);

// See if the center itself is a small object

if (middle> low && arr [mid] <arr [middle 1])

go back in the middle;

// Decide if we need to go left or

// half food

uma (arr [high]> arr [mid])

return count (sorting, low, medium 1);

return count (editing, middle + 1, top);

}

// Call code

int main ()

{

int arr [] = {15, 18, 2, 3, 6, 12};

int n = sizeof (arr) / sizeof (arr [0]);

cout << countRotations (arr, 0, n-1);

return 0;

}


Related Solutions

Let A[1..n] be an array of distinct positive integers, and let t be a positive integer....
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer. (a) Assuming that A is sorted, show that in O(n) time it can be decided if A contains two distinct elements x and y such that x + y = t. (b) Use part (a) to show that the following problem, re- ferred to as the 3-Sum problem, can be solved in O(n2) time: 3-Sum Given an array A[1..n] of distinct positive integers, and...
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We...
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We want to return the k smallest element of A[1.....n], in non-decreasing order. For example: A = [5, 4, 6, 2, 10] and k = 4, the algorithm returns [2, 4, 5, 6]. There are at least the following four approaches: a. heapify A and then extract k elements one by one b. sort the array (e.g. using MergeSort or HeapSort) and then read the...
Show that there is only one positive integer k such that no graph contains exactly k...
Show that there is only one positive integer k such that no graph contains exactly k spanning trees.
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...
that, given an integer N and an integer K, returns the minimum number of rounds that...
that, given an integer N and an integer K, returns the minimum number of rounds that are necessary for John to leave the casino with N chips, having played all-in no more than K times.
Suppose you are given an integer c and an array, A, indexed from 1 to n,...
Suppose you are given an integer c and an array, A, indexed from 1 to n, of n integers in the range from 0 to 5n (possibly with duplicates). i.e. 0 <= A[i ] <= 5n " I = {1, .., n}. a.) Write an efficient algorithm that runs in O(n) time in a pseudo code for determining if there are two integers, A[i] and A[j], in A whose sum is c, i.e. c = A[i] + A[j], for 1...
A positive integer N is a power if it is of the form q^k, where q,...
A positive integer N is a power if it is of the form q^k, where q, k are positive integers and k > 1. Give an efficient algorithm that takes as input a number N and determines whether it is a square, that is, whether it can be written as q^2 for some positive integer q. What is the running time of your algorithm? write the pseudocode for the algorithm.
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether we can use numbers from c[0...n − 1] to make a sum of v, allowing any particular number in the list to be used multiple times. Or, mathematically, this means that there exists non-negative integer coefficients, x0, x1, ..., xn−1, such that v = x0c[0] + x1c[1] + ...xn−1c[n − 1]. For example, given c[0...3] = {2, 4, 6, 10}, and v = 17,...
Problem: Given an integer array consisting of only 0’s and 1’s and a value k that...
Problem: Given an integer array consisting of only 0’s and 1’s and a value k that denotes distance, determine if all the 1’s are at least k spaces away from each other. Details: The distance between each 1 in the array can be greater than or equal to k places. It doesn’t have to be exactly k Assume the array will only contain 0’s and 1’s – no need to do validation checking If k is greater than the size...
Given that the square matrix, A is nilpotent (Ak = 0 for some positive integer k)....
Given that the square matrix, A is nilpotent (Ak = 0 for some positive integer k). If A is n by n, show that An = 0.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT