Advertisement
naman19179

BINFUN (July Lunchtime'20)

Jul 31st, 2020
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.82 KB | None | 0 0
  1. package Codechef;
  2. import java.io.*;
  3. import java.util.*;
  4. public class BINFUN {
  5.     public static int count(int nums) {
  6.         return 1 + Integer.bitCount(Integer.highestOneBit(nums) - 1);
  7.     }
  8.     public static long value(int nums1, int nums2, int len1, int len2) {
  9.         long x = (nums1<<len2) + nums2;
  10.         long y = (nums2<<len1) + nums1;
  11.         return Math.abs(x-y);
  12.     }
  13.     public static void main(String[] args) throws IOException
  14.     {
  15.         StdIn scn = new StdIn();
  16.         int t = scn.nextInt();
  17.         while (t--> 0) {
  18.             int n = scn.nextInt();
  19.             ArrayList<ArrayList<Integer>> adj = new ArrayList<ArrayList<Integer>>();
  20.             for (int i = 0; i<33; i++) {
  21.                 adj.add(new ArrayList<Integer>());
  22.             }
  23.             for (int i = 0; i<n; i++) {
  24.                 int nums = scn.nextInt();
  25.                 int pos = count(nums);
  26.                 adj.get(pos).add(nums);
  27.             }
  28.             for (int i = 0; i<adj.size(); i++) {
  29.                 Collections.sort(adj.get(i));
  30.             }
  31.             long ans = -1;
  32.             for (int i = 1; i<32; i++) {
  33.                 for (int j = 1; j<32; j++) {
  34.                     if (adj.get(i).isEmpty() || adj.get(j).isEmpty()) continue;
  35.                     ans = Math.max(ans, value(adj.get(i).get(0), adj.get(j).get(adj.get(j).size()-1), i, j));
  36.                 }
  37.             }
  38.             System.out.println(ans);
  39.         }
  40.     }
  41.     interface Input {
  42.         public String next();
  43.         public String nextLine();
  44.         public int nextInt();
  45.         public long nextLong();
  46.         public double nextDouble();
  47.     }
  48.     static class StdIn implements Input {
  49.         final private int BUFFER_SIZE = 1 << 16;
  50.         private DataInputStream din;
  51.         private byte[] buffer;
  52.         private int bufferPointer, bytesRead;
  53.  
  54.         public StdIn() {
  55.             din = new DataInputStream(System.in);
  56.             buffer = new byte[BUFFER_SIZE];
  57.             bufferPointer = bytesRead = 0;
  58.         }
  59.        
  60.         public StdIn(String filename) {
  61.             try{
  62.                 din = new DataInputStream(new FileInputStream(filename));
  63.             } catch(Exception e) {
  64.                 throw new RuntimeException();
  65.             }
  66.             buffer = new byte[BUFFER_SIZE];
  67.             bufferPointer = bytesRead = 0;
  68.         }
  69.        
  70.         public String next() {
  71.             int c;
  72.             while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
  73.             StringBuilder s = new StringBuilder();
  74.             while (c != -1)
  75.             {
  76.                 if (c == ' ' || c == '\n'||c=='\r')
  77.                     break;
  78.                 s.append((char)c);
  79.                 c=read();
  80.             }
  81.             return s.toString();
  82.         }
  83.  
  84.         public String nextLine() {
  85.             int c;
  86.             while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
  87.             StringBuilder s = new StringBuilder();
  88.             while (c != -1)
  89.             {
  90.                 if (c == '\n'||c=='\r')
  91.                     break;
  92.                 s.append((char)c);
  93.                 c = read();
  94.             }
  95.             return s.toString();
  96.         }
  97.  
  98.         public int nextInt() {
  99.             int ret = 0;
  100.             byte c = read();
  101.             while (c <= ' ')
  102.                 c = read();
  103.             boolean neg = (c == '-');
  104.             if (neg)
  105.                 c = read();
  106.             do
  107.             {
  108.                 ret = ret * 10 + c - '0';
  109.             }  while ((c = read()) >= '0' && c <= '9');
  110.  
  111.             if (neg)
  112.                 return -ret;
  113.             return ret;
  114.         }
  115.        
  116.         public int[] readIntArray(int n) {
  117.             int[] ar = new int[n];
  118.             for(int i=0; i<n; ++i)
  119.                 ar[i]=nextInt();
  120.             return ar;
  121.         }
  122.  
  123.         public long nextLong() {
  124.             long ret = 0;
  125.             byte c = read();
  126.             while (c <= ' ')
  127.                 c = read();
  128.             boolean neg = (c == '-');
  129.             if (neg)
  130.                 c = read();
  131.             do {
  132.                 ret = ret * 10 + c - '0';
  133.             }
  134.             while ((c = read()) >= '0' && c <= '9');
  135.             if (neg)
  136.                 return -ret;
  137.             return ret;
  138.         }
  139.        
  140.         public long[] readLongArray(int n) {
  141.             long[] ar = new long[n];
  142.             for(int i=0; i<n; ++i)
  143.                 ar[i]=nextLong();
  144.             return ar;
  145.         }
  146.  
  147.         public double nextDouble() {
  148.             double ret = 0, div = 1;
  149.             byte c = read();
  150.             while (c <= ' ')
  151.                 c = read();
  152.             boolean neg = (c == '-');
  153.             if (neg)
  154.                 c = read();
  155.  
  156.             do {
  157.                 ret = ret * 10 + c - '0';
  158.             }
  159.             while ((c = read()) >= '0' && c <= '9');
  160.  
  161.             if (c == '.')
  162.             {
  163.                 while ((c = read()) >= '0' && c <= '9')
  164.                 {
  165.                     ret += (c - '0') / (div *= 10);
  166.                 }
  167.             }
  168.  
  169.             if (neg)
  170.                 return -ret;
  171.             return ret;
  172.         }
  173.  
  174.         private void fillBuffer() throws IOException {
  175.             bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  176.             if (bytesRead == -1)
  177.                 buffer[0] = -1;
  178.         }
  179.  
  180.         private byte read() {
  181.             try{
  182.                 if (bufferPointer == bytesRead)
  183.                     fillBuffer();
  184.                 return buffer[bufferPointer++];
  185.             } catch(IOException e) {
  186.                 throw new RuntimeException();
  187.             }
  188.         }
  189.  
  190.         public void close() throws IOException {
  191.             if (din == null)
  192.                 return;
  193.             din.close();
  194.         }
  195.     }
  196.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement