Hello coders, today we are going to solve Discrepancies in the Voters List CodeChef Solution whose Problem Code is VOTERS.
Task
As you might remember, the collector of Siruseri had ordered a complete revision of the Voters List. He knew that constructing the list of voters is a difficult task, prone to errors. Some voters may have been away on vacation, others may have moved during the enrollment and so on.
To be as accurate as possible, he entrusted the task to three different officials. Each of them was to independently record the list of voters and send it to the collector. In Siruseri, every one has a ID number and the list would only list the ID numbers of the voters and not their names. The officials were expected to arrange the ID numbers in ascending order in their lists.
On receiving the lists, the Collector realised that there were discrepancies – the three lists were not identical. He decided to go with the majority. That is, he decided to construct the final list including only those ID numbers that appeared in at least 2 out of the 3 lists. For example if the three lists were
23 30 42 57 90
21 23 35 57 90 92
21 23 30 57 90
then the final list compiled by the collector would be:
21 23 30 57 90
The ID numbers 35, 42 and 92 which appeared in only one list each do not figure in the final list.
Your task is to help the collector by writing a program that produces the final list from the three given lists.
Input Format
The first line of the input contains 3 integers N1, N2 and N3. N1 is the number of voters in the first list, N2 is the number of voters in the second list and N3 is the number of voters in the third list. The next N1 lines (lines 2,…,N1+1) contain one positive integer each and describe the first list in ascending order. The following N2 lines (lines N1+2,…,N1+N2+1) describe the second list in ascending order and the final N3 lines (lines N1+N2+2,…,N1+N2+N3+1) describe the third list in ascending order.
Output Format
The first line of the output should contain a single integer M indicating the number voters in the final list. The next M lines (lines 2,…,M+1) should contain one positive integer each, describing the list of voters in the final list, in ascending order.
Test Data
You may assume that 1 ≤ N1,N2,N3 ≤ 50000.
Example
Sample Input
5 6 5
23
30
42
57
90
21
23
35
57
90
92
21
23
30
57
90
Sample Output
5
21
23
30
57
90
Solution – Discrepancies in the Voters List
C++
#include <iostream> #include <bits/stdc++.h> using namespace std; int main() { int n1, n2, n3; cin >> n1 >> n2 >> n3; int n = n1+n2+n3; int arr[n]; for(int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr+n); int count = 0; vector<int> ans; for(int i = 0; i < n-2; i++) { if(arr[i] == arr[i+1]) { count++; if(arr[i+1] == arr[i+2]) i++; i++; ans.push_back(arr[i]); } } if(arr[n-1] == arr[n-2] && arr[n-2] != arr[n-3]) { count++; ans.push_back(arr[n-1]); } cout << count << "\n"; for(int i = 0; i < count; i++) cout << ans[i] << "\n"; return 0; }
Python
# cook your dish here N1, N2, N3 = list(map(int,input().split())) tar = [] d= {} for i in range(N1+N2+N3): j = int(input()) if j in d: d[j] += 1 else: d[j] = 1 for ke in d: if d[ke] > 1: tar.append(ke) tar.sort() print(len(tar)) for i in tar: print(i)
Java
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Codechef { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc = new Scanner(System.in); int n1 = sc.nextInt(); int n2 = sc.nextInt(); int n3 = sc.nextInt(); int[] list = new int[n1+n2+n3]; ArrayList<Integer> array = new ArrayList<>(); for(int i=0 ; i<n1+n2+n3 ; i++){ list[i]=sc.nextInt(); } Arrays.sort(list); int j=0; while(j<list.length-1){ if(list[j]==list[j+1]){ array.add(list[j]); if(list[j]==list[j+2]){ j+=3; } else{ j+=2; } } else{ j++; } } System.out.println(array.size()); for (Integer integer : array) { System.out.println(integer); } } }
Disclaimer: The above Problem (Discrepancies in the Voters List) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.