We are given an array of n points in the plane, and the problem is to find out the closest pair of points in the array.
Algorithm to find closest pair of points(O(nlogn))
step 1) First we sort all points according to x coordinates.
step 2) Divide set all points in two set of half size.
step 3) Find the smallest distances in both subarrays
recursively.
step 4) find the minimum of two smallest distances say it d.
step 5) Create an array points[] that stores all points which are
at most d distance away from the middle line dividing the two
sets.
step 6) Find the smallest distance in points[].
step 7) Return the minimum of d and the smallest distance
calculated in above step 6.
Consider the algorithm to find the closest pair of points in the plane. Let's say you wanted to generalize the algorithm to find the two closest pairs of points in the plane given a set of (unsor...
Let's say you are given a sequence of distinct positive numbers. We want to find a subsequence with the maximum possible sum, with the restriction that we are not allowed to take three consecutive elements from the original sequence. For example, for input 1, 6, 5, 2, 7, 9, 3, 4, the subsequence with the maximum possible sum is 6, 5, 7, 9, 4 (we have two pairs of consecutive elements 6, 5 and 7, 9 but not three consecutive...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3 A = [10, 4, 6, 16, 1, 6, 12, 13] Then your method should return the following pairs: 4, 1 15, 12 13, 10 A poor solution: There are...