Question

In: Computer Science

Using Java, Given an array A[0 ... n-1], where each element of the array represent a...

Using Java,

Given an array A[0 ... n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.

Solutions

Expert Solution


  
import java.io.*;
  
class Main{

static void findWinner(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)
System.out.println ("Candidate with id:"+arr[index]+" won the election");
  
else
System.out.println ("It's a tie between candidates");
} //complexity O(n*n)
  
// Driver code
public static void main (String[] args) {
  
int arr[] = {1, 1, 2, 1, 3, 5, 1};
int n = arr.length;
  
// Function calling
findWinner(arr, n);
}

}

output:

Candidate with id:1 won the election


Related Solutions

Problem 1: Given an array A[0 ... n-1], where each element of the array represents a...
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a vote in the election. Assume that each vote is given as integers representing the ID of the chosen candidate. Write the code determining who wins the election. Problem 2: How do we find the number which appeared maximum number of times in an array? ( Use Java and an original code )
Need these written in Java script please Problem 1: Given an array A[0 ... n-1], where...
Need these written in Java script please Problem 1: Given an array A[0 ... n-1], where each element of the array represents a vote in the election. Assume that each vote is given as integers representing the ID of the chosen candidate. Write the code determining who wins the election. Problem 2: How do we find the number which appeared maximum number of times in an array?
java You are creating an array where each element is an (age, name) pair representing guests...
java You are creating an array where each element is an (age, name) pair representing guests at a dinner Party. You have been asked to print each guest at the party in ascending order of their ages, but if more than one guests have the same age, only the one who appears first in the array should be printed. Example input: (23, Matt)(2000, jack)(50, Sal)(47, Mark)(23, will)(83200, Andrew)(23, Lee)(47, Andy)(47, Sam)(150, Dayton) Example output: (23, Matt) (47, Mark) (50, Sal...
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
You are given a set S of n real numbers where for each element x ∈...
You are given a set S of n real numbers where for each element x ∈ S, 1 < | x | < 10. You are also given a black box that returns true if there is some combination (x, y, z) ∈ S such that x · y = z. Say the black box returns only z. Write an O(n log n) algorithm to find x and y. In other words, given a set S with n real numbers...
Suppose that each row of an n × n array A consists of 1’s and 0’s...
Suppose that each row of an n × n array A consists of 1’s and 0’s such that, in any row of A, all the 1’s come before any 0’s in that row. Assuming A is already in memory, describe a method running in O(n log n) time (not O(n2) time!) for counting the number of 1’s in A.
Given an array A[1..n] of integers - all of whose numbers are in the range [0,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0, n^3 − 1] give an algorithm which sorts them in O(n) time.
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
Write a program in Java with a Scanner. Given an array and a number k where...
Write a program in Java with a Scanner. Given an array and a number k where k is smaller than the size of the array, write a program to find the k'th smallest element in the given array. It is given that all array elements are distinct. Example: Input: arr[] = {7,10,4,3,20,15} k = 3 Output: 7
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.Input:    6    2 3 4 11 22 320    where:First line represents the number of elements in the array.Second line represents the elements in the array.Output:    2 3 4 11 22Explanation: Element of the array 320 is the only one in the array which is a multiple of 5, so it is removed from the array.Assumptions:Array can be of size...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT