arraylist - check to see if 3 values in array are consecutive? -


i have array, example contains values 123456, contains more 3 consecutive values.

i want method return true if array contains @ least 3 consecutive values in it, in advance.

for example:

972834 - return true (234)

192645 - return true (456)

etc. etc..

update! :

i have array in java, takes in 6 integers. example nextturn[], , contains 8 4 2 5 6 5 @ moment sorts array - 2 4 5 5 6 8

how return true if there 3 consecutive numbers throughout array?

ie find 4 5 6

i return position of integer in array, original array 8 4 2 5 6 5

it return, 2 4 5 or 2 5 6

thanks guys, appreciated

the straight forward solution loop through items, , check against next 2 items:

bool hasconsecutive(int[] a){   for(int = 0; < a.length - 2; i++) {     if (a[i + 1] == a[i] + 1 && a[i + 2] == a[i] + 2) return true;   }   return false; } 

another solution loop through items , count consecutive items:

bool hasconsecutive(int[] a){   int cnt = 1;   (int = 1; < a.length; i++) {     if (a[i] == a[i - 1] - 1) {       cnt++;       if (cnt == 3) return true;     } else {       cnt = 1;     }   }   return false; } 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -