Linear Search
The most basic search algorithm is the linear search method. To find the targeted item, this search algorithm performs a sequential search over all of the items one by one. Each item is examined in turn until a match is discovered. If a match is discovered, a specific item is returned; otherwise, the search continues until it reaches its conclusion.
Algorithm
Linear Search Example
Let us take an example of an array A[7]={5,2,1,6,3,7,8}.
Array A has 7 items. Let us assume we are looking for 7 in the array. Targeted item=7.
Here, we have A[7]={5,2,1,6,3,7,8}
X=7
At first, When i=0 (A[0]=5; X=7) not matched
i++ now, i=1 (A[1]=2; X=7) not matched
i++ now, i=2(A[2])=1; X=7)not matched
ā¦
ā¦.
i++ when, i=5(A[5]=7; X=7) Match Found
Hence, Element X=7 found at index 5.
Linear search is rarely used practically. The time complexity of above algorithm is O(n).
You are given N (3ā¤Nā¤5000) integer points on the coordinate plane. Find the square of the maximum Euclidean distance (aka length of the straight line) between any two of the points.
We can iterate through every pair of points and find the square of the distance between them, by squaring the formula for Euclidean distance:
Maintain the current maximum square distance in max_square
.
Additionally, it ensures that each pair is only tallied once. It doesn't matter if we double-count pairs or allow I and j to be the same point in this issue; but, in other situations when we're counting something rather than looking at the maximum, it's critical to avoid overcounting.
Last updated