You are on page 1of 4

Homework

Binary search - Mirela

In a nutshell, this search algorithm takes advantage of a collection of elements that is already
sorted by ignoring half of the elements after just one comparison. 
Compare x with the middle element.
If x matches with the middle element, we return the mid index.
Else if x is greater than the mid element, then x can only lie in the right (greater) half subarray
after the mid element. Then we apply the algorithm again for the right half.
Else if x is smaller, the target x must lie in the left (lower) half. So we apply the algorithm for the
left half.

arr = [1, 3, 5, 9, 30, 50]


x=9

Output:

Element is present at index 3


Largest Range - Mirela

 Write a function that takes in an array of integers and returns an array of length 2 representing
the largest range of numbers contained in that array.
 The first number is the output array should be the first number in the range while the second
number should be the last number in the range.
 A range of numbers is defined as a set of numbers that come right after each other in the set of
real integers.
 For instance, the output array [2, 6] represent the range {2, 3, 4, 5, 6}, which is the range of
length 5. Note that numbers do not need to be ordered or adjacent in the array in order to
form a range.

 Assume that there will only be one large range


• Sample input: [1, 11, 3, 0, 15, 5, 2, 4, 10, 7, 12, 6]
• Sample output: [0, 7]
• Tip: don’t use sort() method because it is not the optimal one
Pas 1 Pas 2

Pas 3

You might also like