employer cover photo
employer logo
employer logo

Check Point Software Technologies

Engaged Employer

Check Point Software Technologies Interview Question

Binary search on array

Interview Answers

Anonymous

May 26, 2018

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package checkpoint; /** * * @author Omar */ public class BinarySearch { public static int search(int[] a, int first, int last, int key) { int result = 0; if(first > last) { System.out.println("The " + key + " is not found inside the array"); result = -1; } else { int mid = (first + last) / 2; if(key == a[mid]) { result = mid; } if(key a[mid]) { result = search(a, mid + 1, last, key); } } return result; } }

Anonymous

Nov 3, 2020

public int bs(int[] arr, int val) { int left = 0, right = arr.length - 1; while(left <= right) { int mid = (right - left) / 2; if(arr[mid] == val) { return mid; else if(arr[mid] < val) { left = mid + 1; } else { right = mid - 1; } return -1; }