Recall the pseudocode from lectures for identifying a good VLSI chip. The idea can be reused to finding the majority item in a list, where the majority is defined by the element in a list of size n which occurs at least n/2 + 1 times. Write a linear-time program to find the majority element in an array. What is the invariant of your loop? What is the variant of your loop? Explain why your design has linear time performance. Write JUnit tests to check your program.
First of all, we will discuss What is the invariant of the loop: -A loop invariant is a condition [among program variables] that is necessarily true immediately before and immediately after each iteration of a loop. By itself, a loop invariant doesn't do much. However, given an appropriate invariant, it can be used to help prove the correctness of an algorithm.
A majority element in an array A[] of
size n is an element that appears more than n/2 times (and hence
there is at most one such element).
Examples :
Input : {3, 3, 4, 2, 4, 4, 2, 4, 4}
Output : 4
Input : {3, 3, 4, 2, 4, 4, 2, 4}
Output : No Majority Element
The basic solution is to have two loops and keep track of the
maximum count for all different elements. If maximum count becomes
greater than n/2 then break the loops and return the element having
maximum count. If the maximum count doesn’t become more than n/2
then majority element doesn’t exist.
Below is the implementation of the above approach :
|
void find majority(int arr[], int n) { int maxCount = 0; int index = -1; // sentinels for(int i = 0; i < n; i++) { int count = 0; for(int j = 0; j < n; j++) { if(arr[i] == arr[j]) count++; }
// update maxCount if count of // current element is greater if(count > maxCount) { maxCount = count; index = i; } }
// if maxCount is greater than n/2 // return the corresponding element if (maxCount > n/2) cout << arr[index] << endl;
else cout << "No Majority Element" << endl; }
// Driver code int main() { int arr[] = {1, 1, 2, 1, 3, 5, 1}; int n = sizeof(arr) / sizeof(arr[0]);
// Function calling findMajority(arr, n);
return 0; } |
Time Complexity : O(n*n).
Recall the pseudocode from lectures for identifying a good VLSI chip. The idea can be reused...
I ONLY NEED PART 4 I HAVE DONE 1,2,3 Arrays This assignment is designed in small progressive parts, with the intention of developing the skill of working with arrays. Do each part separately. Include in your submission the code and output for Part I, then the code and output for Part 2, etc. into a Word document. Specification: Part 1. Write a main function that declares an array of 10 int’s. Assign each element in the array a value between...
I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...
Hello I need help with this program. Should programmed in C!
Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...
Lab 6 Instructions Objectives: • Executing and experimenting with programs that handle array related topics such as declaration, initialization, storing, retrieving, and processing elements. • Effectively manipulating and using ArrayList objects. • Becoming familiar with the use of arrays as method arguments and how array elements are passed to and returned from the called method. • Mastering the use of various debugging tools available on the Eclipse IDU. Important: EVERY one of the following Activities MUST be in a separate...
Question 8 A = 23n + 36n2 B = 6 + nlog2(n) + n C = log2n + 36n2 Which one of the following is correct? A. TA = O(n2) TB = O(n) TC = O(log2n) B. TA = O(n2) TB = O(nlog2(n)) TC = O(n2) C. TA = O(n2) TB = O(+ nlog2(n)) TC = O(log2n) D. TA = O(n2) TB = O(n) TC = O(n2) 0.5 points Question 9 Three criteria are used to determine whether a data structure is...
!!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...
Assignment Overview In Part 1 of this assignment, you will write a main program and several classes to create and print a small database of baseball player data. The assignment has been split into two parts to encourage you to code your program in an incremental fashion, a technique that will be increasingly important as the semester goes on. Purpose This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of...
Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler { /** * The number of consecutive shuffle steps to be performed in each call * to each sorting...
This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...
Please read the article bellow and discuss the shift in the
company's approach to genetic analysis. Please also discuss what
you think about personal genomic companies' approaches to research.
Feel free to compare 23andMe's polices on research with another
company's. Did you think the FDA was right in prohibiting 23andMe
from providing health information?
These are some sample talking points to get you thinking about
the ethics of genetic research in the context of Big Data. You
don't have to...