|
|
back to boardTLE Help Please import java.io.*; import java.util.*; public class FindinArray_1196{ public static int binarySearch( int a[], int x ) { int low = 0; int high = a.length - 1; int mid; while( low <= high ) { mid = ( low + high ) / 2; if( a[ mid ] < x ) low = mid + 1; else if( a[ mid ] > x ) high = mid - 1; else return mid; } return -1; // NOT_FOUND = -1 } public static void main(String args[]) { Scanner sc=new Scanner(System.in); int count=0; int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } int m=sc.nextInt(); int brr[]=new int[m+1]; for(int i=0;i<m;i++) { brr[i]=sc.nextInt(); } for(int i=0;i<m;i++) { if(binarySearch(arr,brr[i])!=-1) count++; } System.out.println(count); } } Re: TLE Help Please Scanner is very slow, use StreamTokenizer |
|
|