GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

A google question

Two political parties are contesting in the elections in the N number of states in the country. Each party wins some seats every year in each state. After the current elections are over we have the result of both the parties with their number of seats won in each state. 
The two parties are considered equal if they win the same number of seats in the N states in any order. Otherwise, they are considered unequal. Input/Output Specifications 

INPUT: 1) It is an array of N integers depicting number of seats won by party one in each state. 2) It is an array of N integers depicting number of seats won by party two in each state. 3) An integer containing number of states (N). 

OUTPUT: Equal/Unequal/Invalid 
Equal: if political parties are equal Unequal: if political parties are unequal Invalid: if any party has invalid number of seats in any state 

Examples 

Example 1: 
Two parties contested in seven states Seats won by party one in all the seven states: {12,11,5,2,7,5,-11} Seats won by party two in all the seven states: {5,12,5,7,11,2,11} The party two has an invalid number of winning seats(-11). So the output will be "Invalid". 

Input : 
1) {12,11,5,2,7,5,-11} 
2) {5,12,5,7,11,2,11} 
3) 7 

Output: Invalid 


Example 2: 
Two parties contested in seven states Seats won by party one in all the seven states: {12,11,5,2,7,5,11} Seats won by party two in all the seven states: {5,12,5,7,11,2,11} Both the parties are equal because they have got 11(2 times each), 5(2 times each),7( 1 time each), 2(1 time each), 12( 1 time each other outputs (0 times each). 

Input : 
1) {12,11,5,2,7,5,11} 
2) {5,12,5,7,11,2,11} 
3) 7 

Output: Equal 

Example 3: 

Input : 
{12,11,5,2,7,5,11} 
{5,0,5,7,11,2,11} 


Output: Unequal

asked Nov 21, 2015 by Swanky

3 Answers

import java.io.*; import java.util.*; import java.text.*; import java.math.BigInteger; import java.util.*; class TestClass { public static String print(int[] input1,int[] input2) {Arrays.sort(input1); Arrays.sort(input2); int n=input1.length; int m=input2.length; int count=0; String st=""; if(n!=m) {st="Invalid"; } else {for(int j=0;j<n;j++) {if(input1[j]==input2[j]) {count++; if(count==n-1) {st="Equal"; break; } } else{st="Unequal"; break;} } } return st; } public static void main(String args[] ) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line[] = br.readLine().split(" "); String line1[] = br.readLine().split(" "); int a[]=new int[line.length]; int ar[]=new int[line1.length]; for(int i=0;i<a.length;i++) {a[i]=Integer.parseInt(line[i]); } for(int i1=0;i1<ar.length;i1++) {ar[i1]=Integer.parseInt(line1[i1]); } String st1=print(a,ar); System.out.println(st1); } }
answered Nov 30, 2015 by anonymous

Use code snippet button to post the code!

Using a HashMap, this question can be solved in O(n)

 

answered Nov 30, 2015 by Nithya
import java.util.*;
import java.lang.*;
import java.io.*;

class ElectionResultFinder
{
	
	public enum ElectionResult {
		EQUAL, UNEQUAL, INVALID;
	}
	
	public static ElectionResult getElectionResult(int[] party1, int[] party2, int states) {
		if ( (party1.length != states || party2.length != states) 
			  && party1.length != party2.length) {
			return ElectionResult.INVALID;
		}
		
		int p1xor = 0;
		int p2xor = 0;
		
		for(int i=0 ; i<states ; i++) {
			if (party1[i] < 0 || party2[i] < 0) {
				return ElectionResult.INVALID;
			}
			p1xor ^= party1[i];
			p2xor ^= party2[i];
		}
		
		return 0 == (p1xor ^ p2xor) ? ElectionResult.EQUAL : ElectionResult.UNEQUAL;
		
	}
	public static void main (String[] args) throws java.lang.Exception
	{
		int[] p1 = {12,11,5,2,7,5,11};
		int[] p2 = {5,12,5,7,11,2,11};
		System.out.println(getElectionResult(p1,p2,7).name());
		
		int[] p3 = {12,11,5,2,7,5,-11};
		int[] p4 = {5,12,5,7,11,2,11};
		System.out.println(getElectionResult(p3,p4,7).name());
		
		int[] p5 = {12,11,5,2,7,5,11};
		int[] p6 = {5,0,5,7,11,2,11};
		System.out.println(getElectionResult(p5,p6,7).name());
		
	}
}

 

answered Feb 17, 2016 by Akshay
...