I want to write a C function that gets as arguments a sorted array of integers, its size and an integer pointer, and which accomplishes the following: 1. Finding and returning the length of the longest sequence of numbers of the same value in the sorted array, and 2. Storing the number from that sequence in the variable that the pointer given as argument is pointing at.
Here’s my attempt that didn’t yield the desired results, and I have no idea why.
Please suggest how I can fix my code so it would perform the desired task.
int MaxSequence(const int arr[], int size, int* number)
{
int i = 0;
int max = 1;
int curr_max = 1;
*number = arr[i];
for(i = 0; i < size - 1; i++) {
if(arr[i] == arr[i+1]) {
curr_max++;
}
else if (curr_max > max) {
max = curr_max;
curr_max = 0;
*number = arr[i];
}
}
return max;
}
Oren1996 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1