What can you say about the number of even numbers under the same constraints

Here is a possible solution. It is rather contrived and not practical, but then, so is the problem. I would appreciate any comments if I have holes in my analysis. If this was a homework or challenge problem with an “official” solution, I’d also love to see that if the original poster is still about, given that more than a month has passed since it was asked.

First, we need to flesh out a few ill-specified details of the problem. Time complexity required is O(N), but what is N? Most commentators appear to be assuming N is the number of elements in the array. This would be okay if the numbers in the array were of fixed maximum size, in which case Michael G’s solution of radix sort would solve the problem. But, I interpret constraint #1, in absence of clarification by the original poster, as saying the maximum number of digits need not be fixed. Therefore, if n (lowercase) is the number of elements in the array, and m the average length of the elements, then the total input size to contend with is mn. A lower bound on the solution time is O(mn) because this is the read-through time of the input needed to verify a solution. So, we want a solution that is linear with respect to total input size N = nm.

For example, we might have n = m, that is sqrt(N) elements of sqrt(N) average length. A comparison sort would take N1 operations, but this is not a victory, because the operations themselves on average take N2 time, so we are back to N3.

Also, a radix sort would take N4 if m were the maximum length instead of average length. The maximum and average length would be on the same order if the numbers were assumed to fall in some bounded range, but if not we might have a small percentage with a large and variable number of digits and a large percentage with a small number of digits. For example, 10% of the numbers could be of length N6 and 90% of length N7. The average length would be m, but the maximum length N6, so the radix sort would be N0.

Lest there be any concern that I have changed the problem definition too dramatically, my goal is still to describe an algorithm with time complexity linear to the number of elements, that is N1. But, I will also need to perform operations of linear time complexity on the length of each element, so that on average over all the elements these operations will be N2. Those operations will be multiplication and addition needed to compute hash functions on the elements and comparison. And if indeed this solution solves the problem in N3, this should be optimal complexity as it takes the same time to verify an answer.

One other detail omitted from the problem definition is whether we are allowed to destroy the data as we process it. I am going to do so for the sake of simplicity, but I think with extra care it could be avoided.

Possible Solution

First, the constraint that there may be negative numbers is an empty one. With one pass through the data, we will record the minimum element, N4, and the number of elements, n. On a second pass, we will add N6 to each element, so the smallest element is now 3. (Note that a constant number of numbers might overflow as a result, so we should do a constant number of additional passes through the data first to test these for solutions.) Once we have our solution, we simply subtract N6 to return it to its original form. Now we have available three special marker values N8, N9, and n0, which are not themselves elements.

Step 1

Use the median-of-medians selection algorithm to determine the 90th percentile element, n1, of the array n2 and partition the array into set two sets n3 and n4 where n3 has the n6 elements greater than n1 and n4 has the elements less than n1. This takes N1 steps (with steps taking N2 on average for O(N) total) time. Elements matching n1 could be placed either into n3 or n4, but for the sake of simplicity, run through array once and test n1 and eliminate it by replacing it with N8. Set n3 originally spans indexes m9, where mn0 is about mn1 of n, and set n4 spans the remaining 90% of indexes mn4.

Step 2

Now we are going to loop through mn5 and for each element mn6 we are going to compute a hash function mn7 into mn4. We’ll use universal hashing to get uniform distribution. So, our hashing function will do multiplication and addition and take linear time on each element with respect to its length.

We’ll use a modified linear probing strategy for collisions:

  1. mn7 is occupied by a member of n4 (meaning O(mn)1 but is not a marker N9 or n0) or is N8. This is a hash table miss. Insert mn6 by swapping elements from slots O(mn)6 and mn7.

  2. mn7 is occupied by a member of n3 (meaning N = nm0) or markers N9 or n0. This is a hash table collision. Do linear probing until either encountering a duplicate of mn6 or a member of n4 or N8.

    • If a member of n4, this is a again a hash table miss, so insert mn6 as in N = nm8 by swapping to slot O(mn)6.

    • If a duplicate of mn6, this is a hash table hit. Examine the next element. If that element is N9 or n0, we’ve seen mn6 more than once already, change N9s into n0s and vice versa to track its change in parity. If the next element is not N9 or n0, then we’ve only seen mn6 once before. We want to store a n0 into the next element to indicate we’ve now seen mn6 an even number of times. We look for the next “empty” slot, that is one occupied by a member of n4 which we’ll move to slot O(mn)6, or a 0, and shift the elements back up to index sqrt(N)3 down so we have room next to mn7 to store our parity information. Note we do not need to store mn6 itself again, so we’ve used up no extra space.

So basically we have a functional hash table with 9-fold the number of slots as elements we wish to hash. Once we start getting hits, we begin storing parity information as well, so we may end up with only 4.5-fold number of slots, still a very low load factor. There are several collision strategies that could work here, but since our load factor is low, the average number of collisions should be also be low and linear probing should resolve them with suitable time complexity on average.

Step 3

Once we finished hashing elements of m9 into mn4, we traverse mn4. If we find an element of S followed by a n0, that is our goal element and we are done. Any element N00 of n3 followed by another element of n3 indicates N00 was encountered only once and can be zeroed out. Likewise N00 followed by a N9 means we saw N00 an odd number of times, and we can zero out the N00 and the marker N9.

Rinse and Repeat as Desired

If we have not found our goal element, we repeat the process. Our 90th percentile partition will move the 10% of n remaining largest elements to the beginning of n2 and the remaining elements, including the empty N8-marker slots to the end. We continue as before with the hashing. We have to do this at most 10 times as we process 10% of n each time.

Concluding Analysis

Partitioning via the median-of-medians algorithm has time complexity of O(N), which we do 10 times, still O(N). Each hash operation takes N15 on average since the hash table load is low and there are N1 hash operations in total performed (about 10% of n for each of the 10 repetitions). Each of the n elements have a hash function computed for them, with time complexity linear to their length, so on average over all the elements N2. Thus, the hashing operations in aggregate are N4. So, if I have analyzed this properly, then on whole this algorithm is N20. (It is also N1 if operations of addition, multiplication, comparison, and swapping are assumed to be constant time with respect to input.)

Note that this algorithm does not utilize the special nature of the problem definition that only one element has an even number of occurrences. That we did not utilize this special nature of the problem definition leaves open the possibility that a better (more clever) algorithm exists, but it would ultimately also have to be O(N).

What is the probability of making an even numbe repeated?

Expert-Verified Answer The number of ways that the four digits formed by the digits 1, 2, 3 4 without repetition will be = 4! = 2 × 3! Answer: The probability of making an even number of 4digits using 1,2,3 and 4 without being repeated is 1/2.

How many even numbers are between 1000 and 9999?

How many numbers between 1000 and 9999 have only even digits? The numbers all have 4 digits. There are 4 choices for the first digit (2, 4, 6, and 8), and 5 choices for every other digit (0, 2, 4, 6, and 8). Using the Product Rule there are 4 × 5 × 5 × 5 = 500 numbers.

What is the probability of marking an even number?

When a die is thrown, getting an even number. ∴ The probability of getting an even number when a die is thrown is 1/2.

How many even numbers between 1 and 1000 have distinct digits?

So the total number of even integers less than 1000 with distinct digits is 4 + 41 + 328 = 373.