I have the following code:
int[] arr = { 1, 2, 3, 4 };
int n = arr.Length;
bool result = areConsecutives(arr, n);
if (result == true)
MessageBox.Show("Yes");
else
MessageBox.Show("No");
static bool areConsecutives(int[] arr, int n)
{
int first_term = int.MaxValue;
for (int j = 0; j < n; j++)
{
if (arr[j] < first_term)
first_term = arr[j];
}
int ap_sum = (n * (2 * first_term + (n - 1) * 1)) / 2;
int arr_sum = 0;
for (int i = 0; i < n; i++)
arr_sum += arr[i];
return ap_sum == arr_sum;
}
This works fine even if I change my array to this:
int[] arr = { 4, 2, 1, 3 }
The problem/question is what can I do to check if the array elements are consecutive, for example if I have the following array:
int[] arr = { 4, 8, 12, 16 }
They are consecutive/multiple by 4 and also the array can be in this way:
int[] arr = { 16, 4, 8, 12 }
3, 5, 11, 9, 7can be put as3, 5, 7, 9, 11? Or (more strict) "consecutive" means that all items can be represented asconst * kwherek = 1, 2, ..., n?