Hello, Can anyone help me with this code. Sample cases are running correctly, few test cases made by me are also running fine but still it is giving wrong answer.
Link :-
Code:-
package Codechef;
import java.io.;
import java.util.;
public class BINFUN {
public static int count(int nums) {
return 1 + Integer.bitCount(Integer.highestOneBit(nums) - 1);
}
public static long value(int nums1, int nums2, int len1, int len2) {
long x = (nums1<<len2) + nums2;
long y = (nums2<<len1) + nums1;
return Math.abs(x-y);
}
public static void main(String[] args) throws IOException
{
StdIn scn = new StdIn();
int t = scn.nextInt();
while (t–> 0) {
int n = scn.nextInt();
ArrayList<ArrayList> adj = new ArrayList<ArrayList>();
for (int i = 0; i<33; i++) {
adj.add(new ArrayList());
}
for (int i = 0; i<n; i++) {
int nums = scn.nextInt();
int pos = count(nums);
adj.get(pos).add(nums);
}
for (int i = 0; i<adj.size(); i++) {
Collections.sort(adj.get(i));
}
long ans = -1;
for (int i = 1; i<32; i++) {
for (int j = 1; j<32; j++) {
if (adj.get(i).isEmpty() || adj.get(j).isEmpty()) continue;
ans = Math.max(ans, value(adj.get(i).get(0), adj.get(j).get(adj.get(j).size()-1), i, j));
}
}
System.out.println(ans);
}
}
interface Input {
public String next();
public String nextLine();
public int nextInt();
public long nextLong();
public double nextDouble();
}
static class StdIn implements Input {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public StdIn() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public StdIn(String filename) {
try{
din = new DataInputStream(new FileInputStream(filename));
} catch(Exception e) {
throw new RuntimeException();
}
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String next() {
int c;
while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
StringBuilder s = new StringBuilder();
while (c != -1)
{
if (c == ' ' || c == '\n'||c=='\r')
break;
s.append((char)c);
c=read();
}
return s.toString();
}
public String nextLine() {
int c;
while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
StringBuilder s = new StringBuilder();
while (c != -1)
{
if (c == '\n'||c=='\r')
break;
s.append((char)c);
c = read();
}
return s.toString();
}
public int nextInt() {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public int[] readIntArray(int n) {
int[] ar = new int[n];
for(int i=0; i<n; ++i)
ar[i]=nextInt();
return ar;
}
public long nextLong() {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long[] readLongArray(int n) {
long[] ar = new long[n];
for(int i=0; i<n; ++i)
ar[i]=nextLong();
return ar;
}
public double nextDouble() {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() {
try{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
} catch(IOException e) {
throw new RuntimeException();
}
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
}