The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. ― Donald E. Knuth

# Algorithms

## Arrays & Strings

Problem :-
Given an array with 'n' elements & a value 'x', find two elements in the array which sums to 'x'.
Consider the array { 37, 19, 4, 87, 21 } & a target value 25.
Two elements wich sums to 25 are 4 & 21 .

Solution :-
Approach 1 :: Using Sort.
Sort the elements and starting checking from the beginning of the array for the required pair. See the implementation below [ O(n*(log n)) complexity if we use the best sorting algorithm ]
Approach 2 :: Using a hash map.
We will initialize a hash_map of a large size (s.t all elements of the array are within this range) with all 0s.
As we iterate over the given array from i = 0 ... n-1,
we will set hash_map[target-arr[i]] = 1. If at any point of time, we find an already set index, then we have found the two elements.
See the implementation below [ O(n) time complexity ]