Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gokul.demo.InProgress;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.Arrays;
- public class SearchKey {
- public static void main(String[] args) throws java.lang.Exception {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int N = Integer.parseInt(reader.readLine());
- String[] inputTokens = reader.readLine().split(" ");
- // int a = 0;
- int[] arr = new int[N];
- for (int i = 0; i < N; i++) {
- arr[i] = Integer.parseInt(inputTokens[i]);
- }
- int T = Integer.parseInt(reader.readLine());
- int[] testArr = new int[T];
- for (int j = 0; j < T; j++) {
- testArr[j] = Integer.parseInt(reader.readLine());
- }
- for (int c = 0; c < testArr.length; c++) {
- int l = 0;
- int h = arr.length - 1;
- int m = 0;
- int k = testArr[c];
- keyFind(k, l, h, arr, m);
- }
- }
- public static int keyFind(int k, int l, int h, int[] arr, int m) {
- while (l <= h) {
- m = (l + h) / 2;
- if (arr[m] == k) {
- System.out.println(m);
- return m;
- } else if (arr[m] < k) {
- l = m + 1;
- } else {
- h = m - 1;
- }
- }
- System.out.println(-1);
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement