Last active
October 25, 2020 14:31
-
-
Save barancev/87403bb01dc56bb9cd26995d2e18e857 to your computer and use it in GitHub Desktop.
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
static int[] array; | |
@BeforeClass | |
public static void prepare() { | |
array = new int[]{ | |
1, 3, 5, 7, 9, | |
11, 13, 15, 17, 19, | |
21, 23, 25, 27, 29, | |
31, 33, 35, 37, 39, | |
41, 43, 45, 47, 49, | |
1, 3, 5, 7, 9, | |
11, 13, 15, 17, 19, | |
21, 23, 25, 27, 29, | |
31, 33, 35, 37, 39, | |
41, 43, 45, 22, 11 | |
}; | |
} | |
boolean valid1(int[] array) { | |
long x = 0; | |
for (int j : array) { | |
if (j < 25 && (x & (1 << (j << 1))) != 0) { | |
return true; | |
} | |
if (((j & 1) == 0) && ((x & (1 << (j >> 1))) != 0)) { | |
return true; | |
} | |
x |= 1 << j; | |
} | |
return false; | |
} | |
boolean valid2(int[] array) { | |
int[] bits = new int[50]; | |
for (int j : array) { | |
if (j < 25 && bits[j*2] != 0) { | |
return true; | |
} | |
if (j % 2 == 0 && bits[j/2] != 0) { | |
return true; | |
} | |
bits[j] = 1; | |
} | |
return false; | |
} | |
@Test | |
public void solution1() { | |
int max = 100000000; | |
long start2 = System.currentTimeMillis(); | |
for (int i = 0; i < max; i++) { | |
assertTrue(valid2(array)); | |
} | |
long end2 = System.currentTimeMillis(); | |
System.out.println("2: " + (end2 - start2)); | |
long start = System.currentTimeMillis(); | |
for (int i = 0; i < max; i++) { | |
assertTrue(valid1(array)); | |
} | |
long end = System.currentTimeMillis(); | |
System.out.println("1: " + (end - start)); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment