Skip to content

Instantly share code, notes, and snippets.

@phaniram
Created January 8, 2012 13:38
Show Gist options
  • Save phaniram/1578386 to your computer and use it in GitHub Desktop.
Save phaniram/1578386 to your computer and use it in GitHub Desktop.
Interview Street Challenges - Picking_cards
package interviewstreet;
import java.util.HashMap;
import java.util.Scanner;
public class Picking_cards {
int num;
int[] card_nums;
HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();
public Picking_cards(int num_of_cards, int[] cards) {
this.num=num_of_cards;
this.card_nums=cards;
}
public long solve()
{
int buf=0;
long ret=0;
for(int i=0;i<num;i++)
{
int key=card_nums[i];
if(this.hm.containsKey(key))
{
int value=this.hm.get(key);
this.hm.put(key, ++value);
}
else
{
this.hm.put(key, 1);
}
}
if(hm.containsKey(0))
{
for(int key:hm.keySet())
{
if(buf==0)
{
ret=fact(hm.get(key));
buf++;
}
else if(ret>=hm.get(key))
{
ret*=fact(hm.get(key));
buf++;
}
}
}
if(hm.size()!=buf)
{
ret=0;
}
return ret;
}
public long fact(Integer fc)
{
long ret=1;
for(int i=1;i<=fc;i++)
{
ret*=i;
}
return ret;
}
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
int no_cases = scanner.nextInt();
for (int i = 0; i < no_cases; i++) {
int num_of_cards = scanner.nextInt();
int[] cards=new int[num_of_cards];
for(int j=0;j<num_of_cards;j++)
{
cards[j]=scanner.nextInt();
}
Picking_cards pc =new Picking_cards(num_of_cards,cards);
System.out.println(pc.solve());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment