tarunreddy1018

Untitled

Feb 20th, 2020
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Arrays;
  3.  
  4. public class Integer_Distribution {
  5. private static long minXor;
  6. private static long maxXor;
  7.  
  8. private static int getSetBitCount(int num) {
  9. int count = 0;
  10. while(num > 0) {
  11. if((num & 1) != 0) {
  12. count++;
  13. }
  14. num >>= 1;
  15. }
  16. return count;
  17. }
  18.  
  19. private static void getPairXor(int array[], int N) {
  20. int SIZE = (int)Math.pow(2, N) - 1;
  21.  
  22. long dpMin[] = new long[SIZE+1];
  23. long dpMax[] = new long[SIZE+1];
  24.  
  25. dpMin[0] = 0;
  26.  
  27. for(int i = 1;i <= SIZE;i++) {
  28. int setBitCount = getSetBitCount(i);
  29. dpMin[i] = Long.MAX_VALUE;
  30. dpMax[i] = Long.MIN_VALUE;
  31.  
  32. if(setBitCount%2 == 0) {
  33. for(int j = 0;j < N;j++) {
  34. for(int k = (j+1);k < N;k++) {
  35. if(((i & (1<<j)) != 0) && ((i & (1 << k)) != 0)) {
  36. int num = i ^ (1 << j);
  37. num = num ^ (1 << k);
  38. long xor = array[j] ^ array[k];
  39.  
  40. dpMin[i] = Math.min(dpMin[i], (xor + dpMin[num]));
  41. dpMax[i] = Math.max(dpMax[i], (xor + dpMax[num]));
  42. }
  43. }
  44. }
  45. }
  46. }
  47.  
  48. minXor = dpMin[SIZE];
  49. maxXor = dpMax[SIZE];
  50. }
  51.  
  52. public static void main(String[] args) throws IOException {
  53. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  54. BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
  55. StringBuilder sb = new StringBuilder();
  56.  
  57. int N = Integer.parseInt(br.readLine());
  58. int array[] = Arrays.stream(br.readLine().split(" ")).mapToInt(c -> Integer.parseInt(c)).toArray();
  59.  
  60. getPairXor(array, N);
  61.  
  62. sb.append(minXor).append(" ").append(maxXor);
  63.  
  64. bw.write(sb.toString());
  65. bw.close();
  66. }
  67. }
Add Comment
Please, Sign In to add comment