Parties and Seats
Problem Description​
Given two arrays, one is array of political parties namely - party, and their corresponding array of seats received - seats. You have to print the political parties in lexicographical sorted order along with their seats, and highest seats received by the party.
Input Format: The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains three lines of input. The first line contains number of parties N. The second line contains the names of the parties. The third line contains the votes corresponding to each party.
Output Format: For each testcase, in a new line, print the required answer.
User Task: Your task is to complete the function Election2019(party, seats, n) which accepts three arguments and prints the required result.
Examples​
Example 1:
Input:
1
7
A B C D E F G
90 150 33 23 17 500 2
Output:
A 90
B 150
C 33
D 23
E 17
F 500
G 2
500
Constraints​
1 ≤ T ≤ 100
1 ≤ N ≤ 26
1 ≤ seats ≤ 543
Solution for Parties and Seats​
Code in Different Languages​
- Python
- Java
- C++
def Election2019(party, seats, n):
# Combine party and seats into a list of tuples
v = [(party[i], seats[i]) for i in range(n)]
# Sort the list by party name
v.sort(key=lambda x: x[0])
# Find the maximum seats
max_seats = max(v, key=lambda x: x[1])[1]
# Print the sorted party and seats
for p, s in v:
print(f"{p} {s}")
# Print the maximum seats
print(max_seats)
# Example usage
party = ['A', 'B', 'C', 'A', 'B']
seats = [10, 20, 30, 15, 25]
n = len(party)
Election2019(party, seats, n)
import java.util.*;
public class Main {
public static void Election2019(char[] party, int[] seats, int n) {
// Create a list of pairs
List<AbstractMap.SimpleEntry<String, Integer>> v = new ArrayList<>();
for (int i = 0; i < n; i++) {
v.add(new AbstractMap.SimpleEntry<>(String.valueOf(party[i]), seats[i]));
}
// Sort the list by party name
v.sort(Comparator.comparing(AbstractMap.SimpleEntry::getKey));
// Find the maximum seats
int max_seats = Collections.max(v, Comparator.comparing(AbstractMap.SimpleEntry::getValue)).getValue();
// Print the sorted party and seats
for (AbstractMap.SimpleEntry<String, Integer> entry : v) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
// Print the maximum seats
System.out.println(max_seats);
}
public static void main(String[] args) {
char[] party = {'A', 'B', 'C', 'A', 'B'};
int[] seats = {10, 20, 30, 15, 25};
int n = party.length;
Election2019(party, seats, n);
}
}
void Election2019(char party[], int seats[], int n)
{
//Your code here
vector<pair<string, int>> v;
for (int i = 0; i < n; i++) {
string s(1, party[i]);
v.push_back({s, seats[i]});
}
sort(v.begin(), v.end());
int max_seats = max_element(v.begin(), v.end(), [](pair<string, int> a, pair<string, int> b) {
return a.second < b.second;
})->second;
for (auto x : v) {
cout << x.first << " " << x.second << endl;
}
cout << max_seats << endl;
}
References​
- GeekForGeeks Problem: Parties and Seats