File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ public class binarysearch {
4+ public static void main (String [] jah ) {
5+ Scanner sc = new Scanner (System .in );
6+ int size = sc .nextInt ();
7+ int q = sc .nextInt ();
8+ int [] arr = new int [size ];
9+ for (int i = 0 ; i < size ; i ++) {
10+ arr [i ] = sc .nextInt ();
11+ }
12+ Arrays .sort (arr );
13+ while (q > 0 ) {
14+ boolean isfound = false ;
15+ int x = sc .nextInt ();
16+ int start = 0 ;
17+ int end = size - 1 ;
18+ while (start <=end ) {
19+
20+ int mid = (start + end ) / 2 ;
21+ if (arr [mid ] > x ) {
22+ end = mid - 1 ;
23+ } else if (arr [mid ] < x ) {
24+ start = mid + 1 ;
25+ } else {
26+ System .out .println ("found" +" " +(mid ));
27+ isfound = true ;
28+ break ;
29+ }
30+ }
31+ q --;
32+ if (!isfound ) {
33+ System .out .println ("not found" );
34+ }
35+ }
36+ sc .close ();
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments