Answered step by step
Verified Expert Solution
Link Copied!

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 169) I.e. given, say, the array A =[a,b,c,a,c,a,a], the majority element is the character a.(To 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 1.
(a) Write a brute-force 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 brute-force 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 O(nlog(n)). 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 theory(algo, theo) function similar to what you used in assignment 2 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions

Question

1) 2) 3) 4)

Answered: 1 week ago

Question

Ty e2y Evaluate the integral dy

Answered: 1 week ago