Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Consider the problem of finding the majority element of an array. ( Leetcode question 1 6 9 ) I.e . given, say, the array A
Consider the problem of finding the majority element of an array. Leetcode question I.e given, say, the array A abcacaa the majority element is the character aTo clarify, the majority element is not something which occurs the most that would be a plurality. By majority, we mean an element which occurs in more than half of the array. An array need not have a majority element at all. In this case, we will return
a Write a bruteforce python function for calculating and returning the majority element. Note that the elements of A are not assumed to be numbers they could be anything at all. Your bruteforce solution should not use dictionaries arrays only. Well come back to this question when weve introduced hash tables in a little bit What is the runtime?
b Create a divide and conquer algorithm for calculating and returning the majority element in time Onlogn Explain in writing how and why your algorithm works. Justify using the master theorem that this is the runtime, and then create a compare with theoryalgo theo function similar to what you used in assignment in order to demonstrate it empirically. Hint: Split the array into two halves. If A has a majority element, how does that element relate to the two halves? Another tip: Use the master theorem to compute how much computation time you can afford per level of recursion.
c If your algorithm relies on a claim relating majority elements of sets to majority elements of their subsets, then you must prove that claim using induction on the size of the set.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started