ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1196. History Exam

TLE Help Please
Posted by Varun Kumar(Fundu) 4 Oct 2008 20:32
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
Posted by Iliya (Java) 26 Oct 2008 05:01
Scanner is very slow, use StreamTokenizer