My issue
Array index out of bound at line 22
My code
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int T;
Scanner sc=new Scanner (System.in);
T=sc.nextInt();
for(int i=0;i<T;i++){
int N,B;
N=sc.nextInt(); //6 elements
B=sc.nextInt(); //5 target
int arr[]=new int[N];
for(int a=0;i<N;a++){
arr[a]=sc.nextInt();
}
ArrayList<Integer> al=new ArrayList<>();
ArrayList<Integer> ans=new ArrayList<>();
for(int b=0;b<N;b++){
if((arr[b] & B)==B){
al.add(arr[b]);
}
}
if(al.isEmpty())
{
System.out.println("no");
}
else{
int and=al.get(0);
for(int j=1;j<al.size()-1;j++){
and=(al.get(j) & and);
if(and==B)
{
ans.add(arr[j]);
}
}
}
if(ans.isEmpty()){
System.out.println("no");
}
else{
System.out.println("yes");
}
}
}
}
Problem Link: CS2023_PON Problem - CodeChef