Final answer:
To find the repeated number in an array of n distinct integers where one number is repeated, initialize a counting array, loop through the array, increment corresponding counts, and return the number with a count of 2. The running time of the algorithm is O(n).
Step-by-step explanation:
Given an array A with n distinct integers except one, which is repeated, the algorithm to find the repeated number k is as follows:
- Initialize an array count of size n + 1 to zero.
- Loop through the elements of array A.
- For each element A[i], increment count[A[i]].
- If count[A[i]] becomes 2, return A[i] as k.
The running time of this algorithm is O(n) as it makes a single pass through the array of size n.