Created
January 25, 2017 17:21
-
-
Save perilstar/90682d953d59fc70774f44c54d77d279 to your computer and use it in GitHub Desktop.
In theory, find full house in a set of arbitrary ints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) | |
{ | |
int[] a = new int[]{1,1,2,5,2,1,4}; | |
System.out.println(hasFullHouse(a)?"Full house!":"Better luck next time"); | |
} | |
public static boolean hasFullHouse(int[] arrIn) { | |
//although I dont have an implementation, according to sources, this is linear | |
//O(10(n+10)) | |
//http://www.geeksforgeeks.org/radix-sort/ | |
int[] arr = radixSort(arrIn); | |
int two = 0, threeOrGreater = 0, inARow = 0; | |
for(i=1;i<arr.length;i++) { | |
if (arr[i] == arr[i-1]) { | |
inARow++; | |
} else { | |
if (inARow==2) { | |
two++; | |
} else if (inARow>=3) { | |
threeOrGreater++; | |
} | |
inARow=0; | |
//optionally check for a fill house inside the loop and return if it's found, this could save trillons of cycles on big arrays | |
} | |
} | |
return threeOrGreater >= 2 || (two >= 1 && threeOrGreater >= 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment