Alex loves politics. This year he decided to be the General
Secretary of the College Union Body. There are N hostels in
college and the elections will last the next L days. Elections'
format is similar to the USA's presidential election that means
elections will be held hostel wise and whoever wins the most
hostels among all candidates, wins the election.
His friends Adam and Bob are helping him. Adam has collected
some confidential information (He has his methods) regarding
the starting day of campaigning for each hostel. Bob has
calculated the minimum number of days of campaigning
needed for each hostel bi that guaranteeing Alex's victory in that
hostel.
Let's denote that data for each hostel by ai and bi for i-th hostel.
(1<=İ<=N).
Alex can not campaign in two hostels simultaneously. Also, he
has to campaign continuously bi days to win i-th Hostel.
Find the maximum number of hostels Alex can win during the
election's time.
Answers
Answer:
the starting day of campaigning for each hostel. Bob has
calculated the minimum number of days of campaigning
needed for each hostel bi that guaranteeing Alex's victory in that
hostel.
Let's denote that data for each hostel by ai and bi for i-th hostel.
(1<=İ<=N).
Alex can not campaign in two hostels simultaneously. Also, he
has to campaign continuously bi days to wink
Explanation:
MARK AS BRAINLIEST
Answer:
Program is given for the given scenario.
Explanation:
Given an array of names of candidates in an election. A candidate’s name in the array represents a vote cast on the candidate. Print the name of candidates who received the maximum vote. If there is a tie, print a lexicographically smaller name.
Examples:
Input : votes[] = {"john", "johnny", "jackie",
"johnny", "john", "jackie",
"jamie", "jamie", "john",
"johnny", "jamie", "johnny",
"john"};
Output : John
We have four Candidates with name as 'John',
'Johnny', 'jamie', 'jackie'. The candidates
John and Johny get maximum votes. Since John
is alphabetically smaller, we print it.
A simple solution is to run two loops and count the occurrences of every word. The time complexity of this solution is O(n * n * MAX_WORD_LEN).
An efficient solution is to use Hashing. We insert all votes in a hash map and keep track of counts of different names. Finally, we traverse the map and print the person with the maximum votes.
Program
// Java program to find winner in an election.
import java.util.*;
public class ElectoralVotingBallot
{
/* We have four Candidates with name as 'John',
'Johnny', 'jamie', 'jackie'.
The votes in String array are as per the
votes casted. Print the name of candidates
received Max vote. */
public static void findWinner(String votes[])
{
// Insert all votes in a hashmap
Map<String,Integer> map =
new HashMap<String, Integer>();
for (String str : votes)
{
if (map.keySet().contains(str))
map.put(str, map.get(str) + 1);
else
map.put(str, 1);
}
// Traverse through map to find the candidate
// with maximum votes.
int maxValueInMap = 0;
String winner = "";
for (Map.Entry<String,Integer> entry : map.entrySet())
{
String key = entry.getKey();
Integer val = entry.getValue();
if (val > maxValueInMap)
{
maxValueInMap = val;
winner = key;
}
// If there is a tie, pick lexicographically
// smaller.
else if (val == maxValueInMap &&
winner.compareTo(key) > 0)
winner = key;
}
System.out.println(winner);
}
// Driver code
public static void main(String[] args)
{
String[] votes = { "john", "johnny", "jackie",
"johnny", "john", "jackie",
"jamie", "jamie", "john",
"johnny", "jamie", "johnny",
"john" };
findWinner(votes);
}
}