Cetiri
Last updated
Last updated
This is basically a math problem.
Sort the array using any sorting algorithm you like
Find the correct common difference: This is done by first finding the difference between first two elements (first_diff
) and the difference between the last two elements (last_diff
). Note that either one of them will be the correct common difference for this arithmetic progression.
If first_diff == last_diff
, that means both of them are the correct common difference.
If first_diff > last_diff
, that means the second element is missing, and it should be the third element minus last_diff
, which is the correct common difference.
If first_diff < last_diff
, that means the third element is missing, and it should be the second element plus the first_diff
, which is the correct common difference.