Quantcast
Viewing all articles
Browse latest Browse all 39796

decreasing runtime:paying up

import java.io.BufferedReader;
import java.io.InputStreamReader;





 class test1 {
    public static void main(String[] args)throws Exception {



         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

         int lin = Integer.parseInt(br.readLine());
         String str[];

          while(lin-->0)
         {
             str = br.readLine().split(" ");
             int n = Integer.parseInt(str[0]);
             int m = Integer.parseInt(str[1]);
             int flag=0;
                          int a[] = new int[n] ;

             for(int k =0;k<n;k++)
             {

                 a[k]=Integer.parseInt(br.readLine());
             }


             for(int i =1;i<Math.pow(2,n);i++)
             {
                 int sum =0;

for(int j=0; j<n; j++)
{

   if (( i & (1<<j) )>0) //testing if the jth bit of i is set
   {

      sum+=a[j];
   }
   if(sum==m)
             {
                 System.out.println("Yes");
                 flag=1;
                 break;
             }

}            if(flag==1)
             {

                 break;
             }

               }


             if(flag==0)
             {
               System.out.println("No");  
             }






         }
    }
}

getting a time around 9.7 in codechef is there anyway i can decrease it...!


Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>