473,396 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can't figure out bug in my method.

This method is used to pass in an array of number and "count" the run of numbers and return the longest run. I have all of the code figured out but when I tried testing it with an array like :

1, 2, 2, 3, 3, 3, 4, 4, 4, 4 - it returned 3 as the longest run... so after not being able to figure it out I found this website. Hope someone can help me!

Expand|Select|Wrap|Line Numbers
  1. public class Numbers
  2. {
  3.    /**
  4.       Computes the length of the longest run (sequence of 
  5.       adjacent repeated values) in an array.
  6.       @param values an array of integer values
  7.       @return the length of the longest run in values
  8.    */
  9.    public int lengthOfLongestRun(int[] values)
  10.    {
  11.  
  12.        int max = 0;
  13.        int count = 0;
  14.        for(int i=0; i < values.length - 2; i ++)
  15.        {
  16.  
  17.  
  18.            if(values[i] == values[i+1] && (i < values.length - 1))
  19.                {
  20.                count ++;
  21.                }
  22.            else
  23.                {
  24.                count ++;
  25.                }
  26.  
  27.            if(max < count)
  28.                {
  29.            max = count;
  30.  
  31.                }
  32.            if(values[i] != values[i+1])
  33.            {
  34.            count = 0;
  35.            }
  36.  
  37.  
  38.        }
  39.  
  40.       return max;
  41.  
  42.  
  43.    }
  44. }
Sep 21 '07 #1
2 1355
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. if(values[i] == values[i+1] && (i < values.length - 1))
  2.                {
  3.                count ++;
  4.                }
  5.            else
  6.                {
  7.                count ++;
  8.                }
This if statement will have the same result no matter what; rethink your reasoning with the else... statement.
Sep 21 '07 #2
JosAH
11,448 Expert 8TB
This method is used to pass in an array of number and "count" the run of numbers and return the longest run. I have all of the code figured out but when I tried testing it with an array like :

1, 2, 2, 3, 3, 3, 4, 4, 4, 4 - it returned 3 as the longest run... so after not being able to figure it out I found this website. Hope someone can help me!
If your array is empty (length == 0) the maximum 'run' of numbers is nul (0) of course.

If the array is not empty think of a 'mark' index value the 'mark'ed index is where
a new number started. At the beginning of your loop mark == 0.

Start looping at position mark+1; the following three situations can occur:

1) you ran off the array at this position i == array.length; so the current 'run'
length is i-mark. Update your statistics and stop.

2) the number is equal to the number at position 'mark'; keep looping.

3) the number at position i is different from the number at position 'mark'. The
length of the current run is i-mark, update your statistics and set mark=i.

That's all there is to it.

kind regards,

Jos
Sep 22 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Richard | last post by:
http://www.dyn-web.com/dhtml/scroll/ This script does what I am looking for. I can change the various variables to suit my needs except for one. The javascript based scrollbar locks into one...
2
by: Craig | last post by:
This has got to be easy, but I can't figure it out. I have a login control with a login button to process the username,password, etc. I have the login control within a login page, and this page...
3
by: lawrence k | last post by:
I noticed, to my surprise, that PHP was still able to figure that "search" is suppose to be an array: http://www.accumulist.com/index.php?search%5BallFields%5D=bluewall I'm impressed with...
8
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In...
12
by: jeremito | last post by:
Please excuse me if this is obvious to others, but I can't figure it out. I am subclassing dict, but want to prevent direct changing of some key/value pairs. For this I thought I should override...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.