Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In an array, a \"run\"& occurs when 2 or more consecutive elements match. For ex

ID: 3829993 • Letter: I

Question

In an array, a "run"& occurs when 2 or more consecutive elements match. For example in the array {2, 3, 3, 3, 3, 5, 5 6}, there is a run of threes, and a run of fives. The length of the run of threes is 4 (because there are 4 threes in a row). Write a method that returns the length of the longest run. If no run occurs, return 0. maxRun (2, 3, 3, 3, 3, 5, 5, 6]) rightarrow 4 maxRun([1, 1 1, 2, 2, 2]) rightarrow 3 maxRun([4, 5]) rightarrow 0 ...Save, Compile, Run int maxRun(int a) {int count = 1, maxCount = 0; for(int I = 0;i

Explanation / Answer

Here is the fix for you:

int maxRun(int[] a)
    {
       int count = 1, maxCount = 0;
       for(int i = 0; i < a.length-1; i++)
       {
          if(a[i] == a[i+1])
              count++;
           else
           {
              if(maxCount < count)
              {
                 maxCount = count;
                 count = 1;
              }
           }
       }
       if(maxCount < count)
       {
           maxCount = count;
           count = 1;
       }
       if(maxCount == 1)
           maxCount = 0;
       return maxCount;  
    }  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote