Question

In: Computer Science

We are given a non sorted array so that the values are not pairwise disjoint (the...

We are given a non sorted array so that the values are not pairwise disjoint (the same values may appear in many entries). Say that we can find the k-th smallest element number in the array in time O(n) for any 1 <= k <= n. Give an algorithm that finds if there is a value that appears at least n/3 times.

Please explain the algorithm in words and analyze the run time.

Solutions

Expert Solution

The algorithm is based upon moore voting algorithm to find majority element in an array.

Algorithm->

NBY3COUNT(arr[1..n], n)

count1 = 0, count2 = 0 //simple counter for candidates

first=second=None //there can be at max two elements with at least count>n/3 these are candidates

    for i = 1 to n do

        // if this element is previously seen, increment count1.

        if first = arr[i] then count1++;

        // if this element is previously seen, but not same as first, increment count2.

        else if second = arr[i] then count2++;

//if we haven't assigned first number or previously assigned may not be correct

        else if count1 = 0 then count1++ ,  first = arr[i]

//same for second number

        else if count2 = 0) then count2++, second = arr[i];

        // if current element is different from both the previously seen variables,then decrement both the counts.

        else  count1--,   count2--

end //for loop

    count1 = count2 = 0 //counting the occurrence of first and second

    // Again traverse the array and find the actual counts (verifying if the numbers exist).

    for i=1 to n do

        if arr[i] = first then count1++;

        else if arr[i] = second then count2++;

end //for loop

  

    if count1 > n / 3 or count2> n/3 the return TRUE

return FALSE

end //function

------------------------------------------

Time Complexity O(n) two linear passes only through the array


Related Solutions

Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Input: arr = [1,2,2,6,6,6,6,7,10] Output:
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the...
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the help of : (a) insertion sort; (b) selection sort; (c) bubble sort with swaps counting; (d) bubble sort without swaps counting subject design and analysis of algorithms answer should be like eg p u r p l e p r u p l e
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...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
//   Given an array of size 9, with the values of 1-9, determine if the array...
//   Given an array of size 9, with the values of 1-9, determine if the array //   is valid or not. //   Display a message stating the row is VALId, or state its INVALID and what //   was the index number that determined the data invalid. // //   Use this java code as a start of your code. //   Test the array data by changing the values. //============================================================================= import java.util.*;    public class Soduko_ValidateRow    { public static void main(String...
Given a sorted array with lot of duplicates, write a problem to search specific element m....
Given a sorted array with lot of duplicates, write a problem to search specific element m. If it’s included in the A, return its minimal index, otherwise return -1. For example A = {0, 0, 1, 1, 1, 2, 3, 3, 4, 4, 4, 4, 4}, if we search 4, you program should return 8. You are only allowed to use binary search. Linear search is not allowed here.
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice...
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1: Given nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. It doesn't matter what you leave beyond the returned...
Q4: . The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20,...
Q4: . The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20, 24, 25, 30, 32, 40, 45, 50, 52, 57, 60. How many recursive calls are made by our binarySearch method given an initial invocation of binarySearch(45, 0, 15)? Answer Choices : 2     0     3     4     1
Given the values of So given below in J/mol K and the values of ΔHfo given...
Given the values of So given below in J/mol K and the values of ΔHfo given in kJ/mol, calculate the value of ΔGo in kJ for the combustion of 1 mole of propane to form carbon dioxide and gaseous water at 298 K. S (C3H8(g)) = 274 S (O2(g)) = 209 S (CO2(g)) = 216 S (H2O(g)) = 181 ΔHfo (C3H8(g)) = -105 ΔHfo (CO2(g)) = -394 ΔHfo (H2O(g)) = -225
Q1: The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20, 24,...
Q1: The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20, 24, 25, 30, 32, 40, 45, 50, 52, 57, 60. How many recursive calls are made by our binarySearch method, given an initial invocation of binarySearch(25, 0, 15)? Answer Choices : 3     2     4     0     1 Q2: The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20, 24, 25, 30, 32, 40, 45, 50, 52, 57, 60....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT