Question

In: Computer Science

How do you write a java program that implements Majority-Element using Boyer & Moore approach?

How do you write a java program that implements Majority-Element using Boyer & Moore approach?

Solutions

Expert Solution

CODE

import java.util.Scanner;

public class Main

{

public static void printMajorityElement(int a[])

{

int size = a.length;

int cand = findCandidate(a, size);

if (isMajority(a, size, cand))

System.out.println("Majority element is: " + cand);

else

System.out.println("-1");

}

public static int findCandidate(int a[], int size)

{

int maj_index = 0, count = 1;

int i;

for (i = 1; i < size; i++)

{

if (a[maj_index] == a[i])

count++;

else

count--;

if (count == 0)

{

maj_index = i;

count = 1;

}

}

return a[maj_index];

}

public static boolean isMajority(int a[], int size, int cand)

{

int i, count = 0;

for (i = 0; i < size; i++)

{

if (a[i] == cand)

count++;

}

if (count > size / 2)

return true;

else

return false;

}

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

int n;

System.out.println("Enter the number of elements: ");

n = sc.nextInt();

int a[] = new int[n];

System.out.println("Enter the array elements separated by space: ");

for (int i=0; i<n; i++) {

a[i] = sc.nextInt();

}

printMajorityElement(a);

sc.close();

}

}


Related Solutions

To understand the value of counting loops: Write a java program that implements matrix multiplication using...
To understand the value of counting loops: Write a java program that implements matrix multiplication using counting loop constructs. Then write the same program using only logical loops—for example, while loops. Write 2 classes for each program Sample Result is shown below: Enter the number of rows of matrix A: 2 Enter the number of columns of matrix A: 3 Enter the number of columns of matrix B: 3 Enter the number of columns of matrix B: 4 Enter matrix...
To understand the value of counting loops: Write a java program that implements matrix multiplication using...
To understand the value of counting loops: Write a java program that implements matrix multiplication using counting loop constructs. Then write the same program using only logical loops—for example, while loops. Sample Result is shown below: Enter the number of rows of matrix A: 2 Enter the number of columns of matrix A: 3 Enter the number of columns of matrix B: 3 Enter the number of columns of matrix B: 4 Enter matrix A; 1 2 3 4 5...
Write a Java program that implements a queue in a hospital. I want your program to...
Write a Java program that implements a queue in a hospital. I want your program to ask the user to enter the number of patients then enter the patient number starting from 110 till the end of the queue then print number of patients waiting in the queue. Suppose you have a queue D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty Stack S. Give a code fragment that uses S, to store...
Write a JAVA program that implements the following disk-scheduling algorithms: FCFS
Write a JAVA program that implements the following disk-scheduling algorithms: FCFS
Write a program that implements the follow disk scheduling algorithms. You can use C or Java...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java for this assignment. FCFS SSTF SCAN C-SCAN LOOK C-LOOK Your program will service a disk with 5000 cylinders (numbered 0 to 4999). Your program will generate a random initial disk head position, as well as a random series of 1000 cylinder requests, and service them using each of the 6 algorithms listed above. Your program will report the total amount of head movement required...
Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85....
Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85.                                                                                                                                                                    Q2.Write a Java program to find element 54 in array 45,41,65,53,76,90 using Binary Search. (Hint: Binary search is applied to sorted array elements) Q4.   Write a java program to create array list subject        - add English, Maths, Science to the list        - add Computers at index 2        - display first occurrence index of Maths        - traverse the list using...
SHOW WORK . Using the Boyer-Moore algorithm, find the Bad Match table for the below patterns.                           
SHOW WORK . Using the Boyer-Moore algorithm, find the Bad Match table for the below patterns.                                                                                                   (8 points) i) Pattern1:   AABCACCCA ii) Pattern 2: CCCAAABBD iii) Pattern3:  ABCABCBAD iv) Pattern4:   BSDGSBAA SHOW WORK
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. The program will generate a random series of 50 requests and service them according to each of the algorithms you chose. The program will be passed the initial position of the disk head as a parameter on the command line and report the total amount of head...
Write a Java program (use JDBC to connect to the database) that implements the following function...
Write a Java program (use JDBC to connect to the database) that implements the following function (written in pseudo code): (20 points) CALL RECURSION ( GIVENP# ) ; RECURSION: PROC ( UPPER_P# ) RECURSIVE ; DCL UPPER_P# ... ; DCL LOWER_P# ... INITIAL ( ' ' ) ; EXEC SQL DECLARE C CURSOR FOR SELECT MINOR_P# FROM PART_STRUCTURE WHERE MAJOR_P# = :UPPER_P# AND MINOR_P# > :LOWER_P# ORDER BY MINOR_P# ; print UPPER_P# ; DO "forever" ; EXEC SQL OPEN C...
USING JAVA and NOTTT using BufferedReader or BufferedWriter Write an application that implements a simple text...
USING JAVA and NOTTT using BufferedReader or BufferedWriter Write an application that implements a simple text editor. Use a text field and a button to get the file. Read the entire file as characters and display it in a TextArea. The user will then be able to make changes in the text area. Use a Save button to get the contents of the text area and write that over the text in the original file. Hint: Read each line from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT