employer cover photo
employer logo
employer logo

Check Point Software Technologies

Employeur impliqué

Question d’entretien chez Check Point Software Technologies

Binary search on array

Réponses aux questions d'entretien

Utilisateur anonyme

26 mai 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; } }

Utilisateur anonyme

3 nov. 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; }