Solve in C The array a1n contains sorted integers Write a fu

Solve in C++

The array a(1..n) contains sorted integers. Write a function marlin (a, n) that returns the length of the longest sequence of identical numbers (for example, if a = (1, 1, 1, 2, 3, 3, 5, 6, 6, 6, 6, 7, 9) then marlin returns 4 because the longest sequence 6, 6, 6, 6 contains 4 numbers. Write a demo main program for testing the work of marlin. Explain your solution, and insert comments in your program. The solution should have time complexity O(n).

Solution

Code:

#include<iostream>
using namespace std;

void maxlen(int *a,int n)
{
int tmp=a[0];//saving first number from array a
int tmp_count=1,max_count=0;
int rep_no=a[0];

for(int i=1;i<n;i++)
{
if(tmp==a[i])//If the same number as prev
{
   tmp_count++;
}
else{//If the number is different from prev
   if(max_count<tmp_count)
   {
       max_count=tmp_count;//Calculating max count here
       rep_no=tmp;//The number with the max count
   }
   tmp=a[i];
   tmp_count=1;
}
}

cout<<\"Max_count:\"<<max_count<<\", repeting number:\"<<rep_no<<endl;
}

int main(void)
{

int a[]={1,1,1,2,3,3,5,6,6,6,6,7,9};

maxlen(a,13);

return 0;
}

Output:

Max_count:4, repeting number:6

Solve in C++ The array a(1..n) contains sorted integers. Write a function marlin (a, n) that returns the length of the longest sequence of identical numbers (fo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site