Skip to content

Commit

Permalink
Merge pull request #520 from Aditya-Harsh/main
Browse files Browse the repository at this point in the history
Added solution to one of the Meta Hacker Cup 2022 Questions
  • Loading branch information
tanus786 authored Oct 26, 2022
2 parents 5bddcaf + f41a2f2 commit a87df77
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Facebook HackerCup Problem Solution/Problem Statement 1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Sandy's store has N pre-owned clock parts for sale, where the ith part is of style Si. The store also has two display cases, each capable of holding at most K parts. To maximize the aesthetics of Sandy's secondhand second hands, she'd like to put each of the N parts into one of the two cases so that neither case ends up with two different parts of the same style, and neither case has more than K parts total. Can you determine if this is possible?

Constraints
1≤T≤90
1≤N,K,Si≤100

Input Format
Input begins with an integer T, the number of test cases. For each test case, there is first a line containing 2 space-separated integers, N and K. Then, there is a line containing N space-separated integers, S1, ..., SN.

Output Format
For the ith test case, print "Case #i: " followed by "YES" if it's possible to arrange the N parts into two cases satisfying the description above, or "NO" otherwise.

Sample Test Case
Suppose there are 3 parts of styles 1, 2, and 2, with the display cases having capacity 2. One solution is to put the first and third parts in one display case, and the second part in the other.
47 changes: 47 additions & 0 deletions Facebook HackerCup Problem Solution/Solution 1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.Scanner;

public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int test=sc.nextInt();
int i, j, min, cMax, vMax, c1, c2;
String s;
int vCount, cCount;
for(i=1; i<=test; i++){
min= Integer.MIN_VALUE;
cCount=0; vCount=0;
int[] arr=new int[26];
s=sc.next();
int length=s.length();
for(j=0; j<length; j++){
int ch=s.charAt(j);
arr[ch%65]++;
switch(s.charAt(j)){
case 'A': case 'E': case 'I': case 'O': case 'U': vCount++;
break;
default:cCount++;
}
}
cMax=arr[1];
vMax=arr[0];
for(j=0; j<26; j++) {
if(j==0 || j==4 || j==8 || j==14 || j==20){
if(arr[j]>vMax)
vMax=arr[j];
}
else if(arr[j]>cMax)
cMax=arr[j];
if(min<arr[j])
min=arr[j];
//System.out.println(arr[j]);
}
c1=((vCount-vMax)*2)+cCount;
c2=((cCount-cMax)*2)+vCount;
if(c1<c2)
System.out.println("Case #"+i+": "+c1);
else
System.out.println("Case #"+i+": "+c2);
}

}
}

0 comments on commit a87df77

Please sign in to comment.