Reduction in Complexity of the Algorithm by Increasing the Used Memory-An Example

An algorithm complexity, or its efficiency, meaning its time of evaluation is the focus of primary care in algorithmic problems solving. Raising the used memory may reduce the complexity of algorithm drastically. We present an example of two algorithms on finite set, where change the approach to the same problem and introduction a memory array allows decrease the complexity of the algorithm from the order O(n) up to the order O(n).


Introduction
An algorithm efficiency understood as the time of its execution is the focus of primary care in the design and analysis of algorithms ( [1,2]).The lower bond of the execution time of an algorithm directly correlated with the order of complexity of the algorithm.A different approach to the solution to the problem allows sometimes to change the algorithm and to reduce its complexity by introduction an additional memory, for example ( [3]).Such a method requires some further analysis of the problem at hand.We illustrate this case with the following example.

The Problem
Given two n-digit natural numbers (n > 0).One needs to find the number of matching digits at the same positions in both numbers, alongside with overall count of matching digits over the numbers.If a digit has already participated in a matching pair, it is ignored in further encounter.Consider, for example, two numbers 172345 and 287376.The amount of matching identical digits in the equivalent positions is 1 (this is the digit 3).The count of matching identical digits in the various positions is 2 (digit 2 and digit 7).If the same digit in the numbers appears more than once, the count is defined as the minimum number of occurrences in one of the positions of the numbers.For example: 22275 and 86322 specifies the repetition of the number 2 twice.

The First Approximate
To address the first part of the task (counting the digits on the same positions) we use quite a simple approach: check the number of the units digit in both numbers (the remainder of these numbers divided by 10 gives the number of units in those numbers), and if they are equal, then the corresponding counting variable is increased by 1. Then we "delete" the units digit in both numbers (integer divide by 10).If a match was encountered, the digits are not returned to the original numbers.The performance of this algorithm requires    

end algorithm
Note that in the above algorithm to the presented problem, it is not significant that the variables tmp_num1 and tmp_num2 include digits of the original numbers in reverse order.One can just assign as desired.
For the second part of the solution, an auxiliary algorithm-function that receives two parameters: the number and the digit which will be used.The second algorithm checks if the digit is presented in the number.If so, the algorithm deletes its first occurrence from the right (of unit's digit) and returns the number lower by one order, else it returns the number unchanged.
DeleteDigit The number of actions needed to run this algorithm is about runtimes in the worst case scenario (see [1,2]).Using this algorithm, one can count the total number of occurrences of digits in different positions (matched digits in same positions were "deleted" in the first part of the solution)

deletion occurred, count as digits match} count_eq  count_eq + 1 num1  num1 div 10 {deleting reviewed digit} return count_eq end if end while end algorithm
The number of actions required to perform this algorithm with the auxiliary algorithm will be of order .Finally counting, for both parts the runtime will approach .In other words, the final value of the asymptotic limit of the above functions is of O n .A question is, if one can to reduce the order of operations amount in order to solve the problem under consideration (see [3])?To answer is yes, but the using memory should be extended.

The second Approximate
The idea behind the second solutions is based on the idea of the quick sort method like "counting sort", where we count equal elements in the array, based on the property of the studied values limits.For the first part of the task we construct two auxiliary arrays, which are equal to the length of the given numbers, hence of length n.Thus, equal digits on matching positions give us the matching digits on same positions.By presenting the numbers as digits in arrays, we are not obliged to use digits standing on same positions.
For the second part of the problem we use the same idea, but considering the length of the given numbers.We know that all decimal numbers consist of digits from 0 to 9. Thus, creating an auxiliary array of size 10 we can place in it the amount of counted corresponding digits.Checking the corresponding numbers of such arrays for each of these numbers will give us the result of matching numbers ( [3,4]).
CountEquals2 (num1, num2) {two arrays of length n for digits of first part of the solution.Arr_num1 [n], arr_num2 [n], the lengths of our numbers are given -n.count_pos0 count of position matches count_eq 0 count of total matches}

arr_count1[i]>0 AND arr_count2[i>0] ) count_eqcount_eq +min(arr_count1[i], arr_count2[i]) end for return count_pos, count_eq end algorithm
The number of operations required to perform the algorithm is of an order The final value of the asymptotic function that limits our result from above is comparing with O n in the previous versions.

Conclusions
For small values of n, the second solution may require even more operations than the first solution.There is a range of values of the segment closest to the origin of the function y a n   while above will limit the function by 2 y b n   , where a and b are appropriate constants.
However, when n tends to infinity (or became sufficiently large), the square function grows faster than linear obviously ( [3]).With this simple example, we wanted to show how the runtimes of different algorithms solving the same problem can be different drastically, when the problem is correctly formulated and the right model and built solution are properly matched.