3. Binary search algorithm
• binary search or half-interval search finds the position
of a specified value within a sorted array.
• In each step, the algorithm compares the input key
value with the key value of the middle element of the
array.
• If the keys match, then a matching element has been
found so its index, or position, is returned.
• Otherwise, if the sought key is less than the middle
element's key, then the algorithm repeats its action on
the sub-array to the left of the middle element or, if
the input key is greater, on the sub-array to the right.
• If the remaining array to be searched is reduced to
zero, then the key cannot be found in the array and a
special "Not found" indication is returned.
4. Iterative
int binary_search(int A[], int key, int imin, int imax)
{
while (imax >= imin) {
int imid = (imin + imax) / 2;
if (A[imid] < key)
imin = imid + 1;
else if (A[imid] > key )
imax = imid - 1;
else return imid;
}
return KEY_NOT_FOUND;
}
5. Recursive
int binary_search(int A[], int key, int imin, int imax)
{
if (imax < imin) return KEY_NOT_FOUND;
else {
int imid = (imin + imax) / 2;
if (A[imid] > key)
return binary_search(A, key, imin, imid-1);
else if (A[imid] < key)
return binary_search(A, key, imid+1, imax);
Else
return imid;
}}
7. 1088 - Points in Segments
• Given n points (1 dimensional) and q
segments, you have to find the number of
points that lie in each of the segments. A point
pi will lie in a segment A B if A ≤ pi ≤ B.
• For example if the points are 1, 4, 6, 8, 10.
And the segment is 0 to 5. Then there are 2
points that lie in the segment.
8. Input
• input starts with an integer T (≤ 5), denoting the
number of test cases.
• Each case starts with a line containing two
integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). The
next line contains n space separated integers
denoting the points in ascending order. All the
integers are distinct and each of them range in
[0, 108].
• Each of the next q lines contains two integers Ak
Bk (0 ≤ Ak ≤ Bk ≤ 108) denoting a segment.
9. Output
• For each case, print the case number in a
single line. Then for each segment, print the
number of points that lie in that segment.
12. 1043 - Triangle Partitioning
• You are given AB, AC and BC. DE is parallel to
BC. You are also given the area ratio between
ADE and BDEC. You have to find the value of
AD.
13. Input
• Input starts with an integer T (≤ 25), denoting the
number of test cases.
• Each case begins with four real numbers
denoting AB, AC, BC and the ratio of ADE and
BDEC (ADE / BDEC). You can safely assume that
the given triangle is a valid triangle with positive
area.
Output
For each case of input you have to print the case
number and AD. Errors less than 10-6 will be ignored.
16. Similar
• 1048 - Conquering Keokradong
• 1056 - Olympics
• 1076 - Get the Containers
• 1138 - Trailing Zeroes (III)
• 1276 - Very Lucky Numbers
17. Bisection
• Bisection -> two section
• Divide elements to 2 part.
• Then sort one part & use binary search.
18. 1127 - Funny Knapsack
• Given n integers and a knapsack of weight W, you have to
count the number of combinations for which you can add
the items in the knapsack without overflowing the weight.
• Input
• Input starts with an integer T (≤ 100), denoting the number
of test cases.
• Each case contains two integers n (1 ≤ n ≤ 30) and W (1 ≤ W
≤ 2 * 109) and the next line will contain n integers separated
by spaces. The integers will be non negative and less than
109.
• Output
• For each set of input, print the case number and the
number of possible combinations.
24. 1146 - Closest Distance
• Two men are moving concurrently, one man is
moving from A to B and other man is moving
from C to D. Initially the first man is at A, and
the second man is at C. They maintain
constant velocities such that when the first
man reaches B, at the same time the second
man reaches D. You can assume that A, B, C
and D are 2D Cartesian co-ordinates. You have
to find the minimum Euclidean distance
between them along their path.
25. • Input
• Input starts with an integer T (≤ 1000), denoting the
number of test cases.
• Each case will contain eight integers: Ax, Ay, Bx, By, Cx, Cy,
Dx, Dy. All the co-ordinates are between 0 and 100. (Ax,
Ay) denotes A. (Bx, By) denotes B and so on.
• Output
• For each case, print the case number and the minimum
distance between them along their path. Errors less
than 10-6 will be ignored.