In: Computer Science
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++.
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;
}